[英]How to apply result of order by clause from subquery to main query
我有2個表,這些表由名為MID
的列鏈接。 我想從第一張表中獲取名稱,但是第二張表中提到了該順序。 我的查詢如下:
select name from table1 where MID in(select MID from table2 where CID="100" ORDER BY sequenceNum);
如果我只運行括號內提到的查詢,那么我將按順序對數據進行排序。 但是上面的查詢是從db中直接獲取數據,而不是按順序排列。 可能是什么問題?
我認為這應該可以解決問題...
SELECT name FROM table1
INNER JOIN table2 ON Table2.MID = table1.MID AND CID="100"
ORDER BY
table2.sequenceNum
您要合並兩個表並按合並的表排序結果:
SELECT table1.name
FROM table1
LEFT JOIN table2 ON (table2.MID = table1.MID)
WHERE table2.CID = "100"
ORDER BY table2.sequenceNum;
要么
SELECT table1.name
FROM table1
LEFT JOIN table2 ON (table2.MID = table1.MID AND table2.CID = "100")
ORDER BY table2.sequenceNum;
如果要從具體表中獲取字段,請使用表前綴,如table1.name
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.