注意:查询语句关键字英文字母大小写不敏感,但是表名,字段名英文字母大小写敏感
注意:SQL语句中所用到的表名和字段名不要无中生有,一定要现创建好表和定义好表中的字段
查询所有记录
-- select * from <表名>
-- 举例
select * from users
查询所有记录中的某一个或某几个字段(字段名之间用英文逗号隔开)
-- select <字段名1>,<字段名2> from <表名>
-- 举例
-- 查询所有记录中的 username 字段
select username from users;
-- 查询所有记录中 username 和 password 字段
select username,password from users;
where 子语句查询
-- 查询 id 等于 8 的记录
select * from users where id = 8;
-- 查询 id 大于 8 的记录
select * from users where id > 8;
-- 查询 id 小于 8 的记录
select * from users where id < 8;
-- 查询 id 不等于 8 的记录
select * from users where id <> 8;
-- 或者
select * from users where id != 8;
结合 where 子语句查询某个字段
-- 查询 id 为 8 的记录的username,password两个字段
select username,password from users where id = 8
更新一条记录
-- update <表名> set <字段名> = <值> where id = 8
-- 举例
update users set username = 'jack' where id = 8;
-- update <表名> set <字段名1> = <值1>, <字段名2> = <值2> where id = 9
update users set username = 'Tom', password = '111' where id = 9
插入一条记录
-- insert into <表名> (<字段名1>,<字段名2>) values (<值1>,<值2>)
-- 举例
insert into users (username,password) values ('江疏影','syhxsqq')
删除一条记录
-- detele from <表名> where <字段名> = <值>
-- 举例
delete from users where id = 8
and 和 or 查询
-- 查询 id 大于 8 并且 status 等于 0 的记录
select * from users where id > 8 and status = 0;
-- 查询 username 等于 'zs' 或者 status 等于 1 的记录
select * from users where username = 'zs' or status = 1;
查询并且排序
-- asc 升序 desc 降序
-- id 升序
select * from users order by id asc;
-- id 升序 status 降序
select * from users order by id asc, status desc;
查询统计
-- 统计 status 等于 0 的记录的总数
select count(*) from users where status = 0
别名
select count(*) as total from users where status = 0;
select username as name from users
查询技巧
-- 查询最后一条数据
select * from users order by id desc limit 1;
-- 查询第一条数据
select * from users limit 1;
-- 查询前两条数据
select * from users limit 0,2;
-- 查询第1,2条数据
select * from users limit 1,2;