SQL query (single-table queries)

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

 

table of Contents

aims

Preparation:

Basic data:

This is a simple query :( conditions directly copied directly in his editor that he tried to practice)

Conditions inquiry:

Sort query:

Aggregate queries :( lateral query, column)

Grouping queries:


aims

For this blog focusing on basic exercises, a simple query, the query conditions, sorting queries, combined queries, aggregate queries, grouping inquiries. Each query has a corresponding small example, if you are a beginner, it is recommended to write at least three or more times. Preliminary data preparation, I am here ready, no need to waste time re-building a database, build tables, insert data and other operations, our goal is to master for single-table queries.

Preparation:

 First, the basic data, their user base run the next series of queries. Conditions inquiry, I will put a special outside, to facilitate your practice. I use the sqlyong.

Basic data:

CREATE DATABASE cyl;
USE cyl;

#创建商品表:
CREATE TABLE product(
    -- 商品主键
	pid INT PRIMARY KEY,
    -- 商品名称
	pname VARCHAR(20),
    -- 商品价格
	price DOUBLE,
    -- 商品分类
	category_id VARCHAR(32)
);
INSERT INTO product(pid,pname,price,category_id) VALUES(1,'联想',5000,'c001');
INSERT INTO product(pid,pname,price,category_id) VALUES(2,'海尔',3000,'c001');
INSERT INTO product(pid,pname,price,category_id) VALUES(3,'雷神',5000,'c001');
INSERT INTO product(pid,pname,price,category_id) VALUES(4,'JACK JONES',800,'c002');
INSERT INTO product(pid,pname,price,category_id) VALUES(5,'真维斯',200,'c002');
INSERT INTO product(pid,pname,price,category_id) VALUES(6,'花花公子',440,'c002');
INSERT INTO product(pid,pname,price,category_id) VALUES(7,'劲霸',2000,'c002');
INSERT INTO product(pid,pname,price,category_id) VALUES(8,'香奈儿',800,'c003');
INSERT INTO product(pid,pname,price,category_id) VALUES(9,'相宜本草',200,'c003');
INSERT INTO product(pid,pname,price,category_id) VALUES(10,'面霸',5,'c003');
INSERT INTO product(pid,pname,price,category_id) VALUES(11,'好想你枣',56,'c004');
INSERT INTO product(pid,pname,price,category_id) VALUES(12,'香飘飘奶茶',1,'c005');
INSERT INTO product(pid,pname,price,category_id) VALUES(13,'果9',1,NULL);

# # Query to query all of the merchandise trade names and commodity prices # alias query keyword is used as (as can be omitted) Table Alias:... # Alias ​​query keyword is used as (as can be omitted). column aliases: # # remove duplicates query result is the expression (operation query): the prices of all commodities +10 display.This is a simple query :( conditions directly copied directly in his editor that he tried to practice)

  • # (Note alias)
  • # Query for all goods.    
  • # Query trade names and commodity prices.
  • # Alias ​​query keyword is used as (as can be omitted) Table alias:
  • # Alias ​​query keyword is used as (as can be omitted) column alias:
  • # Remove duplicate values.
  • # Query result is the expression (operation query): The prices of all commodities +10 display. 
#查询所有的商品.
SELECT *FROM product;	
#查询商品名和商品价格.(这里我忘记,(pname,price)逗号)
SELECT pname,price FROM product;
#别名查询.使用的关键字是as(as可以省略的).表别名:
  #select pname "电脑",price "价格" from product;
   SELECT pname,price FROM product AS p;
#别名查询.使用的关键字是as(as可以省略的).列别名:
SELECT pname AS n FROM product AS p;

#去掉重复值.
SELECT DISTINCT price FROM product;

#查询结果是表达式(运算查询):将所有商品的价格+10元进行显示. 
SELECT price+10 FROM product;

Conditions inquiry:

