0

所以这个第一个 SQL 返回零行:

SELECT b.b_id 
FROM `buy` b 
    INNER JOIN `buy_multiples` AS bm 
        ON b.b_id = bm.b_id 
        AND b.buy_status IN (2)  
WHERE bm.`option_type` = 'area_id'  
AND bm.selected_option_key IN (10)
AND bm.`option_type` = 'bzips'  
AND bm.selected_option_key IN (37027)
GROUP BY b.b_id 

第二条 SQL 返回 b_id 5024 和 4987:

SELECT b.b_id 
FROM `buy` b 
    INNER JOIN `buy_multiples` AS bm 
        ON b.b_id = bm.b_id 
        AND b.buy_status IN (2)  
WHERE bm.`option_type` = 'bzips'  
AND bm.selected_option_key IN (37027)
GROUP BY b.b_id 

这第三条 SQL 返回 b_id 5024 和 4987 和 4989:

SELECT b.b_id 
FROM `buy` b 
    INNER JOIN `buy_multiples` AS bm 
        ON b.b_id = bm.b_id 
        AND b.buy_status IN (2)  
WHERE bm.`option_type` = 'area_id'  
AND bm.selected_option_key IN (10)
GROUP BY b.b_id 

为什么第一个返回零行?

4

2 回答 2

1

在第一个查询中,您要查找bm.option_type = 'area_id'
AND的任何记录bm.option_type = 'bzips'。这将始终返回 0 个结果。你想使用:

SELECT b.b_id 
FROM `buy` b 
    INNER JOIN `buy_multiples` AS bm 
        ON b.b_id = bm.b_id 
        AND b.buy_status IN (2)  
WHERE bm.`option_type` = 'area_id'  
AND bm.selected_option_key IN (10)
OR bm.`option_type` = 'bzips'  
AND bm.selected_option_key IN (37027)
GROUP BY b.b_id 
于 2013-04-30T13:00:06.100 回答
0

也许你正在追求这样的东西......

SELECT DISTINCT b.b_id 
  FROM buy b 
  JOIN buy_multiples bm 
    ON bm.b_id = b.b_id 
 WHERE b.buy_status IN (2)  
   AND (
        (bm.option_type = 'area_id' AND bm.selected_option_key IN (10)) 
     OR (bm.option_type = 'bzips' AND bm.selected_option_key IN (37027))
       );  
于 2013-04-30T13:00:12.697 回答