MySQL--資料的尋找

來源:互聯網
上載者:User

標籤:mysql--資料的尋找

- 基本查詢| 查詢所有列 select * from students;| 指定條件查詢 select * from students where is_delete=1;| 查詢指定列 select name, gender from students;| 欄位的順序 select gender, name from students;| as的使用# 在查詢指定列的同時,修改欄位名顯示select 欄位名 as 顯示名 from 列表;select name as 姓名, gender as 性別 from students;# 在sql語句中暫時修改表名,減少代碼量select students.name, students.gender from students;select s.name, s.gender from students as;| 消除重複行 distinct 欄位 select gender from students; select distinct gender from students;- 條件| 比較子>select * from students where age > 18;<=select * from students where age <= 18;| 邏輯運算子andselect * from students where age>18 and gender=2;orselect * from students where age>18 or height>=180;notselect * from students where not (age>18 and gender=2);| 模糊查詢--like"%"尋找以某字元開頭的名字select name from students where name like "小%";"_"  查詢有幾個字的名字select name from students where name like "__";select name from students where name like "___";--rlike(支援正則)select name from students where name rlike "^周";select name from students where name rlike "^周.*倫$";| 範圍查詢-- in 表示在一個非連續範圍內select name from students where age in (18,34);-- not inselect name , age from students where age not in (18,34);【附:左右兩個數字必須是表內含有的】-- between ...and ... 表示在一個連續的範圍內select * from students where age between 18 and 34;-- not between ...and.. 不需要加括弧select * from students where age not between 18 and 34;【左右兩個數字沒有限制】| 空判斷--判斷is null  判斷為空白select * from students where height is null;--判斷is not null 判斷不為空白select * from students where height is not null;- 排序order by 欄位acs 升序 (沒有asc也預設升序) select * from students where (age between 18 and 34) and gender=1 order by age; select * from students where (age between 18 and 34) and gender=1 order by age asc;desc 降序 select * from students where (age between 18 and 34) and gender=2 order by height desc;# 哪個排序操作在前,就先執行哪個年齡從小到大,身高從高到矮的排序select * from students order by age asc, height desc;先對身高降序,如果體重相同,年齡升序select * from students where (age between 18 and 34) and gender=2 order by height desc, age asc;- 彙總函式總數 (統計數量) count select * from students where gender=1; select count(*) as 男生的人數 from students where gender=1; select count(*) as 女生的人數 from students where gender=2; # *的效率比選擇的欄位要高 select count(name) as 男生的人數 from students where gender=1;最大值 max select age from students; select max(age) from students; select max(height) from students where gender=2;最小值 min select sum(age) from students;求和 sum select sum(age) from students;平均值 avg select sum(age)/count(*) from students; select avg(age) from students;四捨五入 round(數字,保留位元) select round(avg(age) ,2) from students where gender=1; 計算所有人的年齡,保留兩位小數 select round(avg(age) ,2) from students;- 分組--group by選擇欄位,資料去重後分成一組按照性別分組select * from students group by gender; # 錯誤select gender from students group by gender;--group_concat取出分組對應的其他欄位下的資料select gender, group_concat(name) from studentsgroup by gender;select gender, group_concat(name, age) from studentsgroup by gender;--having附加查詢每種性別的平均年齡select gender, avg(age) from studentsgroup by gender;查詢平均年齡超過30歲的性別select gender, group_concat(name) from studentsgroup by gender having avg(age)>30;--with rollup--在最後新增一行,記錄當前列所有數的總和select gender, count(*) from students group by gender with rollup;-分頁(limit start, count     0表示第一個)1-操作查詢前5個資料 select * from students limit 0,5; 查詢id4-8的資料 select * from students limit 3,5; 每頁顯示2個,第1,2,3個頁面 select * from students limit 0,2;----每頁數量*(頁數-1) select * from students limit 2,2; select * from students limit 4,2; 每頁顯示2個,顯示第3頁的資訊,按照年齡從小到大排序 select * from students limit 2*(4-1),2; 失敗 select * from students limit 6,2 order by age asc; 失敗  select * from students order by age asc limit 6,2 ;2-加速尋找- 連表查詢-inner join ...on..內串連,取交集--對inner join ...on...的理解select * from goods inner join goods_brandson goods.id=goods_brands.id;分析:從goods某個範圍內中尋找資料,而這個範圍是含有goods_brands的交集,且必須滿足on後面的條件--按照要求顯示欄位select goods.name from goods inner join goods_brandson goods.id=goods_brands.id;--顯示要求欄位的同時,插入表中其他欄位資料select b.*,g.name from goods as g inner join goods_brands as bon g.id=b.id;--給資料表起短名字select g.name from goods as g inner join goods_brands as bon g.id=b.id;-left join ...on...左串連以左邊表為主--左串連,以左表為基礎,匹配另一個表資料,--無資料用NULL表示select b.*,g.name from goods as g left join goods_brands as bon g.id=b.id;--清除NULL的資料select b.*,g.name from goods as g left join goods_brands as bon g.id=b.id where b.name is not null;-自關聯(待補充)create table areas(    aid int primary key,    atitle varchar(20),    pid int);查詢所有省份 select * from areas where pid is null; 查詢山東省的所有市 select * from areas as city inner join areas as province on city.pid=province.aid where province.atitle="山東省";資料庫匯入資料命令——source 檔案.sql- 子查詢- 標量子查詢返回一個資料(一行一列)select * from students height > avg(height); 失敗 select * from students where height > select avg(height) from students;  失敗 select * from students where height > (select avg(height) from students);- 列級子查詢返回的結果是一列(一列多行)select name from classes where id in (select cls_id from students);- 行級子查詢返回結果是一行(一行多列)select * from students where (height,age) = (select max(height),max(age) from students);- 組合Union-- 組合,自動處理重合    select nickname from A union select name from B-- 組合,不處理重合    select nickname from A union all select name from B

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.