无法让sql表加入工作

时间:2017-06-18 05:23:38

标签: mysql sql

我有3个表,我想运行查询,我想要做的是如下:

SELECT table1.name AS product_name,
            table1.description AS product_description,
            table2.name AS product_category
            table3.product_id
            FROM table1
            INNER JOIN table1 a ON table3.product_id = table1.product_id
            INNER JOIN  table2 b ON table3.category_id = table2.category_id
            INNER JOIN table2 ON table1.product_id = table2.product_id;

TABLE2和TABLE1之间的链接是product_ID和category_ID,它们都在TABLE3上

所以这是我试图运行的查询

{{1}}

表的结构如下: 表1(产品编号,名称,描述) 表2(CATEGORY_ID,名2) 表3(product_id,category_id)

2 个答案:

答案 0 :(得分:1)

如果您使用assign alias(table1 a,表2b)到表,则使用它,否则不指定。 您正在加入两个时间表2而不是表3,并假设table2和table1通过category_id相关联 和table3 table1通过product_id和category_id

相关联
  SELECT 
       table1.name AS product_name
      ,table1.description AS product_description
      ,table2.name AS product_category
      ,table3.product_id
  FROM table1
  INNER JOIN table2  table1.category_id = table2.category_id ;
  INNER JOIN table3  ON table3.product_id = table1.product_id  
          and table3.category_id = table1.category_id

答案 1 :(得分:0)

Table1.category_id = Table2.category_id 的条件在哪里?