单表数据一
create database test9;
1.在test9中创建一张表 user (整形id、字符串(50)username、字符串(32)password)
use test9;
create table `user`(
id int primary key auto_increment , #主键 + 自动增长
username varchar(50),
`password` varchar(32)
);
2.向user表中插入3条数据
insert into `user`(username,`password`) values('jack','111');
insert into `user`(username,`password`) values('rose','222');
insert into `user`(username,`password`) values('tom','333');
3.查询user表中的所有数据
select * from `user`;
4.更新user表的第二条数据的密码为666666
update `user` set `password` = '666666' where id = 2;
5.删除user表的第一条数据
delete from `user` where id = 1;
6.计算user表中的总条数
select count(*) from `user`;
单表数据二
create table product(
pid int primary key,
pname varchar(20),
price double,
category_id varchar(32)
);
INSERT INTO product(pid,pname,price,category_id) VALUES(1,'联想',5000,'c001');
INSERT INTO product(pid,pname,price,category_id) VALUES(2,'海尔',3000,'c001');
INSERT INTO product(pid,pname,price,category_id) VALUES(3,'雷神',5000,'c001');
INSERT INTO product(pid,pname,price,category_id) VALUES(4,'JACK JONES',800,'c002');
INSERT INTO product(pid,pname,price,category_id) VALUES(5,'真维斯',200,'c002');
INSERT INTO product(pid,pname,price,category_id) VALUES(6,'花花公子',440,'c002');
INSERT INTO product(pid,pname,price,category_id) VALUES(7,'劲霸',2000,'c002');
INSERT INTO product(pid,pname,price,category_id) VALUES(8,'香奈儿',800,'c003');
INSERT INTO product(pid,pname,price,category_id) VALUES(9,'相宜本草',200,'c003');
INSERT INTO product(pid,pname,price,category_id) VALUES(10,'面霸',5,'c003');
INSERT INTO product(pid,pname,price,category_id) VALUES(11,'好想你枣',56,'c004');
INSERT INTO product(pid,pname,price,category_id) VALUES(12,'香飘飘奶茶',1,'c005');
INSERT INTO product(pid,pname,price,category_id) VALUES(13,'果9',1,NULL);
查询所有商品
SELECT * FROM product ;
查询商品名和商品价格
SELECT pname,price FROM product;
别名查询,使用as 关键字
SELECT pname pn ,price pr FROM product;
去掉重复值
SELECT DISTINCT price FROM product
查询结果是表达式(运算查询):将所有商品价格+10进行显示
SELECT pid,pname,price+10,category_id FROM product ;
查询商品名称为“花花公子”的商品所有信息:
SELECT * FROM product WHERE pname='花花公子'
查询价格为800商品
SELECT * FROM product WHERE price='800'
查询价格不是800的所有商品
SELECT * FROM product WHERE price !='800'
查询商品价格大于60元的所有商品信息
SELECT * FROM product WHERE price>60
查询商品价格在200到1000之间所有商品
SELECT * FROM product WHERE price>='200' AND price<='1000'
查询商品价格是200或800的所有商品
SELECT * FROM product WHERE price='200' OR price='800'
查询含有'霸'字的所有商品
SELECT * FROM product WHERE pname LIKE '%霸%'
查询以'香'开头的所有商品
SELECT * FROM product WHERE pname LIKE '%香%%'
查询第二个字为'想'的所有商品
SELECT * FROM product WHERE pname LIKE '%%想%'
商品没有分类的商品
SELECT * FROM product WHERE category_id IS NULL
查询有分类的商品
SELECT * FROM product WHERE category_id IS NOT NULL