简体   繁体   English

MySQL多个查询还是临时表?

[英]MySql multiple queries or temporary table?

I need help from a SQL guru here... 我需要来自SQL专家的帮助...

I have data in the following format: 我有以下格式的数据:

Key  Value
100  A
100  B
300  A
400  B
500  A
500  B
500  C
600  B
600  W

If I want to find all keys with A or B and A and B, but not AB with anything else...how would I do this? 如果我想找到A或B以及A和B的所有键,而不是AB的其他任何键...我该怎么做? Would I need a temp table or can I somehow join the table to itself? 我需要一个临时表,还是可以以某种方式将其自身联接起来?

EDIT: The desired output would be Key 100 since it has A & B, Key 300 since it has A, Key 400 since it has B. Not Key 500 because it also contains a C, and not key 600 because it also contains a W 编辑:所需的输出将是键100,因为它具有A和B,键300,因为它具有A,键400,因为它具有B。不是键500,因为它也包含C,而不是键600,因为它也包含W

This should work for you: 这应该为您工作:

SELECT DISTINCT t1.`key`
FROM table1 t1
WHERE t1.value in('A', 'B')
GROUP BY t1.`key`
HAVING COUNT(t1.value) = (SELECT COUNT(t2.value) 
                          FROM Table1 t2 
                          WHERE t1.`key` = t2.`key`)

SQL Fiddle Demo SQL小提琴演示

This returns only the keys: 这仅返回键:

  • Has A and B only, and nothing else so key = 500 shouldn't be returned. 仅具有AB ,别无其他,因此不应返回key = 500 But key = 100 should be included. 但应包含key = 100
  • Has only A . 只有A As in key = 300 . key = 300
  • Has only B so key = 600 shouldn't be returned since it contains one more value than B which is w . 只有B因此不应返回key = 600因为它包含的值比Bw But key = 400 should be included. 但是应该包含key = 400

Update: How is this working 更新:这是如何工作的

If any key has a value IN('A', 'B') then it could also contains other values as well. 如果任何键的值是IN('A', 'B')那么它也可以包含其他值。

Thats why I added the HAVING clause: 那就是为什么我添加了HAVING子句:

HAVING COUNT(t1.value) = (SELECT COUNT(t2.value) 
                          FROM Table1 t2 
                          WHERE t1.`key` = t2.`key`)

The COUNT(t1.value) is compared to the total COUNT of the values for the same key, by using a correlated subquery SELECT COUNT(t2.value) FROM Table1 t2 WHERE t1.key = t2.key , for each value t1.value . 通过使用相关的子查询SELECT COUNT(t2.value) FROM Table1 t2 WHERE t1.key = t2.key ,对于每个值t1.value ,将COUNT(t1.value)与同一键的总值COUNT相比较t1.value So if the current key contains values other than 'A', 'B' then the COUNT(t1.value) won't equal to the COUNT of all values for the same key. 因此,如果当前键包含的值不是'A', 'B'COUNT(t1.value)将不等于同一键的所有值的COUNT For example the key = 500 has a count = 3 but using: 例如,键= 500的计数= 3,但使用:

SELECT DISTINCT t1.`key`
FROM table1 t1
WHERE t1.value in('A', 'B')
GROUP BY t1.`key`

Without the HAVING clause will include it as well, since it has either A' or 'B'. 如果没有HAVING子句,则还将包含它,因为它具有A'或'B'。 But the HAVING clause eliminates this key since it has a count = 3 not equal to 2. 但是HAVING子句消除了此键,因为它的count = 3不等于2。

You should explain your question better, if i can understand correctly what you need you could use something like this: 您应该更好地解释您的问题,如果我能正确理解您的需求,则可以使用以下方法:

SELECT Distinct K
FROM tab
WHERE
  value IN ('A', 'B') AND
  NOT EXISTS (SELECT Null from tab tab_1
              WHERE tab_1.K = tab.K and tab_1.value not in ('A', 'B'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM