Within MySQL-- joint statement Exercise

 

Track TABLE DROP; 
DROP TABLE Album; 
the CREATE TABLE Album ( 
albumCode VARCHAR (10) the NOT NULL PRIMARY KEY - album number 
, title VARCHAR (50) - album title 
, artist VARCHAR (50) - author 
, price DECIMAL (10, 2) - price 
, rdate DATETIME - release date 
, label VARCHAR (50) - record companies 
, rank INTEGER - album rankings 
) the DEFAULT CHARSET = utf8; 

the CREATE TABLE Track ( 
album VARCHAR (10) the NOT NULL - album No 
, dsk INTEGER NOT NULL - the disc number 
, posn INTEGER NOT NULL - song disc position 
, song VARCHAR (255) - the name of the song 
, PRIMARY KEY (Album, DSK, posn) 
, a FOREIGN KEY (Album) the REFERENCES Album (albumCode) 
) the DEFAULT the CHARSET = UTF8; 

the SELECT * the FROM Album; 
the SELECT * Track the FROM;

- Find the album title (name) and artist (author) contains song (song) 'Alison' the 
SELECT a.title, a.artist FROM album a, track t WHERE a.albumCode = t.album AND t.song = 'Alison' 

- which artist (author) recorded the song (the song) 'Exodus' 
the SELECT Artist Album the FROM a, the WHERE a.albumCode track t = t.album the AND t.song = 'Exodus' 

- lists the track (tracks table) all belong to 'Blur' album (album) in the song (song) 
the SELECT Track song the FROM T, the WHERE a album title = 'Blur' = t.album the AND a.albumCode 

- displays each album (album) in the title (name) and the track number of the track comprising 
the SELECT a.title, COUNT (Song) the FROM track T, the WHERE a.albumCode album a = t.album the GROUP BY a.title 

- displays each album (album) in the title (name) and track names contained in the 'Heart' the total number of tracks 
SELECT a.title, COUNT (t.song) FROM album a LEFT JOIN track t ON a.albumCode = t.album WHERE t.song LIKE '% Heart' GROUP BY a .album code

- find all the song (the song) and title (album name) the same track 
the SELECT t.song the FROM t Track, Album A = t.album the WHERE a.albumCode the AND a.title = t.song 

- find all the album name (title) and artist (author) same album 
the SELECT a.title the FROM t Track, album a = a.artist the WHERE a.title 

- find appear in two more songs off the album, contains both appear the number of 
the SELECT t.song, COUNT (a, albumCode) Track C the FROM T = the JOIN Album a t.album the ON a.albumCode the GROUP BY t.song the HAVING C> 2 

- find the price per song is less than 0.5 The album, display album title, price, and total number of tracks 
the SELECT a.title, a.price, COUNT (t.song) c t the FROM track, album A = t.album the WHERE a.albumCode 
the GROUP BY t.song the HAVING a.price / C <0.5 

- how many tracks by album contains from more to less lists album name (title), and comprising a total number of tracks 
SELECT title, COUNT (t.song) c fROM track t, album a WHERE a.albumCode = t.album BY a.albumCode the GROUP 
the ORDER BY DESC c

  

 

Guess you like

Origin www.cnblogs.com/xieshilin/p/11780209.html