select查询语句

1.select

1.1 SELECT 单独使用 ***

1.查询数据库的参数
SELECT @@port;
SELECT @@datadir;
SELECT @@basedir;
SELECT @@innodb_flush_log_at_trx_commit;
SHOW VARIABLES LIKE '%trx%';
SHOW VARIABLES LIKE '%dir%';


2.调用内置函数
USE oldboy
SELECT DATABASE();
SELECT NOW()
SELECT CONCAT (USER,"@",HOST) FROM mysql.user;
SELECT GROUP_CONCAT(xid) FROM student;
SELECT SUM(xid) FROM student;


3.简易计算器
SELECT 4*5;

1.2SELECT 配合其他的句子使用

1.2.1 子句列表介绍

FROM     -- 查询对象(表,视图)
WHERE    -- 过滤子句(grep)
GROUP BY -- 分组子句(统计分析类)
HAVING   -- 后过滤子句
ORDER BY -- 排序子句
LIMIT    -- 限制子句(分页子句)

1.2.2配合from使用

word模板库介绍
--- city -- 城市
--- id   -- 序号id主键
--- NAME -- 城市名
--- Countrycode -- 国家代码(CHN  USA  JPN)
--- District    -- 省,州
--- Population  -- 城市人口数



例子:
1.查询表中所有数据(cat)
SELECT * FROM city;


2.查询name和population的信息
SELECT NAME,Population FROM city;

1.2.3 select+from + where(grep)使用

where配合等值查询
例子:
1.查询中国所有的城市
SELECT * FROM city WHERE CountryCode='CHN';
SELECT * FROM city WHERE ID=100;


2.查询中国河北省的城市信息
SELECT * FROM city 
WHERE CountryCode='CHN' AND District='hebei';


3.查询中国或者美国的城市
SELECT * FROM city
WHERE CountryCode='CHN' OR CountryCode='USA';
或者:
SELECT * FROM city
WHERE CountryCode IN ('CHN','USA');
或者:(用UNION ALL将上下的内容链接起来)
SELECT * FROM city
WHERE CountryCode='CHN'
UNION ALL
SELECT * FROM city
WHERE CountryCode='USA';


where配合范围查询
例子:
1.查询人口数量小于100人的城市
SELECT * FROM city
WHERE Population<100;


2.查询人口数量100w-200w之间的
SELECT * FROM city
WHERE Population>=1000000 AND Population<=2000000;
或者:
SELECT * FROM city
WHERE Population BETWEEN 1000000 AND 2000000;


3.国家代号为CH开头的城市信息(like只能针对字符串)
SELECT * FROM city
WHERE CountryCode LIKE 'CH%';

1.2.4 group by 分组子句+聚合函数应用

-- 聚合函数?
COUNT() -- 计数
SUM()   -- 求和
AVG()   -- 求平均值
MAX()   -- 求最大值
MIN()   -- 求最小值
GROUP_CONCAT() -- 聚合列值


结果集显示特点:必须是1v1,不能是一对多


例子:
1.统计每个个国家的人口总数
SELECT CountryCode,SUM(Population) 
FROM city
GROUP BY CountryCode;


2. 统计中国每个省的人口总数
SELECT District,SUM(Population)
FROM city WHERE CountryCode='CHN'
GROUP BY District;


3. 统计一下中国每个省的城市个数及城市名
SELECT CountryCode,COUNT(NAME),GROUP_CONCAT(NAME)
FROM city WHERE CountryCode='CHN'
GROUP BY District;


4.统计每个国家的城市个数
SELECT CountryCode,COUNT(NAME),GROUP_CONCAT(NAME)
FROM city
GROUP BY CountryCode;

1.2.5 having 后判断

1. -- 统计中国每个省的人口总数,只显示总人口数大于500w的省信息
SELECT District,SUM(Population)
FROM city WHERE CountryCode='CHN'
GROUP BY District
HAVING SUM(Population) >=5000000
ORDER BY SUM(Population) DESC;

1.2.6 order by 排序子句

例子:
1.查询中国城市信息并按人口数从大到小排序输出显示
SELECT * FROM city
WHERE CountryCode='CHN'
ORDER BY Population DESC;


2.查询中国城市信息并按城市名排序输出显示
SELECT * FROM city
WHERE CountryCode='CHN'
ORDER BY NAME;


3.查询中国所有省的总人口数并按总人口数从大到小排序输出
SELECT District,SUM(Population)
FROM city WHERE CountryCode='CHN'
GROUP BY District
ORDER BY SUM(Population) DESC;

1.2.7 limit 分页限制子句

1.查询中国所有省的总人口数并按总人口数从大到小排序输出,显示某几行
SELECT District,SUM(Population)
FROM city WHERE CountryCode='CHN'
GROUP BY District
HAVING SUM(Population) >=5000000
ORDER BY SUM(Population) DESC
LIMIT 10 OFFSET 1;



SELECT District,SUM(Population)
FROM city WHERE CountryCode='CHN'
GROUP BY District
HAVING SUM(Population) >=5000000
ORDER BY SUM(Population) DESC
LIMIT 5;


SELECT District,SUM(Population)
FROM city WHERE CountryCode='CHN'
GROUP BY District
HAVING SUM(Population) >=5000000
ORDER BY SUM(Population) DESC
LIMIT 3,7;

注意:limit 谨慎使用, 500w+的表。
LIMIT 5000000.100  性能极差
一般会改为明确范围的查询

你可能感兴趣的:(select查询语句)