Learning Mysql


-> (SELECT engineer_name FROM engineer)



Download 4.24 Mb.
View original pdf
Page232/366
Date04.08.2023
Size4.24 Mb.
#61806
1   ...   228   229   230   231   232   233   234   235   ...   366
Learning MySQL
-> (SELECT engineer_name FROM engineer);
+---------------+
| producer_name |
+---------------+
| George Martin |
+---------------+
1 row inset sec)
The
= ANY
causes the outer query to return a producer when the producer_name is equal to any of the engineer names returned by the subquery. The
= ANY
keyphrase has the alias IN, which you’ll see commonly used in nested queries. Using IN, the previous example can be rewritten:
mysql> SELECT producer_name FROM producer WHERE producer_name
-> IN (SELECT engineer_name FROM engineer);
+---------------+
254 | Chapter 7:
Advanced Querying


| producer_name |
+---------------+
| George Martin |
+---------------+
1 row inset sec)
Of course, for this particular example, you could also have used a join query:
mysql> SELECT producer_name FROM producer INNER JOIN engineer
-> ON (producer_name = engineer_name);
+---------------+
| producer_name |
+---------------+
| George Martin |
+---------------+
1 row inset sec)
Again, nested queries are expressive but typically slow in MySQL, souse a join where you can.
Using ALL
Suppose you want to find engineers who are more experienced than all of the producers
—that is, more experienced than the most experienced producer. You can do this with the
ALL
keyword in place of
ANY
:
mysql> SELECT engineer_name, years FROM engineer
-> WHERE years > ALL (SELECT years FROM producer);
Empty set (0.00 sec)
You can see that there are no answers looking at the data, we see that George Martin has been a producer for 40 years, equal to or longer than the time any engineer has been engineering. While the
ANY
keyword returns values that satisfy at least one condition
(Boolean OR, the
ALL
keyword returns values when all the conditions are satisfied
(Boolean AND).
We can use the alias NOT IN
in place of
<> ANY
or !
= ANY. Let's find all the engineers who aren't producers:
mysql> SELECT engineer_name FROM engineer WHERE

Download 4.24 Mb.

Share with your friends:
1   ...   228   229   230   231   232   233   234   235   ...   366




The database is protected by copyright ©ininet.org 2024
send message

    Main page