数据库查询
建表的时候出现的情况
两个对象,他们之间存在某种关系,这种关系一共有以下四种:
- 一对一
- 一对多
- 多对一
- 多对多
那么如何分表呢?分表遵循一个原则,经常需要修改的放在一个表,不需要经常修改的放一起。
对于表与表之间的关系,可以在表里面写上其他表的id,像这样
user(id,password_id, public_id, private_id);
如果不想这样子也可以直接创建一个关系表,将表与表之间的对应关系以id的方式写进去,这样子使用更加方便。
数据库查询
查询所有
-
查询所有数据库 SHOW DATABASES
-
查询当前数据库的所有表 SHOW TABLES
-
查询当前表的所有数据 SELECT * FROM 表名
运算符
算术运算符 + - * /
SELECT , 列名1+列名2 temp from 表名
在某个表里面查询两列,并且将结果相加,存为temp列
别名 AS,或者省略AS
SELECT , 列名1+列名2 as temp from 表名
和上面的区别在于使用了as,不使用也可以,将查询结果存为temp,而不是原来的列名1+列名2
关系运算符 < > = >= <= ==
查询id大于多少的数据
-
select *,from product where id > 50;
在product表里查询id大于50的值
逻辑运算符 and or
-
select * from product where id > 50 and id < 70;
在product表里查询id大于50并且小于70的值
数学相关的函数
max()
select max(price) from product;
查找product表中,price列的最大值
min()
select max(price) from product;
查找product表中,price列的最小值
avg()
select max(price) from product;
查找product表中,price列的平均值
sum()
select max(price) from product;
查找product表中,price列的和
日期格式化
select date_format(update_time,‘%y/%m/%d’) from product;
-
将update_time的时间格式置为 年/月/日 形式
y是年份末两位,Y是完整年份,h是12小时,H是24小时,/是分隔符,喜欢用什么就用什么,要加上小时这些也一样
分页 limit
分页公式
select * from product limit (page-1) size,size;
-
page是第几页,size是每一页显示多少条
-
limit不支持运算符,只能写结果
-
分页的代码写在SQL语句最后面
总条数 count
select count(*) as count from product;
-
括号里是*,默认找索引的总条数
-
如果里面是列名,则只会统计该列非null的个数
-
是1结果与*一样
条件查询 where
between and
select * from product where id between 50 and 70;
相当于
select * from product where id > 50 and id < 70;
查找product表中id大于50小于70的数据
not in
不推荐使用,因为不过有没有,在什么位置,有没有找到,都会遍历整个数据库
- select * from product where id in (7,3);
- select * from product where id not in (7,3);
like 模糊查询
select * from product where name like ‘%肉%’;
- 查询product表中,name中包含有肉的所有数据
- %是通配符,可以有内容也可以没有
SQL模糊查询可以使用正则表达式
order by 排序
-
select * from product order by id desc; 以id降序
-
select * from product order by id asc; 以id升序
默认情况下也是升序
group by 分组
分组就是将相同的数据分成一个组的意思
select price from product group by price
select price from product group by price,name
这样子分组也是可以的,表示price和name只要同时不相同那就是一个新的组,不过只显示price
select price,name from product group by price,name;
这样写的话price和name都会返回
group by后面的列个数,必须大于等于select后面的列个数,且select后面的列必须在group by后面有
多表查询
内连接
内连接用在两个表有联系的时候,如果不使用内连接
select * from product,cuisine
where product.cuisine_id = cuisine.id;
在product表和cuisine表里面查询product表里面的cuisine_id与cuisine表里的id相等的数据
有的时候表名比较很长,书写不方便,可以取别名
但是,如果要查询的表太多了,这样子就很不方便,需要用内连接
select * from product p
inner join cuisine c
on p.cuisine_id = c.id;
用内连接的好处就是如果还要加表直接再加inner join on就好,一行可以不用写一连串
左连接
在用内连接的时候会发现,如果product表的数据中有数据没有product.cuisine_id,那么不会输出,那么我们想要没有product.cuisine_id的数据也能输出就需要用左连接。
以左边的表为基础,不仅输出满足条件的数据,不满足的也会输出
select * from product p
left join cuisine c
on p.cuisine_id = c.id;
如果查询到的数据中,product表中的数据没有cuisine_id,会用null占位
右连接
与左连接原理一样,只不过是以右边的表为基础
合并查询 union all
有的页面会出现上一条消息,下一条消息这种情况,实际上这个页面查找到了三条数据
要达到这个目的,首先需要像这样下面这样实现查找到我们随便选择的一条,以及它的上一条和下一条
select * from product where id = (
select * max(id) product where id < 66);
select * from product where id = 66;
select * from product where id = (
select * min(id) product where id > 66);
然后使用union all合并
select * from product where id = (
select * max(id) product where id < 66);
union all
select * from product where id = 66;
union all
select * from product where id = (
select * min(id) product where id > 66);
union all
union all只要列名一致就都可以合并
如果列名不一样,最后得到的结果会以第一个出现的列名为准
多表查询的时候如果有要做子连接,必须要声明清楚以那一个表为基础,查询出来的结果要取别名
select * from
(select p.*,c.name cuisineName from product p
left join cuisine c
on p.cuisine_id=c.id)aa
where aa.enable=1;
查询的执行顺序
如果查询的时候与逻辑运算符一起用,那么一旦出现or记得用括号防止出现SQL注入
select-》from-》where-》group by-》having-》order by-》limit
having可以理解为是对group by的where子查询条件,例如:
-
select enable from product where enable=1 group by enable;
-
这样子做事先找到enable=1的,然后再分组
-
select enable from product group by enable having enable=1;
-
这样子做了之后,就是先分组,然后再查找enable为1的