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
随机推荐
- 嵌入式 hi3518c裸板uboot烧写、kernel烧写、fs烧写小结
1.在uboot中我可以添加自己的命令,添加的方法是找到一个uboot的命令,然后模仿着去增加属于自己的命令代码以及实现函数就可以 2.记住在使用printf进行调试的时候,在遇到指针或者字符串的时候 ...
- Linux下的iwpriv(iwlist、iwconfig)的简单应用
无线网络飞速发展的今天,许多设备都提供了连接无线网络的功能. 那么Linux下的wifi到底该怎么配置.连接呢?? 开始配置之前,我们要说说iw家族.iw是linux下常用的wifi配置工具,网上有相 ...
- Effect-Compiler Tool(fxc.exe)
提前编译shader文件,提高运行时的效率. refer to http://msdn.microsoft.com/en-us/library/windows/desktop/bb509710%28v ...
- FIRST集和FOLLOW集
省略号代表其他相关产生式得出的终结符号,一开始的时候,省略号里面是没有的 求FIRST集 情况壹 如果A只在→的右边出现,那么FIRST(A)={A},例子M→α,FIRST(α)={α} 情况 ...
- 输入一个正数 n,输出所有和为 n 连续正数序列。 java实现
用了半小时才写出来啊, 其实这种思路应用范围很广,利用有序性这个特点,比如本体只要是有序就行,例如两个集合是有序的,求交集,有序数组的查找,有序数组的归并,有序有了优化的可能. 输入一个正数 n,输出 ...
- CURL超时处理
一般会设置一个超时时间1S,就是说如果php那边在1S内没有返回给urlserver的话就忽略掉该请求,及不阻塞等待返回了,直接处理下面的操作. 现在php那边有时候会卡,这样一卡就无法再1S内返回消 ...
- Spring笔记之(一)初探
对spring框架的学习我是从模拟它的简单实现开始,这样也易于领悟到它的整个框架结构,以下是简单实现的代码: 配置文件:spring.xml <?xml version="1.0&qu ...
- POJ1947 - Rebuilding Roads(树形DP)
题目大意 给定一棵n个结点的树,问最少需要删除多少条边使得某棵子树的结点个数为p 题解 很经典的树形DP~~~直接上方程吧 dp[u][j]=min(dp[u][j],dp[u][j-k]+dp[v] ...
- POJ1006 - Biorhythms(中国剩余定理)
题目大意 略...有中文... 题解 就是解同余方程组 x≡(p-d)(mod 23) x≡(e-d)(mod 28) x≡(i-d)(mod 33) 最简单的中国剩余定理应用.... 代码: #in ...
- aix 扩展文件系统
今天发现公司的oracle测试 数据库不能启动,检查警告日志日志,提示归档空间不足,不能归档,于是扩展文件系统: 1.检查rootvg卷组的剩余空间[p2704u]:[/dsg/oracle11]$ ...