mysql基礎(六)select語句

來源:互聯網
上載者:User

標籤:mysql

單表查詢:    SELECT        [ DISTINCT ]        [SQL_CACHE | SQL_NO_CACHE]        select_expr [,select_expr ...]        [FROM table_references [ WHERE where_condition ]        [GROUP BY {col_name | expr |}] [HAVING where_condition] [ORDER BY [ASC | DESC]]  DISTINCT:資料去重SQL_CACHE:指定存入緩衝SQL_NO_CACHE:指定進行緩衝    WHERE子句:指定條件,實現過濾功能過濾條件:    算數運算:+   -   *   /   %  >  <  >=  <=    邏輯運算:AND:與運算  OR:或運算  NOT:非運算    字元比較:=  !=        列表元素比較:IN (元素1,元素2,元素3,...)    IS NULL:空    IS NOT NULL:非空    LIKE:模糊比對,能不用盡量不用    RLIKE:基於正則的模糊比對,能不用盡量不用    GROUP BY子句:更具指定的條件把匹配的結果進行分組,實現"彙總"運算        常用函數:    sum(條件)#求和                max(條件)#最大值        min(條件)#最小值        avg(條件)#平均值        count(條件)#統計記錄數    HAVING:對GROUP BY之後的結果進行過濾樣本:    mysql> CREATE DATABASE mydb;#建立測試資料庫    mysql> USE mydb;    mysql> CREATE TABLE test (id int(50),name varchar(50),qq int(15),gender char(2));#建立測試表    mysql> INSERT INTO test VALUES(1,‘zhangshan‘,12345,‘F‘);    #插入測試資料    mysql> INSERT INTO test VALUES(2,‘lisi‘,123142,‘F‘);    mysql> INSERT INTO test VALUES(3,‘zsf‘,124312,‘M‘);    mysql> INSERT INTO test VALUES(4,‘yyy‘,124312,‘F‘);    mysql> INSERT INTO test VALUES(5,‘ytt‘,124312,‘M‘);    單表查詢樣本:    mysql> SELECT * FROM test WHERE id>2 AND id<5;#查詢ip大於2小於5的資料    mysql> SELECT * FROM test WHERE id>2 GROUP BY gender;#對性別進行分組    mysql> SELECT sum(id),gender FROM test GROUP BY gender;#對性別進行分組,並求他們的id之和    mysql> SELECT * FROM test GROUP BY gender HAVING id>2;#對性別進行分組,顯示id大於2的組        多表關聯查詢:    mysql> CREATE TABLE test2 (emain varchar(200),age int(10));#建立測試環境    mysql> INSERT INTO test2 values(‘[email protected]‘,10);    mysql> INSERT INTO test2 values(‘[email protected]‘,15);    mysql> INSERT INTO test2 values(‘[email protected]‘,20);    mysql> SELECT test.id,test2.age FROM test,test2 WHERE test.id>2 AND test2.age>10;    test.id#第一章表的id欄位test2.age#第二章表的age欄位test#第一章表 表名test2#第二章表 表名test.id>2 AND test2.age#拼接顯示test的id欄位和test2的age欄位,但是test的id欄位必須大於2並且test2的age欄位必須大於10    mysql> SELECT * FROM test,test2 WHERE test.id>2 AND test2.age>10;    #拼接顯示test,test2的所有欄位,但是test的id欄位必須大於2並且test2的age欄位必須大於10


本文出自 “自動化營運” 部落格,請務必保留此出處http://hongchen99.blog.51cto.com/12534281/1933300

mysql基礎(六)select語句

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.