我有些头疼,想弄清楚如何创建一个合适的 MySQL 查询来检索我需要的信息。 这是我的 TABLE PARAM 中的示例行:
Serial | Repeater1 | Repeater2 | Repeater3
我想检索所有不在序列字段中的 Repeater1、Repeater2 或 Repeater3。 所有这些字段都在同一个表下。
例如:
Serial | Repeater1 | Repeater2 | Repeater3
1234 | 1111 | 2222 | null
1235 | 1111 | null | null
1236 | 1234 | 1111 | 2222
我的 SELECT 查询应该只返回:
1111
2222
由于 Repeater1 中使用的 1234 出现在 Serial 中(因此需要避免),即使 1111 或 2222 出现在不同的列(本例中的 Repeater2 和 Repeater3)下,我只想列出 DISTINCT Repeater1、Repeater2 或 Repeater3不是连续剧。
你有什么想法来完成这个请求吗?
很多提前帮助!
最佳答案
select * from (
select distinct Repeater1 as repeater from table1 where Repeater1 is not null
UNION
select distinct Repeater2 as repeater from table1 where Repeater2 is not null
UNION
select distinct Repeater3 as repeater from table1 where Repeater3 is not null
) as datarepeater
where datarepeater.repeater not in (select serial from table1) and datarepeater.repeater!=0
关于MySQL 组合/连接/多选查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43470606/