练习一
创建表,并插入相关数据
CREATE TABLE email ( ID INT NOT NULL PRIMARY KEY, Email VARCHAR(255) ) INSERT INTO email VALUES('1','a@b.com'); INSERT INTO email VALUES('2','c@d.com'); INSERT INTO email VALUES('3','a@b.com');
查询案例
#查询表中所有重复的电子邮箱 SELECT * from email HAVING count(email) >1; 可能也会以为这样也行(!错误的) SELECT * from email where count(email) >1 ;
这里涉及到where和having的使用:
(1) where:
是作用在查询结果之前,
(2) having:
是作用在查询结果之后在执行,
说到执行顺序,一般网上都是这样 from> join > on > where >group by > having >select >order by
练习二
创建表并插入相关数据
CREATE TABLE World ( name VARCHAR(50) NOT NULL, continent VARCHAR(50) NOT NULL, area INT NOT NULL, population INT NOT NULL, gdp INT NOT NULL ); INSERT INTO World VALUES('Afghanistan','Asia',652230,25500100,20343000); INSERT INTO World VALUES('Albania','Europe',28748,2831741,12960000); INSERT INTO World VALUES('Algeria','Africa',2381741,37100000,188681000); INSERT INTO World VALUES('Andorra','Europe',468,78115,3712000); INSERT INTO World VALUES('Angola','Africa',1246700,20609294,100990000);
练习
#查找 国家的面积超过 300 万平方公里,或者(人口超过 2500 万并且 gdp 超过 2000 万)
SELECT * FROM world WHERE ( population > 25000000 AND gdp > 20000000 ) OR area > 3000000;