本文介绍了我很胖吗? MySQL:“不是唯一的表/别名";在LEFT JOIN上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下SQL语句,该语句返回"非唯一表/别名".我相当确定我只是在这里遗漏了一些明显的东西,可能在引用StockID时不够具体,因为它是StockSuppliersStock的通用字段名称. Stock中的主键,SuppliersStock

中的外键

SELECT Stock.BuyingPrice, SuppliersStock.StockID, SuppliersStock.Quantity
FROM Stock
LEFT JOIN Stock on Stock.StockID = SuppliersStock.StockID 
WHERE Stock.StockID = <some-integer />

Stock表具有有关库存的特定信息,Suppliers具有有关供应商的信息,SuppliersStock具有具有StockSuppliers的外键引用的新库存的信息订单.

我想做的是返回BuyingPrice(来自Stock),StockIDQuantity(来自SuppliersStock)字段,这样我可以生成新库存订单的成本清单. /p>

免责声明:我知道,关于SQL连接的另一个问题.不要伤害我!我已经用Google搜索,搜索了,但是我有点困惑,我一直试图寻找一个类似的问题,以了解更多有关如何自己解决这个问题的知识,但结果胜过一败涂地........................................................请帮忙吗?

解决方案

好像您将别名跳过到SuppliersStock,或者它是另一个表:

/* If SuppliersStock is a different table */
SELECT Stock.BuyingPrice, SuppliersStock.StockID, SuppliersStock.Quantity
FROM Stock
LEFT JOIN SuppliersStock on Stock.StockID = SuppliersStock.StockID 
WHERE Stock.StockID = <some-integer />

/* If SuppliersStock is the same table, needing an alias */
SELECT Stock.BuyingPrice, SuppliersStock.StockID, SuppliersStock.Quantity
FROM Stock
LEFT JOIN Stock AS SuppliersStock on Stock.StockID = SuppliersStock.StockID 
WHERE Stock.StockID = <some-integer />

I have the following SQL statement which returns "Not unique table/alias". I'm fairly sure I'm just missing something obvious here, possibly not being specific enough when referring to StockID as it's a common field name to Stock and SuppliersStock. Primary Key in Stock, Foreign Key in SuppliersStock

SELECT Stock.BuyingPrice, SuppliersStock.StockID, SuppliersStock.Quantity
FROM Stock
LEFT JOIN Stock on Stock.StockID = SuppliersStock.StockID 
WHERE Stock.StockID = <some-integer />

The Stock table has specific information about stock, Suppliers has info on suppliers and SuppliersStock has information orders for new stock with foreign key references to Stock and Suppliers.

What I want to do is return the BuyingPrice (from Stock), StockID, Quantity (from SuppliersStock) fields so I can produce a list of costs for ordering in new stock.

Disclaimer: I know, another question on SQL joins. Don't hurt me! I've Googled, I've searched but I'm a bit befuddled and I've honestly tried to look for a similar question to learn more about what I can do to solve this myself but come up trumps. Please help?

解决方案

Looks like you are skipping the alias to SuppliersStock, or it is a different table:

/* If SuppliersStock is a different table */
SELECT Stock.BuyingPrice, SuppliersStock.StockID, SuppliersStock.Quantity
FROM Stock
LEFT JOIN SuppliersStock on Stock.StockID = SuppliersStock.StockID 
WHERE Stock.StockID = <some-integer />

/* If SuppliersStock is the same table, needing an alias */
SELECT Stock.BuyingPrice, SuppliersStock.StockID, SuppliersStock.Quantity
FROM Stock
LEFT JOIN Stock AS SuppliersStock on Stock.StockID = SuppliersStock.StockID 
WHERE Stock.StockID = <some-integer />

这篇关于我很胖吗? MySQL:“不是唯一的表/别名";在LEFT JOIN上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 11:56