1

我正在尝试加入 2 个表以获取匹配值,但得到了模棱两可的子句,

我的代码在这里

SELECT *
FROM auction_media
  JOIN auctions
    ON auction_id = auction_id
    WHERE media_type = '3'

可以在此处找到我的表结构的示例错误是 on 子句中的 Column 'auction_id' is ambiguous

http://sqlfiddle.com/#!2/13583

我应该怎么做才能解决这个问题?

4

6 回答 6

4

试试这个

SELECT *
FROM auction_media
JOIN auctions
ON auction_media.auction_id = auctions.auction_id
WHERE media_type = '3'
于 2013-01-22T05:43:39.390 回答
1
SELECT *
FROM auction_media
  JOIN auctions
    ON auction_media.auction_id = auctions.auction_id
    WHERE media_type = '3'
于 2013-01-22T05:43:55.973 回答
1

你需要给你的表起别名。问题是因为多个列具有相同的名称,在这种情况下是您尝试加入的列。尝试

SELECT * 
FROM auction_media AS am
    JOIN auctions AS a
    ON am.auction_id = a.auction_id
    WHERE media_type = '3'

更新:我给它们起了别名以使其更短且更易读,但另一种选择是使用完整的 table.column 名称,例如auction_media.auction_id

或者您可以改用“使用”

SELECT * 
FROM auction_media
    JOIN auctions
    USING (auction_id)
    WHERE media_type = '3'
于 2013-01-22T05:44:37.863 回答
0
SELECT *
FROM auction_media
  JOIN auctions
    ON auction_media.auction_id = auctions.auction_id
    WHERE media_type = '3'
于 2013-01-22T05:43:49.813 回答
0

只需根据表名引用列名。

SELECT * FROM auction_media am JOIN auction a ON am.auction_id = a.auction_id
WHERE media_type = '3'
于 2013-01-22T05:45:43.023 回答
0

这样做:

SELECT * FROM auction_media JOIN auctions ON auction_media.auction_id = auctions.auction_id WHERE media_type = '3'

阅读SQL JOIN

于 2013-01-22T05:49:29.237 回答