4、在資料庫 test1 中進行下列查詢操作,將查詢語句與結果寫入實驗報告。
(1)查詢所有供應商情況,先按城市升序排列,城市相同按供應商名稱降序排列。
select * from s order by city asc,sname desc;
(2)查詢所有零件情況,先按零件名稱升序排列,零件名稱相同按重量降序排列。
select * from p order by pname asc,weight desc;
(3)查詢專案名中含有“廠”的專案情況。
select * from j where jname like concat('%','廠','%');
(4)查詢供應商名稱中第二個字為“方”的供應商情況。
select * from s where sname like concat('_','方','%');
(5)查詢所有零件中的最大、最小、平均重量。
select max(weight),min(weight),avg(weight) from p ;
(6)查詢零件中名為“螺絲刀”的零件的種類數、平均重量。
select count(*),avg(weight) from p where pname='螺絲刀';
(7)查詢供應商 S1 所供應的各種零件的名稱和數量。
select p.pname,sum(qty) from spj join p on spj.pno=p.pno where sno='s1' group by spj.pno,p.pname;
(8)查詢工程 J1 所使用的各種零件的名稱和數量。
select p.pname,sum(qty) from spj join p on spj.pno=p.pno where jno='j1'group by spj.pno, p.pname;
(9)查詢沒有使用紅色螺絲刀的工程名稱。
select jname from j where jno IN (select jno from spj where pno NOT IN (select pno from p where pname='螺絲刀' and color='紅') group by jno);
(10)查詢沒有供應紅色螺絲刀的供應商名稱。
select sname from s where sno in(select sno from spj where pno not in(select pno from p where pname='螺絲刀' and color ='紅') group by sno);
( 11)查詢所用零件數量超過 500 的工程專案號。
select jno from spj group by jno having sum(qty)>500;
( 12)查詢所用零件種類超過 3 種的工程專案名稱。
select jname from j where jno in(select jno from (select jno,count(distinct pno) as sum from spj group by jno)as subquery where sum >3);
( 13)查詢使用了全部零件的工程專案名稱。
select jname from j where jno in(select jno from (select jno,count(distinct pno) as sum from spj group by jno) as subquery where sum=(select count(*) from p));
( 14)查詢至少供應了工程 J1 所使用的全部零件的供應商名稱。
SELECT s.sname FROM spj JOIN p ON spj.pno = p.pno JOIN s ON spj.sno = s.sno WHERE NOT EXISTS ( SELECT * FROM spj WHERE spj.jno = 'j1' AND spj.pno NOT IN ( SELECT pno FROM spj WHERE jno = 'j1' ) AND spj.sno = s.sno )
( 15)查詢供應情況,顯示內容為供應商名稱、零件名、工程名稱、數量。
select s.sname,p.pname,j.jname,spj.qty from spj join j on spj.jno=j.jno join s on spj.sno=s.sno join p on spj.pno =p.pno;
( 16)查詢“東方紅”供應商供應情況,顯示供應的零件名、工程名稱、數量。
select p.pname,j.jname,spj.qty from spj join j on spj.jno=j.jno join s on spj.sno=s.sno join p on spj.pno =p.pno where sname='東方紅';