我有两个MySQL查询
QUERY:
SELECT sodnik_1 FROM prihodnji_krog WHERE file_id='8778' AND sodnik_1 != ''
UNION
SELECT sodnik_2 FROM prihodnji_krog WHERE file_id='8778' AND sodnik_2 != ''
UNION
SELECT sodnik_3 FROM prihodnji_krog WHERE file_id='8778' AND sodnik_3 != ''
UNION
SELECT sodnik_4 FROM prihodnji_krog WHERE file_id='8778' AND sodnik_4 != ''
QUERY:
SELECT value FROM notification_sodniki WHERE user_id='16'
OUTPUT LOOKS LIKE THIS:
Name 1
Name 2
Name 3
Name 4
IN BOTH TABLES
他们给了我一栏。我想执行交叉连接并仅返回两个选择查询中存在的值。这可能吗?
答案 0 :(得分:25)
怎么样
SELECT * FROM
(
SELECT sodnik_1 as value FROM prihodnji_krog WHERE file_id='8778' AND sodnik_1 != ''
UNION
SELECT sodnik_2 as value FROM prihodnji_krog WHERE file_id='8778' AND sodnik_2 != ''
UNION
SELECT sodnik_3 as value FROM prihodnji_krog WHERE file_id='8778' AND sodnik_3 != ''
UNION
SELECT sodnik_4 as value FROM prihodnji_krog WHERE file_id='8778' AND sodnik_4 != ''
) x INNER JOIN
(
SELECT value FROM notification_sodniki WHERE user_id='16'
) y
ON x.value = y.value
在FROM
子句中使用子查询时,就像是在制作临时表一样。
然后使用别名,您可以引用它们并执行INNER JOIN