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
| 执行顺序:select 列 from 表 where 条件 group by 条件 having 条件 order by 条件 limit
from
SELECT 列1,列2 FROM 表 SELECT * FROM 表
例子:
SELECT * FROM stu ;
SELECT sname , intime FROM stu;
where
SELECT col1,col2 FROM TABLE WHERE colN 条件;
例子:
SELECT * FROM city WHERE countrycode='CHN';
SELECT * FROM city WHERE NAME='peking';
SELECT * FROM city WHERE district='gansu'; where配合比较操作符(> < >= <= <>) 例子:
SELECT * FROM city WHERE population<100; where配合逻辑运算符(and or ) 例子:
SELECT * FROM city WHERE countrycode='CHN' AND population>5000000;
SELECT * FROM city WHERE countrycode='CHN' OR countrycode='USA';
where配合like模糊查询 例子:
SELECT * FROM city WHERE district LIKE 'guang%'; 注意:如果需要查询带有guang的数据库可以用'%guang%',但是%不建议放在前面,性能较差,因为不走索引。如果此需求较大可以用NOSQL数据库替代。
SELECT * FROM city WHERE contrycode LIKE 'c%';
where配合in语句
SELECT * FROM city WHERE countrycode IN ('CHN' ,'USA');
where配合between and
SELECT * FROM city WHERE population >1000000 AND population <2000000; SELECT * FROM city WHERE population BETWEEN 1000000 AND 2000000;
|