简体   繁体   English

SQL:如何从多个行中选择一个满足多个条件的单个ID

[英]SQL: how to select a single id that meets multiple criteria from multiple rows

On a MySQL database, I have the table below 在MySQL数据库上,我有下表

package_content :

id | package_id | content_number | content_name | content_quality

1       99           11               Yellow           1
2       99           22               Red              5
3       101          11               Yellow           5
4       101          33               Green            5
5       101          44               Black            5
6       120          11               Yellow           5
7       120          55               White            5
8       135          66               Pink             5
9       135          99               Orange           5
10      135          11               Yellow           5

and i am looking a possibility to make search queries on it: 我正在寻找一种对它进行搜索查询的可能性:

I would like to select the package_id where content_number could be 11 AND 22 (In this case it should select only package_id 99 我想选择package_id ,其中content_number可以是11 AND 22 (在这种情况下,它应该只选择package_id 99

I really don't know if it's possible in SQL since the statement AND will always results as false. 我真的不知道在SQL中是否可行,因为语句AND始终会导致结果为false。 If i use the statement OR i also get the package_id 99, 101, 120, 135 and that's not what i want. 如果我使用语句OR我也得到package_id 99, 101, 120, 135那不是我想要的。

Maybe my table is not well designed too, but any suggestions would help! 也许我的桌子设计也不是很好,但是任何建议都会有所帮助! Thanks in advance 提前致谢

Edit 编辑

I added the content_quality column 我添加了content_quality

I used the sql query from juergen, works very well 我使用了juergen的sql查询,效果很好

select package_id
from package_content
where content_number in (11,22)
group by package_id
having count(distinct content_number) = 2

My last question is how could i now add another criteria : Select the package_id where content_number is 11 and 22 and content_number 11 has content_quality 1 我的最后一个问题是我现在如何添加另一个条件:选择package_id ,其中content_number11 22 content_number 11具有content_quality 1

Edit 2: 编辑2:

For the 2nd question i use now this query. 对于第二个问题,我现在使用此查询。 Thanks to both of you who helped me! 感谢你们两个帮助我的人! :) :)

SELECT *
FROM (
   SELECT package_id
   FROM package_content
   WHERE 
      (content_number=11 AND content_quality > 1)
      OR (content_number = 33 AND content_quality = 5)
      OR (content_number = 44 AND content_quality =5 AND content_name like 'Black')
   GROUP BY package_id
   HAVING count( DISTINCT content_number) = 3
   )t1
LEFT JOIN package_content ON package_content.package_id = t1.package_id

This will output 这将输出

id | package_id | content_number | content_name | content_quality

3       101          11               Yellow           5
4       101          33               Green            5
5       101          44               Black            5

You need to group by the package_id and then use having to perform an aggregate function over the grouped data 您需要按package_id分组,然后使用having对分组的数据执行汇总功能

select package_id
from package_content
where content_number = 22
or
(
    content_number = 11 and content_quality = 1
)
group by package_id
having count(distinct content_number) = 2

You could query with a self join for that: 您可以使用自我联接进行查询:

SELECT DISTINCT package_id
FROM package_content a, package_content b
WHERE a.package_id = b.package_id
      AND a.content_number = 11 AND b.content_number = 22

Edit: For your second question: Just add that to the query. 编辑:对于第二个问题:只需将其添加到查询中即可。 The package_content renamed to a is responsible for the content_number 11. Therefore you can ask, wether a has content_quality 1: 重命名为a的package_content负责content_number11。因此,您可以询问a是否具有content_quality 1:

SELECT DISTINCT package_id
FROM package_content a, package_content b
WHERE a.package_id = b.package_id
      AND a.content_number = 11 AND b.content_number = 22
      AND a.content_quality = 1

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

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