时间:2021-07-01 10:21:17 帮助过:3人阅读
select id, username from user1 where (id < 5) OR (id > 90); select id, username from user1 where id between 5 and 90; # [5, 90] select id, username from user1 where id not between 5 and 90; # ![5, 90] select id, username from user1 where id not in (1, 2, 3, 4, 5);
select id, username from user1 where id is not null; select id, username from user1 where id is null;
_: 单个字符 %: 0 个或者多个
select * from user1; | id | username | +-----+----------+ | 0 | test0 | | 1 | test1 | | 2 | test2 | | 3 | test3 | | 4 | test4 | | 5 | test5 | | 6 | test6 | | 7 | test7 | | 8 | test8 | | 9 | test9 | | 100 | test100 | | 111 | test111 |
select * from user1 where id like ‘1__‘;
| id | username | +-----+----------+ | 100 | test100 | | 111 | test111 |
select * from user1 where username like ‘test___‘;
| id | username | +-----+----------+ | 100 | test100 | | 111 | test111 |
select * from user1 where username like ‘test1%‘;
| id | username | +-----+----------+ | 1 | test1 | | 100 | test100 | | 111 | test111 |
select id, username from users order by username; select id, username from users order by username asc, id asc; select id, username from users order by id desc;
默认是 asc (升序), 降序 (DESC)
limit 3 (只显示 3 位数) limit 10, 10 (返回排名 11 到 20) limit 1, 1 (表示第二位)
select id as userId, username as userName from user; select id, username from user; # 两个字段 select id username from user; # 一个字段, username 是 id 的别名 select id as username from user; # 一个字段, 别名
truncate table user1; insert into user1 (id, username) values (1, ‘test‘); insert into user1 (id, username) values (1, ‘test‘); insert into user1 (id, username) values (1, ‘test‘); insert into user1 (id, username) values (1, ‘test‘); select distinct * from user1;