1、select
1.1 作用
獲取mysql中的數據行
1.2 單獨使用select
1.2.1
select @@XXX;獲取參數信息
select @@port; 查端口號
show variables; 查看所有參數
show variables like '%innodb%'; |查參數
1.2.2 select 函數();
select now(); 函數加括號
mysql> select database(); |
庫 |
mysql> select now(); |
時間 |
mysql> select version(); |
版本 |
1.3 sql92標准的使用語法
####### 1.3.1 select語法執行順序
select開始--> from 字句 --> where子句 -->
group by 子句 --> select 后執行條件-->
having子句 --> order by -->limit
desc city |
表結構 |
id |
自增的無關列 |
name |
城市名字 |
countrycode |
所在國家代號 |
district |
中國省的意思 每個是洲的意思 |
populiation |
城市人口數量 |
select * from jyt; |
相對路徑查詢 生產中使用較少 |
select * from root.jyt |
絕對路徑 生產中使用較少 |
select name,populication from jyt; |
查看兩列的內容 |
select name,populication from root.jyt; |
查看兩列的內容 |
where
where |
相當於grep |
說明 |
where配合等值查詢 |
select * from world.city where countrycode='chn'; |
查詢表中的中國城市信息 |
where配合不等值查詢 |
select * from world.city where Population<100; |
人口小於100人的城市 (>,<,<=,>=,<>) |
where配合模糊查詢 |
select * from world.city where CountryCode like 'c%'; |
國家以c開頭 禁止%開頭 |
where配合邏輯連接符(AND or) |
select * from world.city where Population > 10000 AND Population < 20000; |
select * from world.city where population between 10000 and 20000; |
|
select * from world.city where CountryCode='chn' OR CountryCode='usa'; |
select * from world.city where countrycode in ('chn','usa'); |
|
SELECT * FROM world.city WHERE CountryCode='chn' UNION ALL SELECT*FROM world.city WHERE CountryCode='usa'; |
推薦 union 去重 加all不去重 默認去重 |
常用聚合函數
函數 |
例子 |
avg() |
select district,avg(population) from city where countrycode='chn' group by district; |
count() |
select countrycode,count(name) from city group by countrycode; |
sum() |
select countrycode,sum(population) from city group by countrycode ; |
max() |
- |
min() |
- |
group_concat() |
select countrycode,group_concat(district) from city group by countrycode; |
order by
order by |
排序 |
查詢統計總數 |
select district,sum(population) from city where countrycode='chn' group by district; |
查詢統計總數並排序降序 |
SELECT district,sum(population) FROM city WHERE countrycode='chn' GROUP BY district ORDER BY SUM(Population) DESC; |
查詢中國所有的城市,並以人口數降序輸出 |
select*from city where countrycode='chn' order by population desc; |
- |
- |
limit m,n 跳過m行顯示n行 |
limit x offset y 跳過y行顯示x行 |
前5行 |
SELECT*FROM city WHERE countrycode='chn' ORDER BY population DESC LIMIT 5; |
顯示6-10行 |
SELECT*FROM city WHERE countrycode='chn' ORDER BY population DESC LIMIT 5,5; |
顯示6-10行 |
select*from city where countrycode='chn' order by population desc limit 5 offset 5; |
show
- |
- |
SHOW DATABASES; |
查看所以庫 |
SHOW TABLES; |
查看所以表 |
SHOW COLUMNS FROM 表名; |
查看表內所以內容 數字表名要加反應號 例如: 132 |