SQL语句的基本使用

433 阅读1分钟

下面我给大家介绍一下SQL语句实现增删改查功能的使用语法:

假设有一张表,名字为users,这张表分为id,username,password,status四列,如下:

image.png

  1. 通过*将users中所有的列的数据查询出来 select * from users

  2. 查询部分列数据alter select username,password from users

  3. 向users表中插入新数据 insert into users(username,password) values ('tony stark','098123')

4.更新表中的数据,例如: 将id为4的用户密码改为888888

update users set password=888888 where id=4

同时更新多个属性值时:

update users set password='admin123',status=1 where id=2

5.删除第四行数据

delete from users where id=4

特别注意:更新某一行中的若干列,要记得加上where,否则,整张表的password和status都会被修改

6.演示where子句的使用

select * from users where status=1

select * from users where id>=2

  1. <>和!=都表示不等于,例:

select * from users where username<>'ls'

select * from users where username!='ls'

  1. and运算符 select * from users where status!=1 and id<3

  2. or 语句的使用

select * from users where status=1 or username='zs'

  1. 对users表中的数据进行status升序排序,asc(默认)表示升序

select * from users order by status

  1. 根据id进行降序的排序desc表示降序

select * from users order by id desc

12 多重排序

select * from users order by status desc,username asc

  1. 使用count(*)来统计users表中用户的总数量

select count(*) from users where status=0

14.使用as关键字为查询出来的列设置别名

select count(*) as total from users where status=0

使用as关键字为查询出来的多个列设置别名

select username as uname,password as mima from users