Learning Mysql


-> (SELECT * FROM played WHERE track.artist_id = played.artist_id AND



Download 4.24 Mb.
View original pdf
Page265/366
Date04.08.2023
Size4.24 Mb.
#61806
1   ...   261   262   263   264   265   266   267   268   ...   366
Learning MySQL
-> (SELECT * FROM played WHERE track.artist_id = played.artist_id AND
-> track.album_id = played.album_id AND
-> track.track_id = played.track_id);
Query OK, 142 rows affected (0.01 sec)
Updates and Deletes with Multiple Tables | 289

You can see that the subquery remains the same, but the outer
SELECT
query is replaced by a
DELETE
statement. The
DELETE
statement syntax is as follows first, the keyword
DELETE
is followed by the table or tables from which rows should be removed second,
the keyword
FROM
is followed by the table or tables that should be queried to determine which rows to delete and, last, a
WHERE
clause (and any other query clauses, such as
GROUP BY
or HAVING) follow. In this query, rows are deleted from the track table using the track table in the query along with the played table in the nested subquery.
As another example, let’s cleanup our database to remove albums and tracks by the band New Order:
mysql> DELETE FROM track, album USING artist, album, track WHERE
-> artist_name = "New Order" AND
-> artist.artist_id = album.artist_id AND
-> artist.artist_id = track.artist_id AND
-> album.album_id = track.album_id;
Query OK, 93 rows affected (0.00 sec)
This query deletes rows from track and album, based on a query that involves artist
,
album
, and track. You can seethe result is that 93 rows are removed 7 albums and 86
tracks.
In this syntax, the keywords DELETE FROM
are followed by the table or tables from which you want to delete rows. The keyword
USING
then follows with a list of tables that are used in the query part of the statement (and then the
WHERE
clause or other associated query mechanisms).
With MySQL versions between 4.0 and 4.02, you had to use the following syntax:
mysql> DELETE track, album FROM artist, album, track WHERE

Download 4.24 Mb.

Share with your friends:
1   ...   261   262   263   264   265   266   267   268   ...   366




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

    Main page