关于mysql join的歧义条款

时间:2013-01-22 05:41:39

标签: mysql join

我正在尝试连接2个表来获取匹配的值但是我得到了模糊的子句

我的代码在这里

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

我的表结构的一个例子可以在这里找到 错误是on子句中的'auction_id'列是不明确的

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

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

6 个答案:

答案 0 :(得分:4)

试试这个

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

答案 1 :(得分:1)

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

答案 2 :(得分: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

或者您可以使用'USING'代替

SELECT * 
FROM auction_media
    JOIN auctions
    USING (auction_id)
    WHERE media_type = '3'

答案 3 :(得分:0)

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

答案 4 :(得分:0)

只需根据表名引用列名称。

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

答案 5 :(得分:0)

这样做:

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

阅读SQL JOIN