Mysql
 sql >> डेटाबेस >  >> RDS >> Mysql

कुछ को छोड़कर सभी का चयन करने के लिए mysql क्वेरी

इस उदाहरण पर विचार करें...

 SELECT * FROM recipes;
 +-----------+---------------------------------+
 | recipe_id | recipe                          |
 +-----------+---------------------------------+
 |         6 | Beans & Macaroni                |
 |         9 | Beans on Jacket Potato          |
 |         3 | Beans on Toast                  |
 |        10 | Cheese & Beans on Jacket Potato |
 |         4 | Cheese & Beans on Toast         |
 |         2 | Cheese on Toast                 |
 |         1 | Macaroni & Cheese               |
 |        12 | Peanut Butter on Toast          |
 |         5 | Toast & Jam                     |
 +-----------+---------------------------------+

 SELECT * FROM recipe_ingredient;
 +-----------+---------------+
 | recipe_id | ingredient_id |
 +-----------+---------------+
 |         1 |             1 |
 |         1 |             2 |
 |         2 |             2 |
 |         2 |             4 |
 |         3 |             3 |
 |         3 |             4 |
 |         4 |             2 |
 |         4 |             3 |
 |         4 |             4 |
 |         5 |             4 |
 |         5 |             5 |
 |         6 |             1 |
 |         6 |             3 |
 |         9 |             3 |
 |         9 |             6 |
 |        10 |             2 |
 |        10 |             3 |
 |        10 |             6 |
 |        12 |             4 |
 |        12 |             7 |
 +-----------+---------------+

 SELECT * FROM ingredients;
 +---------------+---------------+
 | ingredient_id | ingredient    |
 +---------------+---------------+
 |             3 | Beans         |
 |             2 | Cheese        |
 |             6 | Jacket Potato |
 |             5 | Jam           |
 |             1 | Macaroni      |
 |             7 | Peanut Butter |
 |             4 | Toast         |
 +---------------+---------------+

निम्नलिखित सभी व्यंजनों और उन्हें बनाने के लिए आवश्यक सामग्री की सूची देता है...

 SELECT r.*
      , i.*
   FROM recipes r
   JOIN recipe_ingredient ri
     ON ri.recipe_id = r.recipe_id
   JOIN ingredients i
     ON i.ingredient_id = ri.ingredient_id;
 +-----------+---------------------------------+---------------+---------------+
 | recipe_id | recipe                          | ingredient_id | ingredient    |
 +-----------+---------------------------------+---------------+---------------+
 |         6 | Beans & Macaroni                |             1 | Macaroni      |
 |         6 | Beans & Macaroni                |             3 | Beans         |
 |         9 | Beans on Jacket Potato          |             3 | Beans         |
 |         9 | Beans on Jacket Potato          |             6 | Jacket Potato |
 |         3 | Beans on Toast                  |             3 | Beans         |
 |         3 | Beans on Toast                  |             4 | Toast         |
 |        10 | Cheese & Beans on Jacket Potato |             2 | Cheese        |
 |        10 | Cheese & Beans on Jacket Potato |             3 | Beans         |
 |        10 | Cheese & Beans on Jacket Potato |             6 | Jacket Potato |
 |         4 | Cheese & Beans on Toast         |             2 | Cheese        |
 |         4 | Cheese & Beans on Toast         |             3 | Beans         |
 |         4 | Cheese & Beans on Toast         |             4 | Toast         |
 |         2 | Cheese on Toast                 |             2 | Cheese        |
 |         2 | Cheese on Toast                 |             4 | Toast         |
 |         1 | Macaroni & Cheese               |             1 | Macaroni      |
 |         1 | Macaroni & Cheese               |             2 | Cheese        |
 |        12 | Peanut Butter on Toast          |             4 | Toast         |
 |        12 | Peanut Butter on Toast          |             7 | Peanut Butter |
 |         5 | Toast & Jam                     |             4 | Toast         |
 |         5 | Toast & Jam                     |             5 | Jam           |
 +-----------+---------------------------------+---------------+---------------+

अब मान लेते हैं कि हमारे पास पनीर, बीन्स और टोस्ट वाली एक पेंट्री है। केवल उन्हीं सामग्रियों का उपयोग करके हम क्या बना सकते हैं?

 SELECT r.*
      , SUM(CASE WHEN ingredient IN ('Cheese','Beans','Toast') THEN 1 ELSE 0 END) x
      , COUNT(*) y
   FROM recipes r
   JOIN recipe_ingredient ri ON ri.recipe_id = r.recipe_id
   JOIN ingredients i ON i.ingredient_id = ri.ingredient_id
  GROUP
     BY r.recipe_id;
 +-----------+---------------------------------+------+---+
 | recipe_id | recipe                          | x    | y |
 +-----------+---------------------------------+------+---+
 |         1 | Macaroni & Cheese               |    1 | 2 |
 |         2 | Cheese on Toast                 |    2 | 2 | <-- 
 |         3 | Beans on Toast                  |    2 | 2 | <--
 |         4 | Cheese & Beans on Toast         |    3 | 3 | <-- *
 |         5 | Toast & Jam                     |    1 | 2 |
 |         6 | Beans & Macaroni                |    1 | 2 |
 |         9 | Beans on Jacket Potato          |    1 | 2 |
 |        10 | Cheese & Beans on Jacket Potato |    2 | 3 |
 |        12 | Peanut Butter on Toast          |    1 | 2 |
 +-----------+---------------------------------+------+---+

 x = y : recipes use only those ingredients found in the pantry.

 x = y = total no of ingredients in pantry : recipes using EXACTLY the ingredients found in the pantry

इसे इस प्रकार फिर से लिखा जा सकता है...

 SELECT r.*
   FROM recipes r
   JOIN recipe_ingredient ri ON ri.recipe_id = r.recipe_id
   JOIN ingredients i ON i.ingredient_id = ri.ingredient_id
  GROUP
     BY r.recipe_id
 HAVING  SUM(CASE WHEN ingredient IN ('Cheese','Beans','Toast') THEN 1 ELSE 0 END) = COUNT(*);


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. रेल पर UTF8 MySQL समस्याएँ - utf8_general_ci के साथ एन्कोडिंग समस्याएँ

  2. MySQL कार्यक्षेत्र में संबंध कैसे बनाएं

  3. क्या मैं MySQL के अंतर्गत सीधे SQL में HTTP GET चला सकता हूँ?

  4. जावा और एसक्यूएल:वापसी शून्य या अपवाद फेंक दें?

  5. Django रॉ () क्वेरी, परिकलित फ़ील्ड WHERE क्लॉज़ में