1. 程式人生 > 其它 >Mysql 中select 語句單表查詢

Mysql 中select 語句單表查詢

1.檢視Mysql 內建函式

  1 # 檢視Mysql 內建函式
  2 mysql> help contents;
  3 You asked for help about help category: "Contents"
  4 For more information, type 'help <item>', where <item> is one of the following
  5 categories:
  6    Account Management
  7    Administration
  8    Compound Statements
9 Data Definition 10 Data Manipulation 11 Data Types 12 Functions 13 Functions and Modifiers for Use with GROUP BY 14 Geographic Features 15 Help Metadata 16 Language Structure 17 Plugins 18 Procedures 19 Storage Engines 20 Table Maintenance 21 Transactions
22 User-Defined Functions 23 Utility 24 25 # mysql> help Functions; 26 You asked for help about help category: "Functions" 27 For more information, type 'help <item>', where <item> is one of the following 28 categories: 29 Bit Functions 30 Comparison operators
31 Control flow functions 32 Date and Time Functions 33 Encryption Functions 34 Information Functions 35 Logical operators 36 Miscellaneous Functions 37 Numeric Functions 38 String Functions 39 40 # mysql> help String Functions 檢視字元功能函式 41 You asked for help about help category: "String Functions" 42 For more information, type 'help <item>', where <item> is one of the following 43 topics: 44 ASCII 45 BIN 46 BINARY OPERATOR 47 BIT_LENGTH 48 CAST 49 CHAR FUNCTION 50 CHARACTER_LENGTH 51 CHAR_LENGTH 52 CONCAT #****** 53 CONCAT_WS 54 CONVERT 55 ELT 56 EXPORT_SET 57 EXTRACTVALUE 58 FIELD 59 FIND_IN_SET 60 FORMAT 61 FROM_BASE64 62 HEX 63 INSERT FUNCTION 64 INSTR 65 LCASE 66 LEFT 67 LENGTH 68 LIKE 69 LOAD_FILE 70 LOCATE 71 LOWER 72 LPAD 73 LTRIM 74 MAKE_SET 75 MATCH AGAINST 76 MID 77 NOT LIKE 78 NOT REGEXP 79 OCT 80 OCTET_LENGTH 81 ORD 82 POSITION 83 QUOTE 84 REGEXP 85 REPEAT FUNCTION 86 REPLACE FUNCTION 87 REVERSE 88 RIGHT 89 RPAD 90 RTRIM 91 SOUNDEX 92 SOUNDS LIKE 93 SPACE 94 STRCMP 95 SUBSTR 96 SUBSTRING 97 SUBSTRING_INDEX 98 TO_BASE64 99 TRIM 100 UCASE 101 UNHEX 102 UPDATEXML 103 UPPER 104 WEIGHT_STRING

2.select 單獨用法 單獨使用

#1.select 配合內建函式使用
 #檢視當前使用者
 mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
 #檢視當前mysql 版本
 mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.13    |
+-----------+
1 row in set (0.00 sec)
 #檢視當前時間
 mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2021-11-02 20:10:55 |
+---------------------+
1 row in set (0.00 sec)
#檢視當前在哪個資料庫
mysql> select database();
+------------+
| database() |
+------------+
| NULL       |
+------------+
1 row in set (0.00 sec)
#拼接命令   concat("")
mysql> select concat("hello world!");
+------------------------+
| concat("hello world!") |
+------------------------+
| hello world!           |
+------------------------+
1 row in set (0.01 sec)

mysql> select user,host from mysql.user;    #拼接格式為:mysql.sys@localhost
+-----------+-----------+
| user      | host      |
+-----------+-----------+
| mysql.sys | localhost |
| root      | localhost |
+-----------+-----------+
2 rows in set (0.00 sec)
mysql> select concat(user,"@",host) from mysql.user;   #拼接最終結果
+-----------------------+
| concat(user,"@",host) |
+-----------------------+
| mysql.sys@localhost   |
| root@localhost        |
+-----------------------+
2 rows in set (0.00 sec)

#2.計算
mysql> select 10 * 100;
+----------+
| 10 * 100 |
+----------+
|     1000 |
+----------+
1 row in set (0.00 sec)

mysql> select 100 / 10;
+----------+
| 100 / 10 |
+----------+
|  10.0000 |
+----------+
1 row in set (0.00 sec)

mysql> select 100 - 50;
+----------+
| 100 - 50 |
+----------+
|       50 |
+----------+
1 row in set (0.00 sec)

mysql> select 100 + 30;
+----------+
| 100 + 30 |
+----------+
|      130 |
+----------+
1 row in set (0.00 sec)

