having similar and where, you can filter the data, where the expression how to write, how to write after having
where to play a role for the columns in the table,
query
data
having to play a role in the query results columns,
filter
data
# Inquiry Our commodity price than the market price how much money is low, the low output of more than 200 yuan of goods
select goods_id,good_name,market_price - shop_price as s from goods having s>200 ;
// can not be used here because where s is the query results, where only screening of field names in the table
If it is, then where:
select goods_id,goods_name from goods where market_price - shop_price > 200;
# Use where and having
select cat_id,goods_name,market_price - shop_price as s from goods where cat_id = 3 having s > 200;
# Inquiry backlog of more than 20,000 yuan of money column, and the column backlog of payment
select cat_id,sum(shop_price * goods_number) as t from goods group by cat_id having s > 20000
# Inquiry two subjects and two more student who has failed the average
Ideas:
# First calculate the average of all students
select name,avg(score) as pj from stu group by name;
# Identify all students hanging branches situation
select name,score<60 from stu;
# This score <60 statement is determined, the result is true or false, mysql the true 1 0 false
# Find out two and two or more failing students
select name,sum(score<60) as gk from stu group by name having gk > 1;
# Consolidated results
select name,sum(score<60) as gk,avg(score) as pj from stu group by name having gk >1;
4、order by
(1) order by price // default ascending
(2) order by price desc // descending
(3) order by price asc // ascending order, as the default
(4) order by rand () // random arrangement, efficiency is not high
# Column number in ascending order, commodity prices under each column in descending order
select * from goods where cat_id !=2 order by cat_id,price desc;
5、limit
limit [offset,] N
at offset, alternatively, do not write the equivalent limit 0, N
Remove Entry N
# Take high commodity prices 4-6
select good_id,goods_name,goods_price from goods order by good_price desc limit 3,3;
### inquiries under each section of the most expensive commodities
Ideas:
# The first sort of commodity prices in each section
select cat_id,goods_id,goods_name,shop_price from goods order by cat_id,shop_price desc;
# Query results above the first line of each commodity column is the most expensive commodity
# The above query understood as the result of a temporary table [in memory] [Subqueries]
# Re-elected each section the most expensive merchandise from the temporary table
select * from (select goods_id,goods_name,cat_id,shop_price from goods order by cat_id,shop_price desc) as t group by cat_id;
# Is used herein because cat_id group by the first temporary table for each commodity column is the most expensive commodity, and without the use of the front group by aggregation function, so the default on the first line of data for each fetch packet, where to cat_id packet
Good understanding of the model:
1, where the latter expression, the expression on each line to see if the establishment
2, the fields (columns), it is understood variables, calculates the values (arithmetic and logic operations)
3, an extraction result can be understood as temporary table