MySQL 查询错误代码:1066 不是唯一的表/别名:“products_stock_location”)?

发布于 2025-01-11 14:06:12 字数 1793 浏览 0 评论 0原文

我环顾四周寻找答案,但似乎找不到。

我不是任何形式的 mysql 数据库分析师。查询根本不是我的强项。

我正在尝试从多个表中提取数据,我可以获得大部分数据,但我需要使用不同的 FROM 来获取最后一位数据。

这是我想要合并在一起的两个工作查询 -

USE *database-name*;
SELECT products.products_id, products_with_attributes_stock.sku, products_description.products_name, products.products_model, products_with_attributes_stock.stock_attributes, products.products_price, products.products_cost, products_stock_location.quantity
FROM products, products_stock_location
INNER JOIN products_stock_location ON products.products_id=products_stock_location.products_id
INNER JOIN products_description ON products.products_id=products_description.products_id
INNER JOIN products_with_attributes_stock ON products.products_id=products_with_attributes_stock.products_id;


USE *database-name*;
SELECT stock_location.stock_location_name
FROM products_stock_location
INNER JOIN stock_location ON products_stock_location.stock_locations_id=stock_location.stock_location_id;

我已经尝试过,但它给出了 1066 错误:

USE *database-name*;
SELECT products.products_id, products_with_attributes_stock.sku, products_description.products_name, products.products_model, products_with_attributes_stock.stock_attributes, products.products_price, products.products_cost, products_stock_location.quantity, stock_location.stock_location_name
FROM products, products_stock_location
INNER JOIN products_stock_location ON products.products_id=products_stock_location.products_id
INNER JOIN products_description ON products.products_id=products_description.products_id
INNER JOIN products_with_attributes_stock ON products.products_id=products_with_attributes_stock.products_id
RIGHT JOIN stock_location ON products_stock_location.stock_locations_id=stock_location.stock_location_id;

I have looked around for an answer but I can't seem to find one.

I am not a mysql database anaylst in any shape or form. Queries are not my strongsuit at all.

I am trying to pull data from multiple tables, I can get most of the data how ever the last bit of data I need to use a different FROM.

Here are the two working queries I want to merge together -

USE *database-name*;
SELECT products.products_id, products_with_attributes_stock.sku, products_description.products_name, products.products_model, products_with_attributes_stock.stock_attributes, products.products_price, products.products_cost, products_stock_location.quantity
FROM products, products_stock_location
INNER JOIN products_stock_location ON products.products_id=products_stock_location.products_id
INNER JOIN products_description ON products.products_id=products_description.products_id
INNER JOIN products_with_attributes_stock ON products.products_id=products_with_attributes_stock.products_id;


USE *database-name*;
SELECT stock_location.stock_location_name
FROM products_stock_location
INNER JOIN stock_location ON products_stock_location.stock_locations_id=stock_location.stock_location_id;

I have tried this but it gives the 1066 error:

USE *database-name*;
SELECT products.products_id, products_with_attributes_stock.sku, products_description.products_name, products.products_model, products_with_attributes_stock.stock_attributes, products.products_price, products.products_cost, products_stock_location.quantity, stock_location.stock_location_name
FROM products, products_stock_location
INNER JOIN products_stock_location ON products.products_id=products_stock_location.products_id
INNER JOIN products_description ON products.products_id=products_description.products_id
INNER JOIN products_with_attributes_stock ON products.products_id=products_with_attributes_stock.products_id
RIGHT JOIN stock_location ON products_stock_location.stock_locations_id=stock_location.stock_location_id;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长不大的小祸害 2025-01-18 14:06:12

您在“合并”查询中使用了products_stock_location 表两次。为其中之一指定一个别名,并在需要时使用该别名。

编辑:我想第一个可以删除。

变成

FROM products, products_stock_location INNER JOIN products_stock_location ON products.products_id=products_stock_location.products_id

FROM products
INNER JOIN products_stock_location ON products.products_id=products_stock_location.products_id

You use the table products_stock_location twice in your 'merged' query. Give one of them an alias and use the alias where needed.

Edit: I guess the first one can be removed.

Change:

FROM products, products_stock_location INNER JOIN products_stock_location ON products.products_id=products_stock_location.products_id

Into:

FROM products
INNER JOIN products_stock_location ON products.products_id=products_stock_location.products_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文