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 是否相等..?

4

4 回答 4

1

MYSQL 中没有任何内容可以检查查询中的某些内容是否相等(从不同查询返回的结果)
您的要求是业务逻辑。因此,您获取第一个查询的返回值和第二个查询的返回值,并在您的代码中进行比较!

于 2012-05-09T06:04:35.787 回答
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

如果您没有从此查询中得到结果,则 nid 不匹配。

于 2012-05-09T06:07:56.923 回答
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查看另一半或完全关闭以查看哪个是哪个。

于 2012-05-09T06:25:18.600 回答
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')
于 2012-05-09T06:04:39.047 回答