gpt4 book ai didi

mysql内连接语句错误

转载 作者:行者123 更新时间:2023-11-29 14:15:13 26 4
gpt4 key购买 nike

我收到以下错误:

1052 - Column 'product_id' in field list is ambiguous

当我运行以下命令时:

SELECT `product_id`, `product_name`
FROM `products`
INNER JOIN `products_has_product_category`
ON `products.product_id` = `products_has_product_category.product_id`
AND `products_has_product_category.category_id` = 1
ORDER BY `products.product_name`

我的产品表有

product_id, product_name, etc 

我的products_has_product_category表有

product_id, category_id

这是我第一次尝试加入,因此非常感谢您的帮助!

最佳答案

您需要指定product_id来自哪个表。由于product_id 位于两个表中,因此当您SELECT 时,您需要指定要从哪个表获取值。使用表别名:

SELECT p.product_id, p.product_name
FROM `products` p
INNER JOIN `products_has_product_category` pc
ON p.product_id = pc.product_id
AND pc.category_id = 1
ORDER BY p.product_name

没有表别名:

SELECT `products`.`product_id`, `products`.`product_name`
FROM `products`
INNER JOIN `products_has_product_category`
ON `products.product_id` = `products_has_product_category.product_id`
AND `products_has_product_category.category_id` = 1
ORDER BY `products.product_name`

关于mysql内连接语句错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12809276/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com