MySQL - #1066 - 不唯一的表/别名:具有多个内部联接的“组件”

时间:2015-11-13 12:31:26

标签: mysql

我有这个查询,我收到错误#1066 - 不唯一的表/别名:'components'。什么似乎是问题?

VBox leftBox = new VBox(addBttn);
leftBox.setPadding(new Insets(10));
leftBox.setAlignment(Pos.CENTER);
addBttn.setPadding(new Insets(100,0,100,0));
addBttn.setPrefWidth(100);
addBttn.setStyle("-fx-background-color: #c3c4c4, linear-gradient(#d6d6d6 50%, white 100%), radial-gradient(center 50% -40%, radius 200%, #e6e6e6 45%, rgba(230,230,230,0) 50%); -fx-background-radius: 30; -fx-background-insets: 0,1,1; -fx-text-fill: black; -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 3, 0.0 , 0 , 1 ); -fx-focus-color: transparent; -fx-base: coral;");

3 个答案:

答案 0 :(得分:4)

您无需在INNER JOIN之前列出表格。实际上,根本不要在FROM子句中使用逗号。所以:

SELECT COUNT(*)
FROM `products`
INNER JOIN `componentsMap` ON componentsMap.product_id = product.id 
INNER JOIN `components` ON componentsMap.component_id = components.id 
INNER JOIN `tradeNamesMap` ON tradeNamesMap.product_id = products.id 
INNER JOIN `tradeNames` ON tradeNamesMap.tradeName_id = tradeNames.id 
WHERE (((((LOWER(inci) LIKE '%abies%') 
          OR (trade_name.LOWER(name) LIKE '%abies%')) 
         OR (components.LOWER(no_cas)='abies')) 
        OR (components.LOWER(no_einecs)='abies')) 
       OR (components.LOWER(name)='abies'))
AND (`published`=1) 
ORDER BY `trade_name`.`name` DESC;

由于COUNT(),上述查询仅返回一行。 order by表示您确实希望每个trade_name.name都有此信息。如果是,则需要GROUP BY

SELECT tn.name, COUNT(*)
FROM `products` p INNER JOIN
     `componentsMap cm
     ON cm.product_id = p.id INNER JOIN
     `components` c
     ON cm.component_id = c.id INNER JOIN
     `tradeNamesMap` tnm
     ON tnm.product_id = p.id INNER JOIN
     `tradeNames` tn
     ON tnm.tradeName_id = tn.id 
WHERE ((LOWER(inci) LIKE '%abies%') OR
       (tn.LOWER(name) LIKE '%abies%') OR
       (c.LOWER(no_cas)='abies') OR
       (c.LOWER(no_einecs)='abies') OR
       (c.LOWER(name)='abies')
      ) AND
      (`published` = 1) 
GROUP BY tn.name
ORDER BY tn.`name` DESC

答案 1 :(得分:0)

INNER JOIN `[components]` ON componentsMap.component_id = components.id 

SELECT COUNT(*) FROM `products`, [`components`], `tradeNames` 

有两个components

答案 2 :(得分:0)

只是猜测,并且未经测试,但我怀疑像这样的东西会做你想要的......

SELECT n.name
     , COUNT(*)
  FROM products p
  JOIN componentsMap pc
    ON pc.product_id = p.id 
  JOIN components c
    ON c.id = pc.component_id 
  JOIN tradeNamesMap pn
    ON pn.product_id = p.id 
  JOIN tradeNames n
    ON n.id = pn.tradeName_id 
 WHERE 
     ( inci LIKE '%abies%'
    OR n.name LIKE '%abies%'
    OR 'abies' IN (c.no_cas,c.no_einecs,c.name)
     )
   AND published = 1
 GROUP
    BY n.name
 ORDER 
    BY n.name DESC