create table manufacturers
(
id integer primary key, -- 商店ID
name varchar(100) not null -- 商店名称
);
create table products
(
id integer primary key, --商品ID
name varchar(100) not null, --商品名称
price decimal not null, --商品价格
manufacturer integer references manufacturers (id) --关联商店
);
insert into manufacturers
values (1, '索尼'),
(2, '华硕'),
(3, '惠普'),
(4, '英特尔'),
(5, '富士通'),
(6, '金士顿');
insert into products
values (1, '硬盘', 240, 5),
(2, '内存', 120, 6),
(3, 'CPU', 150, 4),
(4, 'U盘', 5, 6),
(5, '监控', 240, 1),
(6, 'DVD驱动器', 180, 2),
(7, 'CD驱动器', 90, 2),
(8, '打印机', 270, 3),
(9, '墨粉盒', 66, 3),
(10, 'DVD刻录机', 180, 2);
激光打印机
select name
from products;
select name, price
from products;
select name
from products
where price <= 200;
/* With AND */
select *
from products
where price >= 60
and price <= 120;
/* With BETWEEN */
select *
from products
where price between 60 and 120;
select name, price * 100 as price
from products;
select avg(price)
from products;
select avg(price)
from products
where manufacturer = 2;
select count(*)
from products
where price >= 180;
select name, price
from products
where price >= 180
order by price desc, name;
/* Without INNER JOIN */
select *
from products as p,
manufacturers as m
where p.manufacturer = m.id;
/* With INNER JOIN */
select *
from products as p
inner join manufacturers m on p.manufacturer = m.id;
/* Without INNER JOIN */
select p.name, p.price, m.name
from products as p,
manufacturers as m
where p.manufacturer = m.id;
/* With INNER JOIN */
SELECT p.Name, p.Price, m.Name
FROM products as p
INNER JOIN manufacturers as m
ON p.Manufacturer = m.id;
select avg(price), manufacturer
from products
group by manufacturer;
/* Without INNER JOIN */
select avg(price), m.name
from products as p,
manufacturers as m
where p.manufacturer = m.id
group by m.name
/* With INNER JOIN */
select avg(price), m.name
from products as p
inner join manufacturers m on p.manufacturer = m.id
group by m.name
/* Without INNER JOIN */
select avg(Price), m.name
from products as p,
manufacturers as m
where p.manufacturer = m.id
group by m.name
having avg(Price) >= 150;
/* With INNER JOIN */
SELECT AVG(Price), m.Name
FROM Products as p
INNER JOIN manufacturers as m
ON p.Manufacturer = m.id
GROUP BY m.Name
HAVING AVG(Price) >= 150;
SELECT name, price
FROM Products
ORDER BY price ASC
LIMIT 1;
/* With a nested SELECT */
SELECT Name, Price
FROM Products
WHERE Price = (SELECT MIN(Price) FROM Products);
/* With a nested SELECT and without INNER JOIN */
SELECT A.Name, A.Price, F.Name
FROM Products A,
Manufacturers F
WHERE A.Manufacturer = F.id
AND A.Price =
(
SELECT MAX(A.Price)
FROM Products A
WHERE A.manufacturer = F.id
);
/* With a nested SELECT and an INNER JOIN */
SELECT A.Name, A.Price, F.Name
FROM Products A
INNER JOIN manufacturers F
ON A.Manufacturer = F.id
AND A.Price =
(
SELECT MAX(A.Price)
FROM Products A
WHERE A.Manufacturer = F.id
);
insert into products
values (11, '音箱', 70, 2);
激光打印机
update products
set name='激光打印机'
where id = 8;
UPDATE Products
SET Price = Price - (Price * 0.1);
update products
set price=price - (price * 0.1)
where price >= 120;