我有表A存储代码,表B存储项目详细信息
TableA TableB
-------------------- -------------------------------------
code_id code_name item_id item_name code_1 code_2
-------------------- -------------------------------------
1 AAA 1 apple 1 2
2 BBB 2 orange 1 1
-------------------- -------------------------------------
Dim query as String = "SELECT item_name, TableA.code_name
FROM TableB
LEFT JOIN TableA
ON TableB.code_1 = TableA.code_id
WHERE item_name = apple"
Result
-----------------------
item_name code_name
-----------------------
apple AAA
-----------------------
以上查询只会返回一个代码名称,如果我想检索code_1和code_2?
Result that I want
----------------------------
item_name code_1 code_2
----------------------------
apple AAA BBB
----------------------------
答案 0 :(得分:2)
SELECT S1.item_name, U.code_name
FROM tableA U
JOIN tableB S1 ON (S1.code_1 = U.code_id)
JOIN tableB S2 ON (S2.code_2 = U.code_id)
WHERE item_name = apple"
答案 1 :(得分:1)
您应该在SELECT中指定要反选的列
SELECT item_name, A.code_name, B.code_name FROM TableB WHERE item_name = 'apple'
LEFT JOIN TableA A ON TableB.code_1 = TableA.code_id
LEFT JOIN TableA B ON TableB.code_2 = TableA.code_id