Home > Database > Mysql Tutorial > body text

mysql having

黄舟
Release: 2017-01-16 13:10:38
Original
1717 people have browsed it

having
Query the columns with a price difference of more than 200

select goods_id,(market_price - shop_price ) as chajia from goods having chajia>200;
Copy after login

Query the total amount of extruded goods

select sum(goods_number*shop_price) from goods;
Copy after login

Query the backlog of goods under each column

mysql> select cat_id ,sum(goods_number*shop_price) from goods group by cat_id;
+--------+------------------------------+
| cat_id | sum(goods_number*shop_price) |
+--------+------------------------------+
| 2 | 0.00 | 
| 3 | 356235.00 | 
| 4 | 9891.00 | 
| 5 | 29600.00 | 
| 8 | 4618.00 | 
| 11 | 790.00 | 
| 13 | 134.00 | 
| 14 | 162.00 | 
| 15 | 190.00 | 
+--------+------------------------------+
Copy after login

Query the columns with a backlog greater than 20,000

mysql> select cat_id ,(sum(goods_number*shop_price)) as dae from goods group by cat_id having dae > 20000;
+--------+-----------+
| cat_id | dae |
+--------+-----------+
| 3 | 356235.00 | 
| 5 | 29600.00 | 
+--------+-----------+
insert into result
values
('张三','数学',90),
('张三','语文',50),
('张三','地理',40),
('李四','语文',55),
('李四','政治',45),
('王五','政治',30);
Copy after login

Find the average value of those who failed in more than two subjects

Reverse logic

select name,avg(score) from result group by name having (sum(score<60))>=2 ;
Copy after login

Both are equivalent

select name,avg(score),sum(score<60) as guake from result group by name having guake>=2;
Copy after login

Forward logic (subquery used)

select name,avg(score)
from result
where name in ( 
select name from ( 
(select name ,count(*) as guake from result where score<60 group by name having guake>=2) as tmp 
)
)
group by name;
Copy after login

The above is the content of mysql having, for more related content, please pay attention to PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!