sql
SQL是查询化查询语言,专门用来访问和处理数据库的编程语言,能够让我们以编程的形式,操作数据库里面的数据
sql的关键点
1.sql是一门数据库编程语言
2.使用sql语言编写出来的代码,叫做sql语句
3.sql语言只能在关系型数据库中使用,非关系型数据库不支持sql语言
sql的基础语句
1-- 通过* 把users表中 所有数据查询出来
select * from users;
2-- 从user表中 把username 和 password 对应的数据查询出来
select nusername,password from users
3-- 向 users表中, 插入一条 username 为tony stark ,password为123456 的用户数据
insert into users(username ,password) values ('tony stark' ,'123456');
select *from users
4-- 将 id 为1 的密码 更改为888888
update users set password = '888888' where id=1
select * from users
5-- 更新 id为2的用户,把密码改成666666 状态修改成1
update users set password = '666666' , status=1 where id=2
select * from users
6-- 删除 users 表中 id 为 4 的用户
delete from users where id=4 删除单个
delete from users where id in (多个删除的)
select * from users
7-- 查询where 子句
--查询语句中的 where
select from users where 列 运算符 值
--更新语句中的 where
update 表名称 set 列=新值 where 列 运算符 值
--删除语句中的where
delect from 表名称 where 列 运算符 值
8-- and 和 or 运算符 (JS & | 与 或)
--使用 and 来显示所有状态为0且小于3的用户 必须满足两个条件
select * from users whrer status=0 and id<3
--使用 or 来显示所有状态为1 或 username 为 zs 的用户 满足任意一个
select * from users where status=1 or username=‘zs’
9-- order 和 by 子句 asc(升序) desc(降序)
-- 升序 asc 默认就是升序 可以不写(asc)
select * from users order by id asc
-- 降序 desc 表示降序排序
select * from users order by id desc
-- 多重排序 先按照 status 进行降序排序 在按照username 进行升序
select * from users order by status desc, username asc
10--count(*)函数用于返回查询结果的总数据条款
-- 查询数据中满足 条件的结果 并用条数返回
select count(*) from users status=0
11--给 查询出来的列名设置别名 as
-- 查询数据中满足 条件的结果 用 as 设置的别名显示
select count(*)as tito from users status=0
--多个列名设置 (as) 别名
select username as uname , password as upwd form users
12-- 查询分页
select * from 表(名称) limit (当前页码值 - 1) * 页数(每页显示的数量)
13-- 多表查询
a(自命名) b(自命名)
select * from 表(1) a left join 表(2) b on '表(1)' = '表(2)' limit (当前页码值 - 1) * 页数(每页显示的数量)