MYSQL基礎知識小盲區

來源:互聯網
上載者:User

標籤:use   auto   當前日期   group by   div   post   regex   time()   primary   

MYSQL必會的知識

  1. 命令列 啟動mysql:     mysql  -u使用者名稱 -p密碼
  2. 顯示表中的各列詳細資料:    show columns form tablename  等價於  desc tablename
  3. 查看所有show命令:    help show
  4. 查詢資料時未使用排序,顯示的結果是沒有順序意義的
  5. 去掉重複的行使用distinct:    select distinct id from user
  6. limit關鍵字的使用:   select * from user limit 5; 查詢出不多於5行資料        select * from user  limit 5,5;  從第五行資料開始查詢,返回5條記錄  (即5到10 的記錄)
  7. 使用order by 進行排序,預設的是升序排列:    select *from user order by age desc,name ; 按照年齡降序排列,年齡相同時按照姓名升序排列
  8. 查詢name欄位為null的記錄:  select * from user where name is null;
  9. 關鍵字 and 和 or,優先處理and,然後再處理or旁邊的語句,並且每個只執行一次。
  10. 萬用字元:  %  匹配一個或多個字元    select * from user where name=‘a%‘; 查詢所有name以a開頭的記錄  這裡的查詢條件區分大小寫
  11. 查詢條件為‘%’時 select * from user where name=‘%‘;不會匹配name為null的記錄
  12. 萬用字元 “-” 只匹配一個字元
  13. mysql的Regex關鍵字 regexp:  select *from user where age regexp ‘10‘ ; 查詢所有年齡為10的記錄
  14. Regex符號“.” 一個點表示匹配任意一個字元  select * from user where age regexp ‘.0‘;查詢出所有年齡為整10的記錄,可以是10,20,30,等等。
  15. 就加入需要使用Regex去匹配.點這個符號,則需要轉義,使用  \\. 去匹配
  16. 符號 ^ 的作用有兩個:一個是表示指定串的開始,另一個是在集合中表示否定集合
  17. 符號 $ 的作用: 運算式的結束
  18. 拼接字串使用關鍵字contact,    contact(a,‘(‘,b,‘)‘)
  19. RTrim(name) 去掉name這一列的右邊的所有空格
  20. LTrim(name) 去掉name這一列的左邊的所有空格
  21. Trim(name) 去掉name這一列的兩邊的全部空格
  22. 函數left() ,使用樣本:  select left(‘zhangsan‘,1); 返回字串zhangsan的一個字元z
  23. 函數Locate(),使用樣本: select locate(‘a‘,‘bbabc‘); 返回第一個字串在第二個字串中出現的位置 3
  24. 函數Soundex() 使用樣本: select * from user where Soundex(name) = Soundex(‘li‘);返回所有name的讀音和 li 相似的記錄
  25. 查看系統時間的函數: select CurDate() ;查看當前日期 ,select CurTime() ; 查看目前時間
  26. 函數max()和函數min()都會忽略掉職位null的行
  27. 聚集合函式中可以使用distinct ,例如: select  avg(distinct age) as ageavg from user;
  28. 關鍵字group by必須在where後和order by 之前
  29. where用來過濾行,having用來過濾分組 例如:  select id ,count(*) as nums from user group by  id having count(*)>=2;
  30. 關鍵字union連結查詢語句,至少由兩條select語句組成,多個之間都是用union串連(使用union時會自動去掉重複的行記錄,如果不想去掉就使用 union all)
  31. 全文本搜尋 Match() 指定搜尋的列,Aganist()指定要使用的運算式
  32. 全文檢索搜尋樣本:select  text from  mail where Match(text) Aganist(‘right‘); 其中text是定義表的時候指定的索引欄位,FULLTEXT(text)定義
  33. 建立表,id自增時設定 id int(10) primary key auto_increment
  34. 修改列名和資料類型: alter table 表名 change column 舊列名 新列名 列類型;  alter table user change column name username varchar(30); 將user表中的name列變為username.
  35. 添加列:  alter table user ADD hobby varchar(30);為user表中添加新列hobby,類型為varchar
  36. 刪除列: alter table user DROP column age ; 刪除user表中age那一列
  37. 重新命名表: rename table 舊錶名 to 新表名;  alter table user to person; 將名為user的表改名為 person.

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.