代码之家  ›  专栏  ›  技术社区  ›  pelkas13

MySQL从另一个表中选择组值

  •  0
  • pelkas13  · 技术社区  · 7 年前

    我有两张桌子:

    表格菜单:

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

    表产品属性:

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

    MySQL是否可以从表菜单中获取id_卡路里和id_类型饮食的值,然后检查这两个值是否都存在于表product_属性的id_属性列中,然后获取id_product_属性的值? 在这个例子中,get id_product_attribute=93?

    1 回复  |  直到 7 年前
        1
  •  1
  •   schroedingersKat    7 年前

    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