MySQL查询返回0行,而它应返回一行

时间:2012-02-21 13:08:56

标签: mysql sql

下面我的MySQL查询有问题,但我找不到问题。它没有返回任何错误,但下面的查询应返回1行,但它不返回任何错误。

表'fws_product'包含所有产品。表'webits_product_has_kenmerken'包含产品规格。

SELECT fws_product.* 
FROM webits_product_has_kenmerken 
LEFT JOIN fws_product ON webits_product_has_kenmerken.product_id = fws_product.ID 
WHERE fws_product.CATID = 11 
AND ( 
(webits_product_has_kenmerken.kenmerk_id = 8 AND webits_product_has_kenmerken.kenmerk_value = 'Buddha to Buddha') 
AND
(webits_product_has_kenmerken.kenmerk_id = 19 AND webits_product_has_kenmerken.kenmerk_value = '10 mm')
)

提前致谢!

3 个答案:

答案 0 :(得分:2)

看起来有点讨厌,但以下情况应该按照您的要求进行

SELECT
    p.*
FROM fws_product AS p

INNER JOIN webits_product_has_kenmerken AS ps8 
    ON ps8.product_id = p.ID
    AND ps8.kenmerk_id = 8
    AND ps8.kenmark_value = 'Buddha to Buddha'

INNER JOIN webits_product_has_kenmerken AS ps19 
    ON ps19.product_id = p.ID
    AND ps19.kenmerk_id = 19
    AND ps19.kenmark_value = '10 mm'

WHERE p.CATID = 11

这是另一个可能做到这一点的潜在选择,但仍然感觉非常讨厌

SELECT
    p.*
FROM fws_product AS p
INNER JOIN (
    SELECT 
        product_id,
        COUNT(*) AS numMatches
    FROM webits_product_has_kenmerken
    WHERE (kenmerk_id,kenmerk_value) IN (
            (8,'Buddha to Buddha'),
            (19,'10 mm')
        )
    GROUP BY product_id
    HAVING numMatches = 2
) AS ps
ON ps.product_id = p.ID

WHERE p.CATID = 11

答案 1 :(得分:0)

我认为您需要以下内容:

SELECT fws_product.* 
FROM webits_product_has_kenmerken 
LEFT JOIN fws_product ON webits_product_has_kenmerken.product_id = fws_product.ID 
WHERE fws_product.CATID = 11 
AND ( 
(webits_product_has_kenmerken.kenmerk_id = 8 AND webits_product_has_kenmerken.kenmerk_value = 'Buddha to Buddha') 
OR
(webits_product_has_kenmerken.kenmerk_id = 19 AND webits_product_has_kenmerken.kenmerk_value = '10 mm')
)

答案 2 :(得分:0)

将这些列替换为NULL值:

fws_product.CATID 
webits_product_has_kenmerken.kenmerk_id
webits_product_has_kenmerken.kenmerk_value 

与NULL的每次比较都将排除reult中的行