简体   繁体   English

MYSQL从另一个表中选择组值

[英]MYSQL select group values from another table

I have two tables: 我有两个表:

Table menu: 表格菜单:

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

And table product_attribute: 和表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? 在MYSQL中是否可以从表菜单获取id_calories和id_type Diet的值,然后从表product_attribute的id_attribute列中检查这两个值是否都存在,然后获取id_product_attribute的值? In this example get id_product_attribute = 93? 在此示例中,获取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

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

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