MySQL —— 簡單查詢與按條件查詢

來源:互聯網
上載者:User

標籤:set   sql表中   部分   order   集合   dma   tween   from   讀卡機   

MySQL —— 簡單查詢與按條件查詢

在MySQL中從資料表中查詢資料的基本語句時select語句。
  select語句基本文法格式:
      select 查詢內容
      from 表名
      where 運算式
      group by 欄位名
      having 運算式
      order by 欄位名
      limit 記錄數
每一個select語句由多個子句組成。

1. from 表名 指定是從那張表中查詢


2. select 查詢內容

查詢所有欄位 select * from 表名;
*萬用字元表示所有欄位
mysql> select * from test;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    1 | A    |    4 |
|    2 | B    |    7 |
|    3 | C    |    5 |
|    4 | D    |   12 |
+------+------+------+
4 rows in set (0.06 sec)

查詢部分欄位 select 欄位名 from 表名;
mysql> select name from test;
+------+
| name |
+------+
| A    |
| B    |
| C    |
| D    |
+------+
4 rows in set (0.00 sec)

在MySQL表中,每個欄位的資料可以當做變數處理。
查詢所需的某個欄位資料處理後的結果:select 欄位處理方式 from 表名;
mysql> select age-3  from test;
+-------+
| age-3 |
+-------+
|     1 |
|     4 |
|     2 |
|     9 |
+-------+
4 rows in set (0.11 sec)
 

3. where 運算式 (按條件查詢)
在MySQL的表查詢時,往往並不是需要將所有內容全部查出,而是根據實際需求,查詢所需資料
select 查詢內容 from 表名 where 運算式;

在MySQL語句中,條件運算式是指select語句的查詢條件,在where子句中可以使用關係運算子連
接運算元作為查詢條件對資料進行選擇。
關係運算子:
=   等於
<>  不等於
!=  不等於
<   小於
>   大於
<=  小於等於
>=  大於等於

例如查詢年齡大於5的資訊
mysql> select * from test where age > 5;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    2 | B    |    7 |
|    4 | D    |   12 |
+------+------+------+
2 rows in set (0.04 sec)

帶in的關鍵字查詢
查詢某個指定集合內的記錄 select 查詢內容 from 表名 where 條件 in(指定內容);
mysql> select * from test where age in (5, 12);
+------+------+------+
| id   | name | age  |
+------+------+------+
|    3 | C    |    5 |
|    4 | D    |   12 |
+------+------+------+
2 rows in set (0.00 sec)

帶有between and 關健字查詢
查詢某個在給定範圍內的記錄 select 查詢內容 from 表名 where 條件 between 值1 and 值2;
mysql> select * from test where age between 5 and 12;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    2 | B    |    7 |
|    3 | C    |    5 |
|    4 | D    |   12 |
+------+------+------+
3 rows in set (0.07 sec)

查詢某些為空白NULL  或 非空的記錄 select 查詢內容 from 表名 where 條件 is(not) NULL;
mysql> select * from test where age is NULL;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    6 | F    | NULL |
+------+------+------+
1 row in set (0.00 sec)

在查詢時過濾掉重複的值:select distinct 欄位名 from 表名;欄位名表示要過濾重複記錄的欄位
mysql> select num from a;
+------+
| num  |
+------+
|    5 |
|   10 |
|   15 |
|   10 |
|   15 |
|    5 |
|   10 |
+------+
7 rows in set (0.00 sec)

mysql> select distinct num from a;
+------+
| num  |
+------+
|    5 |
|   10 |
|   15 |
+------+
3 rows in set (0.00 sec)
在使用distinct指定多個欄位時,只有被指定的這些欄位的值都相同,才會被認為是重複的

在查詢具有一類相同特徵的資料時,需要用到模糊查詢,這是就需要使用like關鍵字
select 查詢內容 from 表名 where 內容 (not) like ‘匹配的字串’
百分比符號萬用字元 %,表示匹配任意長度的任一字元串
mysql> select name from name;
+------+
| name |
+------+
| 1112 |
| 1122 |
| 1222 |
| 2111 |
+------+
4 rows in set (0.00 sec)

mysql> select name from name where name like ‘11%‘;
+------+
| name |
+------+
| 1112 |
| 1122 |
+------+
2 rows in set (0.00 sec)

底線萬用字元 _ :表示匹配任意單個字元,如果需要匹配多個字元,則需要使用多個 _
mysql> select name from name where name like ‘11__‘;
+------+
| name |
+------+
| 1112 |
| 1122 |
+------+
2 rows in set (0.00 sec)

如果需要查詢帶有 % 或 _ 的資料,由於 % 和 _ 是萬用字元,則需要使用 \ 進行轉義
\% 表示 %,\_ 表示 _

有時在查詢時為了查詢結果更加精確,需要多個限條件,這時就需要 and(&&) 來串連條件
mysql> select cat_id, cat_name, parent_id from category;
+--------+---------------------------+-----------+
| cat_id | cat_name                  | parent_id |
+--------+---------------------------+-----------+
|      1 | 手機類型                  |         0 |
|      2 | CDMA手機                  |         1 |
|      3 | GSM手機                   |         1 |
|      4 | 3G手機                    |         1 |
|      5 | 雙模手機                  |         1 |
|      6 | 手機配件                  |         0 |
|      7 | 充電器                    |         6 |
|      8 | 耳機                      |         6 |
|      9 | 電池                      |         6 |
|     11 | 讀卡機和記憶卡            |         6 |
|     12 | 儲值卡                    |         0 |
|     13 | 小靈通/固話儲值卡         |        12 |
|     14 | 移動手機儲值卡            |        12 |
|     15 | 聯通手機儲值卡            |        12 |
+--------+---------------------------+-----------+
14 rows in set (0.00 sec)

mysql> select cat_id, cat_name, parent_id from category
    -> where cat_id > 7 and parent_id = 6;
+--------+-----------------------+-----------+
| cat_id | cat_name              | parent_id |
+--------+-----------------------+-----------+
|      8 | 耳機                  |         6 |
|      9 | 電池                  |         6 |
|     11 | 讀卡機和記憶卡        |         6 |
+--------+-----------------------+-----------+
3 rows in set (0.05 sec)

有時在查詢時,只需要資料滿足某些條件中的某一個,這時就需要使用 or(||) 來串連條件
ysql> select cat_id, cat_name, parent_id from category where cat_id = 3 or cat_id = 9;
+--------+-----------+-----------+
| cat_id | cat_name  | parent_id |
+--------+-----------+-----------+
|      3 | GSM手機   |         1 |
|      9 | 電池      |         6 |
+--------+-----------+-----------+
2 rows in set (0.02 sec)

在查詢時,and 的優先順序高於 or

 

MySQL —— 簡單查詢與按條件查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.