12

我有两个 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

他们给了我 1 列。我想执行交叉连接并仅返回两个选择查询中都存在的值。那可能吗 ?

4

1 回答 1

31

怎么样

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

于 2012-11-15T15:33:05.330 回答