mysql> select 100 % 10;
+----------+
| 100 % 10 |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

#3.查詢資料庫引數        
   #檢視全部引數: 504個
| transaction_prealloc_size                                | 4096                                   
| transaction_write_set_extraction                         | OFF                                   
| tx_isolation                                             | REPEATABLE-READ                       
| tx_read_only                                             | OFF                                   
| unique_checks                                            | ON                                     
| updatable_views_with_limit                               | YES                                   
| version                                                  | 5.7.13                                 
| version_comment                                          | Source distribution                   
| version_compile_machine                                  | x86_64                                 
| version_compile_os                                       | Linux                                 
| wait_timeout                                             | 28800                         
| warning_coun                                            | 0                                   
504 rows in set (0.00 sec) #版本不一樣,總的引數數量也不一樣

#檢視只記住引數部分時命令  用Like 
mysql> show variables like "%trx%";
+--------------------------------+-------+
| Variable_name                  | Value |
+--------------------------------+-------+
| innodb_api_trx_level           | 0     |
| innodb_flush_log_at_trx_commit | 1     |
+--------------------------------+-------+
2 rows in set (0.00 sec)


mysql> select @@datadir;           #檢視資料目錄
+-------------------+ 
| @@datadir               
+-------------------+
| /home/mysql/data/ |
+-------------------+
1 row in set (0.00 sec)

mysql> select @@socket;     #檢視socket 
+-----------------+
| @@socket        |        
+-----------------+
| /tmp/mysql.sock |
+-----------------+
1 row in set (0.00 sec)
mysql> select @@port;    #檢視port
+--------+
| @@port |                
+--------+
|   3306 |
+--------+
1 row in set (0.00 sec)

mysql> select @@innodb_flush_log_at_trx_commit;     
+----------------------------------+
| @@innodb_flush_log_at_trx_commit |
+----------------------------------+
|                                1 |
+----------------------------------+
1 row in set (0.00 sec)

3.select 標準用法 (配合其它子句使用)

#1.單表
預設執行順序(嚴格按照執行順序)
select * 
1. from      表1,表2,l...
2. where     過濾條件1 過濾條件2 ...
3. group by  條件列1 條件列2
4. select_list 
5. having    過濾條件1 過濾條件2 ...
6. order by  條件列1 條件列2
7. limit     限制條件

先匯入資料world.sql
[root@shell ~21:01:45]# mysql -uroot -p123456 <world.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.

mysql> show databases;     #檢視所有庫
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| world              |
+--------------------+
5 rows in set (0.00 sec)

mysql> use world;       #進入world庫
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;      #檢視world庫中所有表
+-----------------+
| Tables_in_world |
+-----------------+
| city            |
| country         |
| countrylanguage |
+-----------------+
3 rows in set (0.00 sec)

mysql> desc city;     #檢視city 表結構
+-------------+----------+------+-----+---------+----------------+
| Field       | Type     | Null | Key | Default | Extra          |
+-------------+----------+------+-----+---------+----------------+
| ID          | int(11)  | NO   | PRI | NULL    | auto_increment |
| Name        | char(35) | NO   |     |         |                |
| CountryCode | char(3)  | NO   | MUL |         |                |
| District    | char(20) | NO   |     |         |                |
| Population  | int(11)  | NO   |     | 0       |                |
+-------------+----------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

mysql> show create table city;       #檢視city 建表語句
| Table | Create Table                                                                                 CREATE TABLE `city` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `Name` char(35) NOT NULL DEFAULT '',
  `CountryCode` char(3) NOT NULL DEFAULT '',
  `District` char(20) NOT NULL DEFAULT '',
  `Population` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`ID`),
  KEY `CountryCode` (`CountryCode`),
  CONSTRAINT `city_ibfk_1` FOREIGN KEY (`CountryCode`) REFERENCES `country` (`Code`)
) ENGINE=InnoDB AUTO_INCREMENT=4080 DEFAULT CHARSET=latin1 |

#1. select 配合from 子句使用
 語法:select 列 from 表; 類似 於cat /etc/passwd
  select * from 表 ;
---查詢表中所有資料
 select *  from world.city;
 select Id,name,countycode,district,population from world.city;
---查詢部分列值    類似於awk 取列
select name,population from world.city;

