我有点头疼试图弄清楚如何创建一个合适的MySQL查询来检索我需要的信息。 以下是我的TABLE PARAM中的示例行:
Serial | Repeater1 | Repeater2 | Repeater3
我想检索不在Serial字段中的所有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不是串行。
您有任何想法要完成此请求吗?
许多人提前帮忙!
答案 0 :(得分:0)
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
答案 1 :(得分:0)
select r from (
select Repeater1 as r from t WHERE Repeater1 IS NOT NULL
union
select Repeater2 as r from t WHERE Repeater2 IS NOT NULL
union
select Repeater3 as r from t WHERE Repeater3 IS NOT NULL
) reps
left join t
on reps.r = t.Serial
WHERE t.Serial IS NULL