我的DB MySQL上有这样的情况:
表_A
Cod |值1 |值2 |值3
TABLE_B
鳕鱼|价值4|价值5
表C Cod |值6
我使用这个查询:
SELECT
table_a.value1 AS Number,
table_a.value2 AS Date,
table_b.name AS Name,
table_b.age AS Age
FROM
table_a
LEFT JOIN table_c
ON table_a.cod = table_c.cod
AND table_c.value5 = 1
LEFT JOIN table_b
ON table_c.cod = table_b.cod
WHERE value3 = 'MY_SEARCH_VALUE'
ORDER BY table_a.cod ASC
一切都很好,但是现在我必须从TABLE_A中选择元素(与TABLE_B和TABLE_C连接)并与TABLE_A上的一个元素连接。
例如,当TABLE_A.Cod = “X”
时,当TABLE_A.Cod = “ALL_OTHERS_CODE”
时,也选择TABLE_A.Cod ='Y'
和TABLE_A.Cod ='Z'
时,则相反。
编辑:(从OP的评论中添加):
我的查询结果
SELECT
TABLE_A.Value1 AS Number,
TABLE_A.Value2 AS Date,
TABLE_B.name AS Name,
TABLE_B.age AS Age
FROM
TABLE_A
LEFT JOIN TABLE_C
ON TABLE_A.Cod = TABLE_C.Cod
AND TABLE_C.Value5 =1
LEFT JOIN TABLE_B
ON TABLE_C.Cod = TABLE_B.Cod
WHERE Value3 = 'DEFENDER'
ORDER BY TABLE_A.Cod ASC
例如:(< code > 12 -< code > 14/01/01 -< code > John -< code > 18 )
我希望结果更像上面的结果。如果我应该尝试另一个代码,例如“DEFENDER_A”
,我希望有:(12
,14/01/01
,约翰
,18
---5
,14/01/01
,弗兰克
,19
)
像这样更改代码。
Select
TABLE_A.Value1 AS Number,
TABLE_A.Value2 AS Date,
TABLE_B.name AS Name,
TABLE_B.age AS Age
FROM TABLE_A LEFT JOIN TABLE_C ON TABLE_A.Cod = TABLE_C.Cod
AND TABLE_C.Value5 =1 LEFT JOIN TABLE_B ON TABLE_A.Cod = TABLE_B.Cod
WHERE Value3 = 'DEFENDER'
ORDER BY TABLE_A.Cod ASC