sql查询困难

时间:2010-04-30 17:34:41

标签: sql mysql

我有以下表格:

TableA (id, tableB_id, tableC_id)
TableB (id, expirationDate)
TableC (id, expirationDate)

我想从tableB.expirationDate和tableC.expirationDate排序的TableA中检索所有结果。我怎么能这样做?

3 个答案:

答案 0 :(得分:3)

select ta.*
from TableA ta
inner join TableB tb on ta.tableB_id = tb.id
inner join TableC tc on ta.tableC_id = tc.id
order by tb.expirationDate, tc.expirationDate

更新:

如果您没有收到所有记录,那么您需要使用left outer join

select ta.*
from TableA ta
left outer join TableB tb on ta.tableB_id = tb.id
left outer join TableC tc on ta.tableC_id = tc.id
order by tb.expirationDate, tc.expirationDate

答案 1 :(得分:2)

如果结果集为空并带有其他建议,您确定表中的数据是否实际上彼此正确相关?

您可以为每个表发布一些示例行吗?

答案 2 :(得分:0)

你试过了吗?

SELECT a.* FROM TableA  a
INNER JOIN TableB b on b.id = a.tableB_id
INNER JOIN TableC c on c.id = a.tableC_id
ORDER BY b.expirationDate, c.expirationDatetableB_id