数据库搜索

一.where条件查询
1.like模糊查询,%是通配符
select*from item where name like '%联想%';
2.and,or前者优先级高
select*from item where name like '%联想%' and detail like '%4%';
select*from item where name like '%联想%' or name like '%宏碁%';

3.>,<,>=,<=,!=,<>
select*from item where name like '%显示器%' and price<=1000;
select*from item where name like '%显示器%' and price<=1000 and price>=900;
select*from item where name like '%显示器%' and price!=1299;
select*from item where name like '%显示器%' and price<>1299;
select*from item where name like '%显示器%' and (price<=1000 or price>5000);

4.between m and n(包含m和n)
select*from item where price between 500 and 1000;
5.is,in,not
select*from item where category_id not in (6,7,8);
select*from item where price not between 500 and 1000;

二.搜索结果排序order by语句——在where子句之后
asc——升序,desc——降序
select*from item where name like '%得力%' order by price asc;
select*from item where name like '%得力%' order by price desc;
select*from item where name like '%得力%' order by price;——默认升序
select*from item where name like '%得力%' order by category_id asc,price desc;

三.搜索结果分页limit子句——在order by语句之后
1.begin(起始行),size(本页总行数)
select*from item order by price asc limit 0,5;——第一页,每页五条
select*from item order by price asc limit 5,5;——第二页,每页五条

四.搜索结果的显示
1.concat()函数——拼接字符串
select concat(name,' ',price,'元') as title,detail,stock from item;
2.数值计算
+ - * / % mod()
select name,detail,price*stock as total from item;

3.date_format()函数——日期格式化
select name,price,date_format(upload_time,'%Y年%c/m月%d日 %h时%i分%s秒') from item;
4.ifnull()函数——空值处理
select name,price,ifnull(image,'无') from item;
5.搜索结果的显示
汇总搜索结果——聚合函数
1.sum();——库存合计
2.avg();——平均单价
select sum(stock),avg(price) from item where name like '%笔记本%';

3.count();——查询条目数
select count(*) from item where name like '%笔记本%';
select count(stock) from item where name like '%笔记本%';
4.max();——最高单价
select max(price) from item where name like '%笔记本%';
5.min();——最小库存
select min(stock) from item where name like '%笔记本%';
select name as 最贵的电脑 from item where price in (select max(price) from item);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

linsa_pursuer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值