针对实验一建立的SPXS数据库中的三个表,实现如下查询: (1)查询与商品“电视”颜色相同的商品名; (2)查询不销售商品P2的商店名; (3)查询至少销售商品P1、P2两种商品的商店名; (4)所有商店都销售的商品号。 (5)只销售一种商品P4的商店名。写SQL语句
时间: 2023-06-23 13:08:21 浏览: 238
(1)查询与商品“电视”颜色相同的商品名:
```
SELECT b.PNAME
FROM SPXS_GOODS a
JOIN SPXS_GOODS b ON a.CLR=b.CLR AND a.PNAME!='电视' AND b.PNAME!='电视';
```
(2)查询不销售商品P2的商店名:
```
SELECT b.SNAME
FROM SPXS_GOODS a
RIGHT JOIN SPXS_STORE b ON a.SNO=b.SNO AND a.PNO!='P2'
WHERE a.PNO IS NULL;
```
(3)查询至少销售商品P1、P2两种商品的商店名:
```
SELECT b.SNAME
FROM SPXS_GOODS a
JOIN SPXS_STORE b ON a.SNO=b.SNO
WHERE a.PNO IN ('P1','P2')
GROUP BY b.SNO
HAVING COUNT(DISTINCT a.PNO)>=2;
```
(4)所有商店都销售的商品号:
```
SELECT a.PNO
FROM SPXS_GOODS a
GROUP BY a.PNO
HAVING COUNT(DISTINCT a.SNO)=(SELECT COUNT(*) FROM SPXS_STORE);
```
(5)只销售一种商品P4的商店名:
```
SELECT b.SNAME
FROM SPXS_GOODS a
JOIN SPXS_STORE b ON a.SNO=b.SNO
WHERE a.PNO='P4'
GROUP BY b.SNO
HAVING COUNT(DISTINCT a.PNO)=1;
```
阅读全文