MySQL必知必會---檢索資料

來源:互聯網
上載者:User

標籤:mysql

  • select 語句
  • 檢索單個列
  • 檢索多個列
  • 檢索所有列
  • 檢索不同的行
  • 限制結果
  • 使用完整表名
  • 1.select語句
    使用select檢索表資料,必須至少給出兩條資訊-----想選擇什麼以及從什麼地方選擇。

    2.檢索單個列
    MariaDB [test]> select age from user;
    +------+
    | age |
    +------+
    | 22 |
    | 25 |
    | 56 |
    | 14 |
    | 36 |
    | 68 |
    | 45 |
    | 17 |
    | 33 |
    | 27 |
    | 29 |
    | 70 |
    | 24 |
    +------+

    注意: 預設情況下查詢結果並未進行排序,結束SQL語句必須以分號(;)分隔,SQL語句不區分大小寫。

    建議:為了方便閱讀以及調試建議把sql語句分行。

    3.檢索多個列
    MariaDB [test]> select *
    -> from user;
    +----+------+------+----------+
    | id | sex | age | province |
    +----+------+------+----------+
    | 1 | 1 | 22 | 北京 |
    | 2 | 0 | 25 | 廣東 |
    | 3 | 0 | 56 | 天津 |
    | 4 | 1 | 14 | 北京 |
    | 5 | 0 | 36 | 廣東 |
    | 6 | 1 | 68 | 湖南 |
    | 7 | 1 | 45 | 北京 |
    | 8 | 1 | 17 | 河北 |
    | 9 | 2 | 33 | 天津 |
    | 10 | 1 | 27 | 湖南 |
    | 11 | 1 | 29 | 北京 |
    | 12 | 2 | 70 | 廣東 |
    | 13 | 0 | 24 | 北京 |
    +----+------+------+----------+
    13 rows in set (0.00 sec)

    1. 檢索所有列
      提示:萬用字元(*)表示所有列

    MariaDB [test]> select *
    -> from user
    -> ;
    +----+------+------+----------+
    | id | sex | age | province |
    +----+------+------+----------+
    | 1 | 1 | 22 | 北京 |
    | 2 | 0 | 25 | 廣東 |
    | 3 | 0 | 56 | 天津 |
    | 4 | 1 | 14 | 北京 |
    | 5 | 0 | 36 | 廣東 |
    | 6 | 1 | 68 | 湖南 |
    | 7 | 1 | 45 | 北京 |
    | 8 | 1 | 17 | 河北 |
    | 9 | 2 | 33 | 天津 |
    | 10 | 1 | 27 | 湖南 |
    | 11 | 1 | 29 | 北京 |
    | 12 | 2 | 70 | 廣東 |
    | 13 | 0 | 24 | 北京 |
    +----+------+------+----------+
    13 rows in set (0.00 sec)

    5.檢索不同的行

    注意關鍵字(DISTINCT)

    MariaDB [test]> select distinct age from user;
    +------+
    | age |
    +------+
    | 22 |
    | 25 |
    | 56 |
    | 14 |
    | 36 |
    | 68 |
    | 45 |
    | 17 |
    | 33 |
    | 27 |
    | 29 |
    | 70 |
    | 24 |
    +------+
    13 rows in set (0.00 sec)

    提示:使用distinct關鍵字,必須直接放在列名之前。

    6.限制結果
    關鍵字(limit)

    MariaDB [test]> select age from user
    -> limit 5;
    +------+
    | age |
    +------+
    | 22 |
    | 25 |
    | 56 |
    | 14 |
    | 36 |
    +------+
    5 rows in set (0.00 sec)

    注意下面的語句:

    MariaDB [test]> select age
    -> from user
    -> limit 5,5;
    +------+
    | age |
    +------+
    | 68 |
    | 45 |
    | 17 |
    | 33 |
    | 27 |
    +------+
    5 rows in set (0.00 sec)

    提示:檢索結果的第一行為行0,而不是行1, limit 5,5 則是表示從第5行開始的5行!!!

    7.使用完整表名
    MariaDB [test]> select user.age
    -> from user
    -> limit 3,4;
    +------+
    | age |
    +------+
    | 14 |
    | 36 |
    | 68 |
    | 45 |
    +------+
    4 rows in set (0.00 sec)

    提示:使用完整表名可以更加直觀的看出操作的是那個表,或者是那個資料庫。

    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.