例如下面几行代码就完成了数据库和数据表的创建并插入7行数据:
create database test
use test
create table orderproduct(orderid char(11) primary key,productid varchar(255))
insert into orderproduct values
(20161116001,'D0020'),
(20161116035,'E0055'),
(20161101048,'A0035'),
(20161005321,'B0049'),
(20160901515,'C0038'),
(20160814525,'C0038,A0035,E0055'),
(20160714510,'D0020,B0049')
-- 1.找出包含song(歌曲)‘Alison’的专辑title(名称)和artist(作者)SELECT a.title,a.artist FROM album a,track t WHERE a.albumCode = t.album AND t.song = 'Alison'
-- 2.那个artist(作者)录制了送(歌曲)‘Exodus’
SELECT a.artist FROM album a,track t WHERE a.albumCode = t.album AND t.song = 'Exodus'
-- 3.列出track(曲目表)中所有的‘Blur’ album(专辑)的song(歌曲)
SELECT t.song FROM album a,track t WHERE a.albumCode = t.album AND a.title = 'Blur'
-- 4.显示每张album(专辑)的title(名称)和包含的track曲目数
SELECT a.title,COUNT(t.dsk) track_num FROM album a,track t WHERE a.albumCode = t.album
GROUP BY a.title
-- 5.显示每张album(专辑)的title(民称)以及曲目民称中包含‘Heart’的曲目总数量(如果专辑中如果没有满足条件的曲目,则不显示该专辑)
SELECT a.title,SUM(CASE WHEN t.song LIKE '%Heart%' THEN 1 ELSE 0 END) track_num FROM album a,track t WHERE a.albumCode = t.album
GROUP BY a.title
HAVING (SUM(CASE WHEN t.song LIKE '%Heart%' THEN 1 ELSE 0 END)>0 )
-- 6.“title track”(主打歌曲)就是song(歌曲)和(title)(专辑名)相同的曲目。请找出所有title tracks(主打曲目)
SELECT a.title FROM album a,track t WHERE a.albumCode = t.album AND t.song = a.title
-- 7.同名专辑就是专辑名和artist(作者名)相同的专辑(列如,乐队‘Blur’的专辑'Blur'就是同名专辑)。请列出同名专辑
SELECT a.title FROM album a,track t WHERE a.albumCode = t.album AND a.artist = a.title
--8.找出两张以上专辑出现过的歌曲,同时包含出现的次数
SELECT a.title,t.song,COUNT(DISTINCT t.song) FROM album a,track t WHERE a.albumCode = t.album
HAVING(COUNT(DISTINCT t.song)>2)
-- 9.一张物超所值的专辑就是专辑中每首歌曲的定价低于50pence(便士)的专辑,请找出这些专辑,并显示专辑标题,专辑价格和曲目总数
SELECT a.title,a.price,COUNT(t.dsk) track_num FROM album a,track t WHERE a.albumCode = t.album AND a.price <50
GROUP BY a.title,a.price
-- 10.Wagner‘s Ring cycle的专辑有173track(曲目),Bing Crosby的专辑包含101track(曲目)。请根据专辑中包含曲目的多少,
-- 从多到少一次列出这些专辑的名称和包含的总曲目数目。
SELECT a.title,a.price,COUNT(t.dsk) track_num FROM album a,track t WHERE a.albumCode = t.album AND a.artist IN('Wagner''s Ring cycle','Bing Crosby')
ORDER BY track_num DESC
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)