数据库查询的相关基础知识

 

1.算数运算符的使用

select * from 表名 where (normal_price + price) > 50;

2.给一系列数据命名,取一个另外的别名。

select normal_price + price as 别名 from product;

3.逻辑运算符

&&和 and

select * from product where price < 100 && price >50;

select * from product where price <100 and price >50;

||和or

select * from product where price > 100 || price <50;

select * from product where price >100 or price <50;

between  A and B

select * from product where price between 50 and 100;等价于下面这一行!
select * from product where price >=50 and price <=100;

not in

select * from product where id in(1,22,33,43,66);

select * from product where id not in(1,22,33,43,66);注:一般不建议使用这个not in 语法!

模糊查询

select * from product where name like '%肉%'; 

注意:一般不建议在关键词的左边写上%,因为这样会遍历数据库表中的所有的数据,会失去用关键字查询的本来目的----提高检索效率。

可以使用%也可以使用短的下划线,其中短的下划线表示代表一类字符。

select * from product where name like '%回%肉%';

select * from product where name like '_回_肉';

"[]"方括号运算符,在方括号里面的东西表示一个关键词标识,意思是方括号里面的字和外面的相组合进行模糊查询,表示取里面的一个字符进行组合模糊查询!

select * from product where name regexp '[锅吧香]肉';

可以用符号^表示取非,成为一个特殊的组合字符。

 排序:order by

select * from product order by id desc;//desc表示反序的意思。
select * from product order by name,id desc;

分组:group by

group by 右边的条件一定大于等于左边的列,查询的列一定在group by 里面包含。

select * from product group by name,id;
select * from product group by id;
select name from product group by name;
select name from product group by name,where_show;

注意事项:

1.select where_show from product where where_show > 2 group by where_show;
2.select where_show from product group by where_show having where_show > 2;

对于以上两中待条件分组的写法,建议不要用第一种,因为第一种是先进行选择然后再分组,所以会进行遍历,第二种先分组再进行条件能够提高运行效率。having 关键词一般只和group by有关,相当于where条件。

分页:limit

表一定要做分页,不然查询起来会很慢。

一般的公式是--(page-1)*size,size;

查询关键字的顺序

select --> from --where -- >group by --> having --> order by-->limit;

数据库里面的函数用法:

 函数运用:查询总条数

select count(*) from product where price > 50;

查询最大值:

select max(price) from product;

查询最小值:
select min(price) from product;

查询平均值:
select avg(price) from product;

查询总和:
select sum(price) from product;

查询日期

select date_format(create_time,'%y-%m-%d') from product;

笛卡尔积:

select * from cuisine,product;

当对象和对象存在一对一,多对一的时候,在“多”的地方加“一”的id。

交叉查询:

select * from cuisine,product where product.cuisine_id = cuisine.id;

使用别名,会更方便语句的书写和查询。

select * from product p,cuisine c where p.cuisine_id=c.id;

内连接:inner join

select * from product p
inner join cuisine c
on p.cuisine_id=c.id
inner join product_cuisine_ralation pcr
on pcr.cuisine_id = p.id
and pcr.product_id = c.id
 

注释:英文符号中短划线,然后记得和后面的注释内容有一个空格。

-- 将所有的产品都查询出来,无论和菜系是否关联,有关联的直接关联就好了,没有关联直接用null代替
 

左连接

select * from product p
left join cuisine c
on p.cuisine_id = c.id;
 

右连接

select * from product p
right join cuisine c
on p.cuisine_id = c.id;

子查询

注意:这个地方有一点不理解。

select * from 
(select p.*,c.name cuisine_name  from product p
left join cuisine c
on p.cuisine_id = c.id) aa
 where aa.name like '%肉%';

合并查询 union all 就算里面有相同的数据,也会查询出来(不去重)

union(去重)

 (select * from product where style=1 limit 1)
union all
 (select * from product where style=2 limit 2)
union all
(select * from product where style not in(1,2) limit 7);

去重:

select distinct(price)  from product;不排序
select distinct(price) from product group by price;排序

sql注入:有危险,做好将条件当作一个整体而不是一个参数进行查询。

是个啥?没搞懂!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值