我有一个包含4列的prices
表id
,price1
,price2
,date
)
一些解释:
对i.E的正常查询选择Id,price1和price2使用查询
SELECT `id`, `price1`, `price2`
FROM `prices`
WHERE `price1` = '1.23' AND `price='3.45' AND `date`='J2015'
然后客户想要扩展搜索。他想要所有通过以下标准的ID(例如:)
price1 ='1.50'和 price2 ='1.75'且日期 ='AP2013'
还有
price1 ='1.39'和 price2 ='1.45'和日期 ='MAY2013'
起初我想使用IN参数但是会/可能会返回不正确的数据。所以我在这一点上陷入困境。
答案 0 :(得分:1)
使用自我加入
SELECT p1.id
FROM prices AS p1
JOIN prices AS p2 ON p1.id = p2.id
WHERE p1.price1 = '1.50' AND p1.price2 = '1.75' AND p1.date = 'AP2013'
AND p2.price1 = '1.39' AND p2.price2 = '1.45' AND p2.date = 'MAY2013'
答案 1 :(得分:1)
这可行:
SELECT id
FROM prices
WHERE id IN (SELECT id FROM prices WHERE price1 = '1.50' AND price2 = '1.75' AND date = 'AP2013')
AND (price1 = '1.39' AND price2 = '1.45' AND date = 'MAY2013');