Learning Mysql



Download 4.24 Mb.
View original pdf
Page127/366
Date04.08.2023
Size4.24 Mb.
#61806
1   ...   123   124   125   126   127   128   129   130   ...   366
Learning MySQL
142 | Chapter 5:
Basic SQL

value, or a string that is a prefix of another. Almost all our examples in this and later chapters contain
WHERE
clauses, and you’ll become very familiar with them.
The simplest
WHERE
clause is one that exactly matches a value. Consider an example where we want to find out the details of the artist with the name New Order Here’s what you type:
mysql> SELECT * FROM artist WHERE artist_name = "New Order";
+-----------+-------------+
| artist_id | artist_name |
+-----------+-------------+
| 1 | New Order |
+-----------+-------------+
1 row inset sec)
MySQL returns all rows that match our search criteria—in this case, just the one row and all its columns. From this, you can see that the artist New Order has an artist_id of Lets try another exact-match example. Suppose you want to find out the name of the artist with an artist_id value of 4. You type:
mysql> SELECT artist_name FROM artist WHERE artist_id = 4;
+--------------------+
| artist_name |
+--------------------+
| The Rolling Stones |
+--------------------+
1 row inset sec)
In this example, we’ve chosen both a column and a row we’ve included the column name artist_name after the
SELECT
keyword, as well as WHERE artist_id = If a value matches more than one row, the results will contain all matches. Suppose we ask for the names of all tracks with a track_id of 13; this retrieves the thirteenth song on every album that has at least that many songs. You type in:
mysql> SELECT track_name FROM track WHERE track_id = 13;
+------------------------------------------+
| track_name |
+------------------------------------------+
| Every Little Counts |
| Everyone Everywhere |
| Turn My Way Olympia, Liverpool 18/7/01] |
| Let It Loose |
+------------------------------------------+
4 rows inset sec)
The results show the names of the thirteenth track of different albums, so there must be 4 albums that contain at least 13 tracks If we could join the information we get from the track table with information we get from the album table, we could display the names of these albums. We’ll see how to perform this type of query later in “Joining
Two Tables.”

Download 4.24 Mb.

Share with your friends:
1   ...   123   124   125   126   127   128   129   130   ...   366




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

    Main page