简体   繁体   中英

Two Select query help needed

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');

This Query will return a Node Id..

And this query also returns a Node id.

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

is that possible to check within the MySQL query whether both return node id are equal..?

There is nothing in MYSQL to check if something is equal in a query (results being returned from different queries)
Your requirement is a business logic. So you take the return value of first query and return value of second query and compare them in your code !

Well, you could try something like this:

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

If you do not get a result from this query, the nids dont match.

How about using a FULL OUTER JOIN to compare the results?

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

The WHERE from_recipe + from_taxonomy = 1 will return rows that are in one query only. Use = 2 to see the other half or leave it off entirely to see which is which.

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM