简体   繁体   English

Mysqli-#1052-字段列表中的列“ id”不明确

[英]Mysqli - #1052 - Column 'id' in field list is ambiguous

SELECT id,firstName,lastName, SUM(qty*price) 
       FROM orders,menu,customers  
       WHERE menu.id = orders.id_menu 
         AND orders.id_customer = customers.id 
 GROUP BY (id,firstName,lastName) 
 ORDER BY (SUM(qty*price)) LIMIT 1;

1052 - Column 'id' in field list is ambiguous 1052-字段列表中的列“ id”不明确

i need to get the best customer 我需要获得最好的客户

Some simple rules when writing SQL: 编写SQL时的一些简单规则:

  • Never use commas in the FROM clause. 请勿在FROM子句中使用逗号。
  • Always use explicit, proper JOIN syntax. 始终使用明确的,正确的JOIN语法。
  • Always alias your tables with abbreviations. 始终在表的别名上加上缩写。
  • Always qualify your column references. 始终限定您的列引用。

Your resulting queries are more likely to work the first time: 您产生的查询更有可能在第一次运行:

SELECT c.id, c.firstName, c.lastName, SUM(o.qty * m.price)
FROM orders o JOIN
     menu m
     ON m.id = o.id_menu JOIN
     customers c 
     ON  c.id = o.id_customer
GROUP BY c.id, c.firstName, c.lastName
ORDER BY SUM(o.qty * m.price)
LIMIT 1;

Change the query to this 将查询更改为此

SELECT customers.id,firstName,lastName, SUM(qty*price) FROM orders,menu,customers  WHERE menu.id = orders.id_menu AND orders.id_customer = customers.id GROUP BY (customers.id,firstName,lastName) ORDER BY (SUM(qty*price)) LIMIT 1;

I believe all these tables have column called 'id' and you need to specify which one you need otherwise it will give you this message. 我相信所有这些表都有名为“ id”的列,您需要指定所需的表,否则它将给您此消息。

您在不同的表中有许多列作为名称“ id”,因此mysql无法知道它的来源(menu.id与customer.id)。

change to: 改成:

SELECT id,firstName,lastName, SUM(qty*price) 
   FROM orders,menu,customers  
   WHERE menu.id = orders.id_menu 
     AND orders.id_customer = customers.id 
GROUP BY (orders.id,firstName,lastName) 
ORDER BY (SUM(qty*price)) LIMIT 1;

you want orders.id as those will be associated with qty and price 您需要orders.id,因为这些将与数量和价格相关联

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

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