# (Not, in, is, is not) use this four-way, I forgot to
# query trade name "Playboy" Whole information:
# query commodity price of 800
# 800 Check rates not all goods
# check prices of $ 60 is greater than all the item information
# check prices between 200-1000 all goods
# queries all commodities or goods 200 800
# query 'Hong' all items beginning
# of the second word queries 'wants' of all goods
# goods not classified goods
# commodity classification of query

#查询商品名称为“花花公子”的商品所有信息:
SELECT *FROM product WHERE pname="花花公子";

#查询价格为800商品
SELECT *FROM product WHERE price=800;

#查询价格不是800的所有商品(第三种方法,需要注意啦)
SELECT *FROM product WHERE price<>800;
SELECT *FROM product WHERE price!=800;
SELECT *FROM product WHERE NOT(price=800);

#查询商品价格大于60元的所有商品信息
SELECT *FROM product WHERE price>800;

#查询商品价格在200到1000之间所有商品
SELECT *FROM product WHERE price BETWEEN 200 AND 1000;
SELECT *FROM product WHERE price>=200 AND price<=1000;

#查询商品价格是200或800的所有商品
SELECT *FROM product WHERE price=200 OR price=800;
SELECT *FROM product WHERE price IN(200,800);

#查询以'香'开头的所有商品(我把顺序竟然记错了)
SELECT *FROM product WHERE pname LIKE "香%";

#查询第二个字为'想'的所有商品
SELECT *FROM product WHERE pname LIKE "_想%";

#查询含有为'想'的所有商品
SELECT *FROM product WHERE pname LIKE "%想%";

#查询第3个字为'你'的所有商品
SELECT *FROM product WHERE pname LIKE "__你%";

#商品没有分类的商品
#SELECT *FROM product WHERE category_id=null;(这种方式是错误的)
SELECT *FROM product WHERE category_id IS NULL;

#查询有分类的商品
SELECT *FROM product WHERE category_id IS NOT NULL;

Sort query:

#SELECT * FROM table name ORDER BY sort field ASC | DESC;
#ASC Ascending (default)
#DESC Descending

# Use (descending order)
# on the basis of price (descending) on to classification (descending order)
# display the prices of goods (to repeat), and (descending)

#使用价格排序(降序)
SELECT * FROM product ORDER BY price DESC;

#在价格排序(降序)的基础上,以分类排序(降序)
SELECT * FROM product ORDER BY category_id DESC;

#显示商品的价格(去重复),并排序(降序)

SELECT DISTINCT price FROM product ORDER BY price DESC;

Aggregate queries :( lateral query, column)

Aggregate field function, sum, count, avg, max , min,. # Query commodity total article number
# Check rates greater than the total strip number 200 product
sum # query is classified as' C001 'of all commodities
# query is classified as' c002 'average prices for all commodities
maximum price and minimum price # commodity inquiry

SELECT *FROM product;
#查询商品的总条数
SELECT COUNT(*) FROM product;
 
#查询价格大于200商品的总条数
SELECT COUNT(*) FROM product WHERE price>200;

#查询分类为'c001'的所有商品的总和
SELECT SUM(price) FROM product WHERE category_id='c001';

#查询分类为'c002'所有商品的平均价格
SELECT AVG(price) FROM product WHERE category_id='c002';

#查询商品的最大价格和最小价格
SELECT MIN(price),MAX(price) FROM product;

Grouping queries:

SELECT field 1, field 2 ... FROM table packet field HAVING GROUP BY grouping condition;
# statistical classification of each item number
# count the number of each classification of goods, and only display information of the number is greater than 1

#统计各个分类商品的个数
SELECT category_id, COUNT(*) FROM product GROUP BY  category_id;

#统计各个分类商品的个数,且只显示个数大于1的信息
SELECT category_id,COUNT(*) FROM product GROUP BY category_id HAVING COUNT(*)>1;

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/longyanchen/article/details/93096834