我有以下表格:
TableA (id, tableB_id, tableC_id)
TableB (id, expirationDate)
TableC (id, expirationDate)
我想从TableA检索由tableB.expirationDate和tableC.expirationDate排序的所有结果。我怎样才能做到这一点?
最佳答案
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