#2. select+from +where   其中where類似於grep 過濾
 #a.where 配合比較符來使用,例如: > = < >= <= !=
 查詢city表中,所有中國城市資訊
 mysql> select * from world.city where countrycode='CHN';
 查詢city表中,人口數小於1000的城市
 mysql> select * from world.city where population <1000;    
 #b.where 配合like 進行模糊查詢   適合於字串型別,不能like數字等
 查詢city表中,國家代號為CH開頭的城市資訊
 select * from world.city where countrycode like 'CH%';
 #注意:like語句在使用時,切記不可出現前面帶%的模糊查詢,因為不走索引
 #c.where 配合邏輯連結符  AND OR XOR(非)
 查詢中國人口數小於500萬的城市資訊
 select * from world.city where countrycode='CHN' AND population <5000000;
 查詢中國或者美國的城市資訊
 select * from world.city where countrycode='CHN' OR countrycode='USA';
 
 #3.select +from +where +group by   #分組後一定要顯示該列,
     select CountryCode, count(id/name)from world.city  group by CountryCode;
   #a.gtoup by 配合聚合函式  
   max()           #求最大值
   min()           #求最小值
   avg()           #求平均值
   count()         #統計個數
   sum()           #求和
   group_concat()  #列轉行
   說明:碰到group by  必然會有聚合函式
   統計每個國家的總的人口數
   select CountryCode, count(id/name)from world.city  group by CountryCode;
   統計中國,每個省的總人口數
   select District,sum(population) from city  where CountryCode='CHN' group by District;
   統計中國,每個省總人口,城市個數,城市名列表
   select District,sum(population),count(id) from city  where CountryCode='CHN' group by District;
 
#4.select +from +where +group by+having   #having 屬於後過濾,以group by 為中間,where 為前過濾.
  having 作用:與where 子句型別,having 屬於後過濾
  場景:需要在group by +聚合函式後,再做過濾時使用
   統計中國,每個省的總人口數並只顯示總人口數大於500萬
   select  District,sum(population)\
   from city\
   where CountryCode='CHN'\
   group by District\
   having sum(population) >5000000;

#5.select +from +where +group by+having +order by    
  order by 作用: 排序作用
  統計中國,每個省的總人口數,只顯示總人口數大於500萬並以總人口數以小到大進行排序
  select  District,sum(population)\
  from city\
  where CountryCode='CHN'\
  group by District\
  having sum(population) >5000000\
  order by sum(population);
  統計中國,每個省的總人口數,只顯示總人口數大於500萬並以總人口數以大到小進行排序
  select District,sum(population)\
  from city\
  where CountryCode='CHN'\
  group by District\
  having sum(population) >5000000\
  order by sum(population) desc;
  
#6.select +from +where +group by+having +order by+limit
  limit作用:分頁顯示結果集
  統計中國,每個省的總人口數,只顯示總人口數大於500萬並以總人口數以大到小進行排序,只顯示前5名
  select District,sum(population)\
  from city\
  where CountryCode='CHN'\
  group by District\
  having sum(population) >5000000\
  order by sum(population) desc\
  limit 5;
  
  統計中國,每個省的總人口數,只顯示總人口數大於500萬並以總人口數以大到小進行排序,只顯示6-10名
  select District,sum(population)\
  from city\
  where CountryCode='CHN'\
  group by District\
  having sum(population) >5000000\
  order by sum(population) desc\
  limit 5,5;  #意思為跳過5(左邊開始第2個5為跳),再顯示5,及為6-10名
  
  select District,sum(population)\
  from city\
  where CountryCode='CHN'\
  group by District\
  having sum(population) >5000000\
  order by sum(population) desc\
  limit 5 offset 5;  #意思為跳過5,再顯示5,及為6-10名
  
  顯示3-5行
  limit 2,3 或 limit 3 offset 2
  
  #7.select 的別名
  #a.列別名(在select 後面就是列),或者不接AS 直接別名也行.
  作用:
  1.做為好看
  2.group by having order by 子句呼叫,where 子句不行(因為執行順序原因)
  select District AS '城市名稱', sum(population) AS '總人口數'\
  from city\
  where CountryCode='CHN'\
  group by District\
  having sum(population) >5000000\
  order by sum(population) desc\
  limit 5 offset 5;
  
   #別名子名呼叫
  例子:
  select District AS '城市名稱', sum(population) AS '總人口數'\
  from city\
  where CountryCode='CHN'\
  group by 城市名稱\
  having 總人口數 >5000000\
  order by 總人口數 desc\
  limit 5 offset 5;
  
  #b.表別名
   作用:全域性呼叫定義表別名
   select a.sno AS '學號',a.sname AS '姓名',group_concat(c.cname) AS '課程名稱'\
   from student AS a\
   join sc AS b\
   on a.sno=b.sno\
   join course AS c\
   where a.sname='zhang3'\
   group by a.sno,a.sname;
Do everything well