JAVA--数据库查询

本文详细介绍了SQL的基础查询语法,包括全表查询、使用WHERE子句指定条件、模糊查询、分页查询、排序、聚合函数(COUNT、SUM、AVG、MIN、MAX)、分组查询、HAVING条件、子查询、内连接和外连接等。
摘要由CSDN通过智能技术生成

查询 select

基本查询

查询整张表

select * from 表名

用where表示查询的条件

查询在user表中年龄大于6岁的用户,显示用户名,手机号,密码
select name,phone,password from user where age > 6


查询在user表中年龄大于10岁并且密码为999的用户,显示id,用户名,年龄,手机号,密码
select id,name,age,phone,password from user where age >10 and password = 999


查询在user表中年龄大于30岁或者密码为777的用户,显示id,用户名,年龄,手机号,密码
select id,name,age,phone,password from user where age >30 or password = 777


查询在user表中id为23到35之间的用户,显示id,用户名,年龄,手机号,密码
select id,name,age,phone,password from user where id between 23 and 35

模糊查询

左模糊

(一般不使用左模糊,%放在左边,导致索引失效,降低查询效率)

查询user表中用户名第二个字符为x的用户,显示id,用户名

select id,name from user where name like "%x"

右模糊

查询user表中,第一个字为张的所有用户,显示id,用户名,年龄,手机号,密码

select id,name,age,phone,password from user where name like "张%"

 _代表匹配一个字符

select id,name ,age,password from user where password like "_23"

select id,name,age,phone,password from user where name like "m__"

分页查询

关键字 limit


分页查询第m页,每页展示n条数据 limit(m-1)*n,n
查询在user表中,分页查询第0页,每页显示3条数据,显示id,用户名,年龄,手机号,密码

select id,name,age,phone,password from user limit 0,3

查询在user表中,分页查询第6页,每页显示10条数据,显示id,用户名,年龄,手机号,密码


select id,name,age,phone,password from user limit 6,10 

排序

ASC 升序

查询user表中,将年龄进行升序

select id,name,age,phone,password from USER order by age ASC

DESC降序

查询user表中,将年龄进行降序

SELECT ID,NAME,AGE,PHONE FROM USER ORDER BY AGE DESC

聚合函数

count() 查询有多少条数据

查询user表中有多少条数据

select count(0) from user

 sum()总和

查询user表中的年龄的总和

select sum(age) from user

avg()平均值

查询user表中的年龄的平均值

select avg(age) from user

min() 最小值

查询user表中的年龄的最小值


select min(age) from user


max() 最大值

查询user表中的年龄的最大值


select max(age) from user

分组查询  

group by 字段

查询user表中,根据性别分组,查询出男女各有多少条数据,以及男女年龄的平均值

select sex,count(0),avg(age) from user group by sex

having条件查询,where分组前,having分组后的条件

查询user表中,根据性别查询出年龄大于8并且年龄的平均值大于20的


select sex,count(0),avg(age) from user where age > 8 group by sex 
having avg(age) > 20

子查询

将括号内查询的结果当作条件进行再次查询

select id,name,age from user where jobid in (select id from job where 
jobname = "卖衣服")

内连接


笛卡尔积 查询出来的结果没有意义

select * from user,job

显示内连接   inner可以省略

可以给表名起别名,如果两个表中有相同的字段名称,那么需要用别名+点号来区别,否则报错
u为user表的别名,j为job表的别名

select u.id,u.name from user u inner join job j on u.jobid = j.id

select u.id,u.name,u.age,j.id,j.jobname from user u join job j on u.jobid = j.id

 隐式内连接

select * from user u,job j where u.jobid = j.id

外连接

左外连接  

left [outer] join on (on是条件)

主表放左,从表放右

查询的是左边表的所有部分和右边表的交集

select * from user u left outer join job j on u.jobid = j.id 

select * from user u left join job j on u.jobid = j.id 

 

右外连接

 rigth [outer] join on

主表放右,从表放左

查询的是右边表的所有部分和左边表的交集


select * from job j right outer join user u on u.jobid = j.id

  • 17
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值