mysql的having语句
mysql> use qq;
Database changed
mysql> #查询本店价比市场价省的钱,并且要求省钱200元以上的取出来
mysql> select goods_id,market_price,shop_price,(market_price-shop_price) as discount from goods where having discount>200;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'having discount>200' at line 1
mysql> select goods_id,market_price,shop_price,(market_price-shop_price) as discount from goods having discount>200;
+----------+--------------+------------+----------+
| goods_id | market_price | shop_price | discount |
+----------+--------------+------------+----------+
| 1 | 1665.60 | 1388.00 | 277.60 |
| 9 | 2757.60 | 2298.00 | 459.60 |
| 10 | 1593.60 | 1328.00 | 265.60 |
| 13 | 1573.20 | 1311.00 | 262.20 |
| 14 | 3150.00 | 2625.00 | 525.00 |
| 17 | 2760.00 | 2300.00 | 460.00 |
| 18 | 3453.60 | 2878.00 | 575.60 |
| 21 | 2400.00 | 2000.00 | 400.00 |
| 22 | 7198.80 | 5999.00 | 1199.80 |
| 23 | 4440.00 | 3700.00 | 740.00 |
| 24 | 2400.00 | 2000.00 | 400.00 |
| 31 | 1604.39 | 1337.00 | 267.39 |
| 32 | 3612.00 | 3010.00 | 602.00 |
+----------+--------------+------------+----------+
13 rows in set (0.01 sec) mysql> #where和having不能同时出现
mysql> #查询出每种商品所积压的货款
mysql> select goods_id,shop_price,goods_number,shop_price*goods_number as jy from goods;
+----------+------------+--------------+-----------+
| goods_id | shop_price | goods_number | jy |
+----------+------------+--------------+-----------+
| 1 | 1388.00 | 1 | 1388.00 |
| 4 | 58.00 | 17 | 986.00 |
| 3 | 68.00 | 24 | 1632.00 |
| 5 | 20.00 | 8 | 160.00 |
| 6 | 42.00 | 15 | 630.00 |
| 7 | 100.00 | 20 | 2000.00 |
| 8 | 399.00 | 1 | 399.00 |
| 9 | 2298.00 | 4 | 9192.00 |
| 10 | 1328.00 | 7 | 9296.00 |
| 11 | 1300.00 | 1 | 1300.00 |
| 12 | 983.00 | 8 | 7864.00 |
| 13 | 1311.00 | 8 | 10488.00 |
| 14 | 2625.00 | 1 | 2625.00 |
| 15 | 788.00 | 3 | 2364.00 |
| 16 | 823.33 | 0 | 0.00 |
| 17 | 2300.00 | 1 | 2300.00 |
| 18 | 2878.00 | 1 | 2878.00 |
| 19 | 858.00 | 12 | 10296.00 |
| 20 | 280.00 | 12 | 3360.00 |
| 21 | 2000.00 | 40 | 80000.00 |
| 22 | 5999.00 | 1 | 5999.00 |
| 23 | 3700.00 | 8 | 29600.00 |
| 24 | 2000.00 | 100 | 200000.00 |
| 25 | 48.00 | 2 | 96.00 |
| 26 | 19.00 | 2 | 38.00 |
| 27 | 95.00 | 2 | 190.00 |
| 28 | 45.00 | 0 | 0.00 |
| 29 | 90.00 | 0 | 0.00 |
| 30 | 18.00 | 9 | 162.00 |
| 31 | 1337.00 | 1 | 1337.00 |
| 32 | 3010.00 | 4 | 12040.00 |
+----------+------------+--------------+-----------+
31 rows in set (0.00 sec) mysql> #查询该店积压的总货款,每个商品的积压货款之和
mysql> select sum(goods_number*shop_price) from goods;
+------------------------------+
| sum(goods_number*shop_price) |
+------------------------------+
| 398620.00 |
+------------------------------+
1 row in set (0.00 sec) mysql> #查询每个栏目下的积压货款
mysql> select cat_id,sum(shop_price*goods_number) from goods group by cat_id;
+--------+------------------------------+
| cat_id | sum(shop_price*goods_number) |
+--------+------------------------------+
| 2 | 0.00 |
| 3 | 356235.00 |
| 4 | 6891.00 |
| 5 | 29600.00 |
| 8 | 4618.00 |
| 11 | 790.00 |
| 13 | 134.00 |
| 14 | 162.00 |
| 15 | 190.00 |
+--------+------------------------------+
9 rows in set (0.00 sec) mysql> #查询出栏目积压货款,,并筛选出积压货款>20000的栏目
mysql> select cat_id,sum(goods_number*shop_price) as jhk
-> from goods group by cat_id
-> having jhk>20000;
+--------+-----------+
| cat_id | jhk |
+--------+-----------+
| 3 | 356235.00 |
| 5 | 29600.00 |
+--------+-----------+
2 rows in set (0.00 sec) mysql> create table result(
-> name varchar(20) default null,
-> subject varchar(20) default null,
-> score tinyint(4) default null
-> )engine myisam charset utf8;
Query OK, 0 rows affected (0.08 sec) mysql> insert into result{
-> \c
mysql> insert into result
-> values
-> ('张三','数学',90),
-> ('张三','语文',50),
-> ('张三','地理',40)
-> ;
ERROR 1366 (HY000): Incorrect string value: '\xD5\xC5\xC8\xFD' for column 'name' at row 1
mysql> set names gnk;
ERROR 1115 (42000): Unknown character set: 'gnk'
mysql> set names gbk;
Query OK, 0 rows affected (0.00 sec) mysql> insert into result
-> values
-> ('张三','数学',90),
-> ('张三','语文',50),
-> ('张三','地理',40);
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0 mysql> insert into result
-> values
-> ('','',55),
-> ('张三','数学',90),\c
mysql> insert into result
-> values
-> ('李四','语文',55),
-> ('李四','政治',45),
-> ('王五','政治',30);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from result;
+------+---------+-------+
| name | subject | score |
+------+---------+-------+
| 张三 | 数学 | 90 |
| 张三 | 语文 | 50 |
| 张三 | 地理 | 40 |
| 李四 | 语文 | 55 |
| 李四 | 政治 | 45 |
| 王五 | 政治 | 30 |
+------+---------+-------+
6 rows in set (0.03 sec) mysql> #查询2门及2门以上不及格者的平均成绩
mysql> select name,avg(subject) form result group by name having subject<60;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'result group by name having subject<60' at line 1
mysql> select name,avg(subject) form result group by name having score<60;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'result group by name having score<60' at line 1
mysql> select name,avg(score) form result group by name where score<60;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'result group by name where score<60' at line 1
mysql> select name,score,score<60 from result group by name;
+------+-------+----------+
| name | score | score<60 |
+------+-------+----------+
| 张三 | 90 | 0 |
| 李四 | 55 | 1 |
| 王五 | 30 | 1 |
+------+-------+----------+
3 rows in set (0.02 sec) mysql> select name,score,score<60 from result;
+------+-------+----------+
| name | score | score<60 |
+------+-------+----------+
| 张三 | 90 | 0 |
| 张三 | 50 | 1 |
| 张三 | 40 | 1 |
| 李四 | 55 | 1 |
| 李四 | 45 | 1 |
| 王五 | 30 | 1 |
+------+-------+----------+
6 rows in set (0.00 sec) mysql> select name,score,sum(score<60) from result group by name;
+------+-------+---------------+
| name | score | sum(score<60) |
+------+-------+---------------+
| 张三 | 90 | 2 |
| 李四 | 55 | 2 |
| 王五 | 30 | 1 |
+------+-------+---------------+
3 rows in set (0.00 sec) mysql> select name,score,sum(score<60) as g from result group by name having g>=2;
+------+-------+------+
| name | score | g |
+------+-------+------+
| 张三 | 90 | 2 |
| 李四 | 55 | 2 |
+------+-------+------+
2 rows in set (0.00 sec) mysql> select name,avg(score) from result group by name having sum(score<60)>=2;
+------+------------+
| name | avg(score) |
+------+------------+
| 张三 | 60.0000 |
| 李四 | 50.0000 |
+------+------------+
2 rows in set (0.00 sec) mysql> select name,avg(score) from result group by name where sum(score<60)>=2;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where sum(score<60)>=2' at line 1
mysql> select name,avg(score) from result group by name having sum(score<60)>=2;
+------+------------+
| name | avg(score) |
+------+------------+
| 张三 | 60.0000 |
| 李四 | 50.0000 |
+------+------------+
2 rows in set (0.00 sec) mysql> select name,avg(score),sum(score<60) as g from result group by name having g>=2;
+------+------------+------+
| name | avg(score) | g |
+------+------------+------+
| 张三 | 60.0000 | 2 |
| 李四 | 50.0000 | 2 |
+------+------------+------+
2 rows in set (0.00 sec) mysql> #首先将平均数求出,然后再筛选出挂科两科及两科以上的同学,筛选用score<60,是否及格用0,1表示,然后求和就是挂科数,筛选出>=2的,整出结果
mysql> #找出每个栏目下id号最大的商品
mysql> select goods_id,cat_id,goods_name from goods group by cat_id order by goods_id desc limit 1;
+----------+--------+-----------------+
| goods_id | cat_id | goods_name |
+----------+--------+-----------------+
| 29 | 14 | 移动100元充值卡 |
+----------+--------+-----------------+
1 row in set (0.03 sec) mysql> select goods_id,cat_id,goods_name from goods group by cat_id having goods_id order by goods_id desc limit 1;
+----------+--------+-----------------+
| goods_id | cat_id | goods_name |
+----------+--------+-----------------+
| 29 | 14 | 移动100元充值卡 |
+----------+--------+-----------------+
1 row in set (0.00 sec) mysql> select goods_id,cat_id,goods_name from goods group by cat_id having goods_id order by goods_id desc;
+----------+--------+-----------------------+
| goods_id | cat_id | goods_name |
+----------+--------+-----------------------+
| 29 | 14 | 移动100元充值卡 |
| 27 | 15 | 联通100元充值卡 |
| 25 | 13 | 小灵通/固话50元充值卡 |
| 23 | 5 | 诺基亚N96 |
| 16 | 2 | 恒基伟业G101 |
| 8 | 3 | 飞利浦9@9v |
| 5 | 11 | 索爱原装M2卡读卡器 |
| 4 | 8 | 诺基亚N85原装充电器 |
| 1 | 4 | KD876 |
+----------+--------+-----------------------+
9 rows in set (0.00 sec) mysql> select goods_id,cat_id,goods_name from goods group by cat_id having max(goods_id);
+----------+--------+-----------------------+
| goods_id | cat_id | goods_name |
+----------+--------+-----------------------+
| 16 | 2 | 恒基伟业G101 |
| 8 | 3 | 飞利浦9@9v |
| 1 | 4 | KD876 |
| 23 | 5 | 诺基亚N96 |
| 4 | 8 | 诺基亚N85原装充电器 |
| 5 | 11 | 索爱原装M2卡读卡器 |
| 25 | 13 | 小灵通/固话50元充值卡 |
| 29 | 14 | 移动100元充值卡 |
| 27 | 15 | 联通100元充值卡 |
+----------+--------+-----------------------+
9 rows in set (0.00 sec) mysql> select goods_id,cat_id,goods_name from goods group by cat_id where cat_id having max(goods_id);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where cat_id having max(goods_id)' at line 1
mysql> select goods_name,goods_id,cat_id from (select goods_name,cat_id,goods_id from goods order by goods_id desc)as temp group by cat_id;
+------------------------------+----------+--------+
| goods_name | goods_id | cat_id |
+------------------------------+----------+--------+
| 恒基伟业G101 | 16 | 2 |
| 诺基亚N85 | 32 | 3 |
| 夏新T5 | 18 | 4 |
| 诺基亚N96 | 23 | 5 |
| 诺基亚N85原装立体声耳机HS-82 | 7 | 8 |
| 胜创KINGMAX内存卡 | 6 | 11 |
| 小灵通/固话20元充值卡 | 26 | 13 |
| 移动20元充值卡 | 30 | 14 |
| 联通50元充值卡 | 28 | 15 |
+------------------------------+----------+--------+
9 rows in set (0.03 sec) mysql> #必须要用到子查询
mysql的having语句的更多相关文章
- Mysql 常用 SQL 语句集锦
Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...
- 基于表的数据字典构造MySQL建表语句
表的数据字典格式如下: 如果手动写MySQL建表语句,确认麻烦,还不能保证书写一定正确. 写了个Perl脚本,可快速构造MySQL脚本语句. 脚本如下: #!/usr/bin/perl use str ...
- mysql常用操作语句
mysql常用操作语句 1.mysql -u root -p 2.mysql -h localhost -u root -p database_name 2.列出数据库: 1.show datab ...
- Mysql 常用 SQL 语句集锦 转载(https://gold.xitu.io/post/584e7b298d6d81005456eb53)
Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...
- MySQL之alter语句用法总结
MySQL之alter语句用法总结 1:删除列 ALTER TABLE [表名字] DROP [列名称] 2:增加列 ALTER TABLE [表名字] ADD [列名称] INT NOT NULL ...
- MySQL数据库sql语句的一些简单优化
1.查询条件的先后顺序 有多个查询条件时,要把效率高能更精确筛选记录的条件放在后边.因为MySQL解析sql语句是从后往前的(不知是否准确). 例: select a.*,b.* from UsrIn ...
- MySql的like语句中的通配符:百分号、下划线和escape
MySql的like语句中的通配符:百分号.下划线和escape %:表示任意个或多个字符.可匹配任意类型和长度的字符. Sql代码 select * from user where user ...
- 23个MySQL常用查询语句
23个MySQL常用查询语句 一查询数值型数据: SELECT * FROM tb_name WHERE sum > 100; 查询谓词:>,=,<,<>,!=,!> ...
- mysql下sql语句 update 字段=字段+字符串
mysql下sql语句 update 字段=字段+字符串 mysql下sql语句令某字段值等于原值加上一个字符串 update 表明 SET 字段= 'feifei' || 字段; (postgr ...
- MYSQL中的语句
MYSQL中的语句 decimal(8,2):最多存10位数的数字,小数点后保存两位.如:999999.99
随机推荐
- 理解SVG坐标系统和变换: transform属性
SVG元素可以通过缩放,移动,倾斜和旋转来变换-类似HTML元素使用CSS transform来变换.然而,当涉及到坐标系时这些变换所产生的影响必然有一定差别.在这篇文章中我们讨论SVG的transf ...
- C#中属性简写原理
1. 属性简写时不能只有get或者set 原因: 如果只有get,那么没有办法给其赋值,所有也就没法get到值: 如果只有set,没有意义,因为根本没法获取到这个值.
- 【Java基础】Java中的多态
什么是多态 多态是一个对象的多种实现,是建立在继承的基础上的,即对象“人”,有老师和学生不同的实现,其实总结起来就是允许将子类类型的指针赋值给父类类型的指针. 多态的发生条件 多态发生的前提是:1. ...
- hadoop入门必备基础知识
1.对Linux 系统的要求 会基本的命令: (1)知道root用户 (2)ls命令会查看文件夹内容 (3)cd命令等2.Java 的要求 ...
- HW2.23
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- POJ2250 - Compromise(LCS+打印路径)
题目大意 给定两段文本,问公共单词有多少个 题解 裸LCS... 代码: #include<iostream> #include<string> using namespace ...
- hdoj 5112 A Curious Matt
A Curious Matt Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) ...
- fileupload控件上传、文件下载
常遇到上传下载的功能,这里把我习惯的用法写下来: 上传: string fileName = "";fileName = this.fu_pic.FileName;if (stri ...
- JS继承的几种方式
JS作为面向对象的弱类型语言,继承也是其非常强大的特性之一. 既然要实现继承,那么我们先定义一个父类: // 定义一个动物类 function Animal (name) { // 属性 this.n ...
- 【JAVA - SSM】之MyBatis动态SQL
动态SQL就是在SQL语句中添加一些标签,以完成某些逻辑.通常用到的动态SQL标签有<if>.<choose>.<where>.<trim>.<s ...