MySQL 组合/连接/多选查询

标签 mysql select

我有些头疼,想弄清楚如何创建一个合适的 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/

相关文章:

mysql - 当插入两个不同的表时,MySQL 是否提高了性能?

mysql - 如何将庞大的实时数据库变成小型测试数据库?

javascript - 使用选择选项添加/编辑/删除行 - Javascript

javascript - jQuery 通过其文本从选择框获取值

java - ORA-00907 : missing right parenthesis when using % wildcard from Java

php - 在更改事件上使用 jquery 从数据库获取值

mysql - 本地 mysql mac osx 上的 'concatenado' 列的 SQL 错误 1406 数据太长,它在 Windows 上完美运行

php - 增加 MySQL wait_timeout 的影响

javascript - 如何将数据从 ajax 加载到 Chosen jquery?

mysql - 从具有总和和计数的表中选择多个数据