2012-05-09 211 views
0
SELECT r.nid 
FROM recipe_node_ingredient r,recipe_ingredient ri 
WHERE r.`ingredient_id` = ri.id 
AND ri.name = 'carrot' 
AND r.nid NOT IN (SELECT r.nid 
        FROM recipe_node_ingredient r,recipe_ingredient ri 
        WHERE r.`ingredient_id` = ri.id AND ri.name = 'salt'); 

這個查詢將返回一個節點ID ..兩個選擇查詢需要幫助

而這個查詢還返回一個節點ID。

SELECT nid 
FROM taxonomy_index JOIN taxonomy_term_data USING (tid) 
WHERE name IN ('Desert."', 'Indian') 
GROUP BY nid HAVING COUNT(*) > 1 

是否可以在MySQL查詢中檢查兩個返回節點ID是否相等..?

+0

你需要什麼結果.... @ Parthi04 –

回答

1

MYSQL沒有什麼可以檢查查詢中是否有相同的結果(結果從不同的查詢中返回)
您的需求是業務邏輯。因此,您將第一個查詢的返回值和第二個查詢的返回值進行比較,並在您的代碼中進行比較!

0
select if(select r.nid from recipe_node_ingredient r,recipe_ingredient ri where 
r.`ingredient_id` = ri.id and ri.name = 'carrot' and r.nid 
NOT IN (select r.nid from recipe_node_ingredient r,recipe_ingredient ri 
where r.`ingredient_id` = ri.id and ri.name = 'salt') == (SELECT nid FROM taxonomy_index JOIN taxonomy_term_data 
USING (tid) WHERE name IN ('Desert."', 'Indian') 
GROUP BY nid HAVING COUNT(*) > 1),'true','false') 
1

那麼,你可以嘗試這樣的事情:

SELECT * 
FROM (SELECT r.nid 
     FROM recipe_node_ingredient r, 
      recipe_ingredient ri 
     WHERE r.`ingredient_id` = ri.id 
     AND ri.name = 'carrot' 
     AND r.nid NOT IN (SELECT r.nid 
          FROM recipe_node_ingredient r, 
           recipe_ingredient ri 
          WHERE r.`ingredient_id` = ri.id 
          AND ri.name = 'salt')) subSelect1, 
    (SELECT nid 
     FROM taxonomy_index 
     JOIN taxonomy_term_data USING (tid) 
     WHERE name IN ('Desert."','Indian') 
     GROUP BY nid 
     HAVING COUNT(*) > 1) subSelect2 
WHERE subSelect1.nid = subSelect2.nid 

如果你沒有得到從該查詢結果,NIDS不匹配。

+0

感謝您的回覆。此查詢不返回任何結果。但實際結果是Node id = 10。 – Parthi04

1

如何使用FULL OUTER JOIN比較結果?

SELECT 
    nid, 
    IFNULL(from_recipe, 0) from_recipe, 
    IFNULL(from_taxonomy, 0) from_taxonomy 
FROM 
    (
    SELECT r.nid, 1 AS from_recipe 
    FROM  recipe_node_ingredient r 
    WHERE EXISTS (
       SELECT 1 FROM recipe_ingredient WHERE id = r.ingredient_id AND name = 'carrot' 
      ) 
      AND NOT EXISTS (
       SELECT 1 FROM recipe_ingredient WHERE id = r.ingredient_id AND name = 'salt' 
      ) 
) AS recipe 
    FULL OUTER JOIN (
    SELECT ti.nid, 1 AS from_taxonomy 
    FROM  taxonomy_index td 
      INNER JOIN taxonomy_term_data ti ON td.tid = it.tid 
    WHERE td.name IN ('Desert."', 'Indian') 
    GROUP BY ti.nid 
    HAVING COUNT(*) > 1 
) AS taxonomy ON recipe.nid = taxonomy.nid 
WHERE 
    IFNULL(from_recipe, 0) + IFNULL(from_taxonomy, 0) = 1 

WHERE from_recipe + from_taxonomy = 1將返回僅在一個查詢中的行。使用= 2來查看另一半,或者完全關閉以查看哪個是哪個。