常用的MySQL查询语句

注意:查询语句关键字英文字母大小写不敏感,但是表名,字段名英文字母大小写敏感

 注意: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;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值