一、查询语句
1、查询某个表中所有字段及数据
1
| select * from <table-name>;
|
2、查询某个表中指定字段的所有数据 (逗号分隔)
1
| select 字段名, 字段名 from <table-name>;
|
3、查询某个表共有多少条数据
1
| select count(1) from <table-name>;
|
4、查询某个字段共有多少条数据
1
| select count(字段) from <table-name>;
|
5、查询某个字段数据汇总值是多少
1
| select sum(字段名) from <table-name>;
|
6、按字段进行分组,查询所有数据 (group by 之后为分组字段)
1
| select * from <table-name> group by 字段名;
|
二、条件查询语句
1、查询数据值在一定范围内 ( between…and… )
1
| select * from <table-name> where 字段 between 值1 and 值2;
|
2、查询字段值等于某个值的数据 ( = )
1
| select * from <table-name> where 字段 = 值;
|
3、查询字段值不等于某个值的数据 ( != )
1
| select * from <table-name> where 字段 != 值;
|
4、查询数据时,同时成立多个条件 ( and )
1
| select * form <table-name> where 字段值 = 值 and 字段值 = 值;
|
5、查询数据时,在给出条件中成立任意一个条件 ( or )
1
| select *from <table-name> where 字段值 = 值 or 字段值 = 值;
|
6、查询数据时,字段值在给出的值中检索数据 ( in )
1
| select * from <table-name> where 字段值 in (值1,值2,....)
|
7、查询数据时,字段值不检索给出的值 ( not in ) –not 取反
1
| select * from <table-name> where 字段值 not in (值1,值2,....)
|
三、模糊查询 (like
)
1、匹配任意一个字符 (_
)
1 2
| Select * from <table-name> where 字段 like '_明'
|
2、匹配任意多个字符 (%
)
1 2
| select * from <table-name> where 字段 like '%明'
|
3、通配符混合使用 (_%
)
1 2
| Select * from <table-name> where 字段 like '%明_'
|
3、模糊查询数据,但只显示没符合条件的数据 (not like
)
1 2
| select * from <table-name> where 字段not like '_明'
|
四、SQL语句中的计算
1、对字段 加、减、乘、除
1
| select 字段 + 100 * 2 / 5 - 1000 from <table-name>;
|
2、字段 与 字段 进行 加、减、乘、除
1
| select 字段 + 字段2 / 字段3 from <table-name>;
|