mysql子查询
来源:互联网 发布:辽宁快乐12遗漏数据 编辑:程序博客网 时间:2024/06/06 16:39
1. where 型子查询
如题:在goods表中查到最大的价格的一条商品
select * from goods order by shop_price desc limit 1; (排序一般都比较消耗资源)
以上语句也可以用where型子查询来完成。
select * from goods where shop_price=(select max(shop_price) from goods);
这里的条件shop_price用后面的select语句来获取。
select * from goods where shop_price in (select max(shop_price) from goods group by cat_id );--获取每个栏目下的最贵的商品信息
2. from 型子查询
每次查询出来的结果还是可以当成一张表来看。接下去上面的例子:
将上面的查询出来的结果集可以被查询。
select * from (select * from goods where shop_price in (select max(shop_price) from goods group by cat_id )) as mytable where goods_id>20;
需要注意的是一定要加上as 表名
否者会报错:Every derived table must have its own alias
这句话的意思是说每个派生出来的表都必须有一个自己的别名
3. exists 型子查询
如题:用exists型子查询,查出所有不存在商品的栏目
分析:如果有商品那么 select * from goods where cat_id=”栏目id” 能够找到商品。
select * from category where not exists(select * from goods where goods.cat_id=category.cat_id);
以上语句分析:在栏目表中查询所有想要的字段信息,条件是栏目中的cat_id 在商品表中的不存在。
0 0
- mysql 子查询
- mysql 子查询
- Mysql子查询实例
- MySQL IN子查询
- mysql 子查询
- mysql 子查询
- Mysql子查询
- MySQL子查询
- mysql 子查询
- mysql子查询
- mysql子查询
- mysql 子查询
- 24、MySQL-子查询
- Mysql子查询
- MYSQL子查询
- mysql子查询优化
- mysql子查询
- MYSQL 子查询
- 结局的启发
- java web软件了解tomcat
- Mac搭建Java环境
- Android Studio gradle 编译提示‘default not found’ 解决办法
- sql2o的使用
- mysql子查询
- 深度学习画图神器(TikZ)
- C++反射机制的实现
- 杭电OJ——1024 Max Sum Plus Plus
- 无法在 System.Guid 和 System.String 上执行“=”操作
- cocopods升级后报错
- Linux中man手册的使用
- npm install <packageName> 使用时提示command not found 问题
- error LNK2001: 无法解析的外部符号