MYSQL从另一个表中选择组值

时间:2018-07-10 09:37:49

标签: mysql

我有两个表:

表格菜单:

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

和表product_attribute:

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

在MYSQL中是否可以从表菜单中获取id_calories和id_type Diet的值,然后从表product_attribute的id_attribute列中检查这两个值是否都存在,然后获取id_product_attribute的值? 在此示例中,id_product_attribute = 93?

1 个答案:

答案 0 :(得分:1)

这应该可以解决问题:

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