-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask02.sql
172 lines (124 loc) · 3.62 KB
/
task02.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use shop;
SELECT product_name, product_type
FROM product
WHERE product_type='衣服';
SELECT product_name
FROM product
WHERE product_type = '衣服';
select * from product;
select product_id As id,product_name As name,purchase_price as '进货单价' from product;
select distinct product_type from product;
select product_name,product_type from product where sale_price = 500;
SELECT *
FROM product
WHERE purchase_price is not NULL;
SELECT product_name,sale_price,sale_price * 2 as 'sale_price×2'
from product;
select product_name,sale_price,purchase_price
from product
where sale_price - purchase_price >= 500;
create table chrs
(chr char(3) not null,
primary key(chr));
select chr from chars WHERE chr > '2';
select product_name,purchase_price
from product
where purchase_price is null;
select product_name, product_type, sale_price
from product
where not sale_price >= 1000;
select product_name,product_type,regist_date
from product
where product_type = '办公用品'
and regist_date = '2009-09-11'
or regist_date = '2009-09-20';
select product_name,product_type,regist_date
from product
where product_type = '办公用品'
and (regist_date = '2009-09-11' or regist_date = '2009-09-20');
SELECT *
FROM product
WHERE purchase_name > NULL;
select product_name,sale_price,purchase_price
from product
where sale_price-purchase_price >= 500;
SELECT product_name,sale_price,purchase_price
from product
where not sale_price-purchase_price < 500;
select product_name,product_type
from product
where sale_price*0.9 - purchase_price >=100;
select count(*) from product;
select count(purchase_price) from product;
select sum(sale_price),sum(purchase_price) from product;
SELECT AVG(sale_price), AVG(purchase_price)
FROM product;
-- MAX和MIN也可用于非数值型数据
SELECT MAX(regist_date), MIN(regist_date)
FROM product;
select COUNT(distinct product_type)
from product;
select product_type,count(*) from product;
select product_type,count(*)
from product
GROUP BY product_type;
select product_type,sale_price
from product
GROUP BY product_type;
select product_type from product GROUP BY product_type;
select distinct product_type,count(*) from product;
SELECT purchase_price,count(*)
from product
GROUP BY purchase_price;
SELECT purchase_price,count(*)
from product
WHERE product_type = '衣服'
GROUP BY purchase_price;
SELECT purchase_price
from product
WHERE product_type = '衣服';
SELECT product_type, COUNT(*)
from product
GROUP BY product_type
HAVING count(*) >= 2;
SELECT product_id,product_name,sale_price,purchase_price
from product
ORDER BY sale_price DESC;
CREATE TABLE user (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(5),
date_login DATE,
PRIMARY KEY (id)
);
INSERT INTO user(name, date_login) VALUES
(NULL, '2017-03-12'),
('john', NULL),
('david', '2016-12-24'),
('zayne', '2017-03-02');
SELECT * from user
ORDER BY name is null,name ASC;
SELECT * from `user`
ORDER BY isnull(name),name ASC;
SELECT * FROM `user`
ORDER BY coalesce(name,'zzzzzz') ASC;
SELECT * from `user`
ORDER BY name is not null,name DESC;
SELECT * FROM `user`
ORDER BY -date_login;
SELECT * from `user`
ORDER BY name is NOT null;
select * from(SELECT product_type,SUM(sale_price) as sum1,SUM(purchase_price) as sum2
from product
GROUP BY product_type
HAVING sum1 > sum2*1.5);
SELECT product_type,SUM(sale_price) as sum,SUM(purchase_price) as sum
from product
GROUP BY product_type
HAVING SUM(sale_price) > SUM(purchase_price)*1.5;
SELECT *
from product
ORDER BY regist_date desc,regist_date DESC;
SELECT product_id
--本SELECT语句中存在错误。
FROM product
GROUP BY product_type