SQL常用语法

查询数据库

show databases;
显示所有数据库
show tables;
显示所有数据库中的表

增删查改

INSERT

INSERT INTO 表名 (字段名1,字段名2,字段名3VALUES ('值1','值2','值3');

UPDATE

UPDATE 表名 SET hospitalname='***', username='***' where id='***';

DELECT

delect from 表名 where  id='***';

SELECT

基本语法

select * from table

排序(order by)

默认不加desc,从小到大
加desc从大到小

select birthday from sys_doctor_info ORDER BY birthday desc

数据分页显示

select birthday from sys_doctor_info ORDER BY birthday desc LIMIT 10 offset 0

从第0条开始显示10条数据,第一页

select birthday from sys_doctor_info ORDER BY birthday desc LIMIT 10 offset 10

从第11条开始显示10条数据,第二页

排序和分页显示组合

select birthday from sys_user_doctor_info ORDER BY birthday desc LIMIT 10 offset 10

去重

select distinct  字段 from table

where

select 字段 from table where a=1
LIKE ‘%字符%’
<> 不等于,< 小于,> 大于,=等于
where price BETWEEN 10 AND 20;
where price IN (10,20,30)
and or not
举例:

select * from user_doctor_info  where idNumber LIKE '%1000%' and gender <> 0
SELECT * from user_doctor_info where username <> ''
两个select组合查询
select * from user_doctor_info  where idNumber IN(SELECT * from table)

where 和order by desc 和limit offset

select birthday from sys_user_doctor_info where gender =2 ORDER BY birthday desc LIMIT 10 offset 10

聚合查询

基本语法:group by 字段 having 条件
函数:count,min,max,sum,avg

SELECT count(*) from user_doctor_info where flagDoctorStatus=1
SELECT min(birthday) from user_doctor_info
SELECT max(birthday) from user_doctor_info
SELECT sum(doctorPower) from user_doctor_info where gender=2 and flagDoctorStatus=1
SELECT AVG(doctorPower) from user_doctor_info where gender=2 and flagDoctorStatus=1
select count(gender),gender from sys_user_doctor_info  GROUP BY gender having gender=1

在这里插入图片描述
group by 和 having in

select count(gender),gender from sys_user_doctor_info  GROUP BY gender having gender in (1,2)

在这里插入图片描述
where和group by having 怎么结合

select count(*),hospitalInfoCode from sys_user_doctor_info where gender=2 GROUP BY hospitalInfoCode HAVING hospitalInfoCode='***' 

在这里插入图片描述

LEFT JOIN ON

左连接:左表中独特的所有数据和两个表的交际
方法1:

select userName,birthday,hospitalName,departmentName from sys_user_doctor_info a LEFT JOIN hospital_info b on a.hospitalInfoCode=b.hospitalInfoCode LEFT JOIN hospital_department c on a.departmentId=c.hospitalDepartmentId

在这里插入图片描述
JOIN ON
内连接,两个表的交际
方法2:

select userName,birthday,hospitalName,departmentName from sys_user_doctor_info a LEFT JOIN (hospital_info b ,hospital_department c)on a.hospitalInfoCode=b.hospitalInfoCode and a.departmentId=c.hospitalDepartmentId
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值