简体   繁体   中英

MYSQL select group values from another table

I have two tables:

Table menu:

+-------------+--------------+
| id_calories | id_type_diet |
+-------------+--------------+
|      39     |      48      |
+-------------+--------------+

And table product_attribute:

+--------------+----------------------+
| id_attribute | id_product_attribute |
+--------------+----------------------+
|      39      |          93          |
+--------------+----------------------+
|      48      |          93          |
+--------------+----------------------+

Is it possible in MYSQL to get values of id_calories and id_type diet from table menu then check if both of these values exist in column id_attribute from table product_attribute and then get the value of id_product_attribute? In this example get id_product_attribute = 93?

This should do the trick:

SELECT b1.id_product_attribute
FROM menu a
LEFT JOIN product_attribute b1 ON a.id_calories = b1.id_attribute
LEFT JOIN product_attribute b2 ON a.id_type_diet = b2.id_attribute
WHERE b1.id_product_attribute = b2.id_product_attribute

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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