【发布时间】:2014-09-10 21:26:28
【问题描述】:
我实际上是在尝试使用限制从amgb 中选择一条记录,同时与amga 进行连接。
我的桌子是这样的:
amga
"id" "itemId" "itemTempId" "itemName" "itemCountry" "userId"
"1" "US1" "T001" "Samsung Galaxy Note 5" "US" "1"
"2" "CA2" "T002" "Samsung Galaxy Note 6" "CA" "2"
"3" "UK3" "T003" "Samsung Galaxy Note 7" "UK" "3"
amgb
"id" "itemId" "itemTempId" "itemImageName" "userId"
"1" "US1" "T001" "front.jpg" "1"
"2" "US1" "T001" "side-left.jpg" "1"
"3" "US1" "T001" "side-right.jpg" "1"
"4" "US1" "T001" "back.jpg" "1"
"5" "CA2" "T002" "front.jpg" "2"
"6" "CA2" "T002" "side-left.jpg" "2"
"7" "CA2" "T002" "side-right.jpg" "2"
"8" "CA2" "T002" "back.jpg" "2"
"9" "UK3" "T003" "front.jpg" "3"
用简单的语言来说,这相当于:
select itemName from amga where itemId = 'US1' and userId = 1 along with 1 itemImageName from amgb where itemId = sameOneAsBefore and userId = sameOneAsBefore
我希望的最终结果就像这样简单:
Samsung Galaxy Note 5 | front.jpg - 这是来自 amga 的 itemName 和来自 amgb 的 itemImageName 其中 userId = 1 和 itemId = US1
但是,尽管所有连接和其他一切都正确,但我看不出哪里出错了。什么可能导致问题/* SQL Error (1054): Unknown column 'b.userId' in 'on clause' */
SELECT a.itemName, b.itemImageName
FROM amga a
LEFT JOIN (
SELECT
itemImageName
FROM
amgb
LIMIT 1
) b
ON a.userId = b.userId and a.itemId = b.itemId
WHERE a.userId = 1 and a.itemId = 'US1';
【问题讨论】:
-
您的
Left Join中没有名为UserId(或itemId)的列。您拥有的唯一列是itemImageName。错误直接告诉你。 -
我有它。为什么要在那里再次选择它?
-
您不能
Join On一个不存在的列。如果您没有在子选择中选择它,则它不存在。您需要将UserId和ItemId添加到Left JoinSelect语句中。