下面的MySQL语句给了我三倍相同的结果,因为我需要从不同的表中选择所有数据来进行比较。我怎么能告诉MySQL我只需要其中一个条目?
SELECT watchedItems.id FROM watchedItems, globalItems, bidGroups
WHERE (watchedItems.bidGroupID IS NULL OR (watchedItems.bidGroupID IS NOT NULL AND bidGroups.bidGroupQty > 0))
AND watchedItems.aid = globalItems.aid
AND watchedItems.maxPrice > globalItems.currentPrice
输出:
ID
2
2
2
答案 0 :(得分:3)
SELECT distinct(watchedItems.id) FROM ...
http://dev.mysql.com/doc/refman/5.0/en/select.html
“DISTINCT指定从结果集中删除重复的行。”
答案 1 :(得分:0)
你需要一些联接:
SELECT watchedItems.id
FROM watchedItems
left join globalItems on watchedItems.bidGroupID = bidGroups.bidGroupID
left join bidGroups on watchedItems.aid = globalItems.aid
WHERE (watchedItems.bidGroupID IS NULL OR bidGroups.bidGroupQty > 0)
AND watchedItems.maxPrice > globalItems.currentPrice
您获得了重复项,因为bidGroups表中有三行。