mysql - SQL: how to select a single id that meets multiple criteria from multiple rows -


on mysql database, have table below

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 looking possibility make search queries on it:

i select package_id content_number 11 and 22 (in case should select package_id 99

i don't know if it's possible in sql since statement and results false. if use statement or package_id 99, 101, 120, 135 , that's not want.

maybe table not designed too, suggestions help! in advance

edit

i added content_quality column

i used sql query juergen, works well

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

my last question how add criteria : select package_id content_number 11 and 22 and content_number 11 has content_quality 1

edit 2:

for 2nd question use query. both of helped me! :)

select * (    select package_id    package_content           (content_number=11 , content_quality > 1)       or (content_number = 33 , content_quality = 5)       or (content_number = 44 , content_quality =5 , content_name 'black')    group package_id    having count( distinct content_number) = 3    )t1 left join package_content on package_content.package_id = t1.package_id 

this 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 group package_id , use having perform aggregate function on grouped data

select package_id package_content content_number = 22 or (     content_number = 11 , content_quality = 1 ) group package_id having count(distinct content_number) = 2 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -