I have a table:
custID orderID orderComponent
=====================================
1 123 pizza
1 123 wings
1 234 breadsticks
1 239 salad
2 456 pizza
2 890 salad
I have a list of values – pizza, wings, breadsticks, and salad. I need a way to just get a true/false value if a customer has at least one record containing each of these. Is that possible with a mysql query, or do I just have to do a select distinct(orderComponent) for each user and use php to check the results?
If you are just looking to see if the customer has ordered all items, then you can use:
See SQL Fiddle with Demo
If you want to expand this out, to see if the
custidhas ordered all items in a single order, then you can use:See SQL Fiddle with Demo
If you only want the custid and the true/false value, then you can add
distinctto the query.See SQL Fiddle with Demo
Or by custid and orderid:
See SQL Fiddle with Demo