標籤:mysql
簡單查詢:
select * from tb_name
select field1,field2 from tb_name;
select * from tb_name where qualification ;
例子:select name,age from students where age+1>20;
select name from student where age>20 and gender=‘M‘;
select name from student where age>20 or gender=‘M‘;
select name from student where not age>20 and not gender=‘M‘;
select name from student where not (age>20 or gender=‘M‘);
select name from student where age>=20 and age<=50;或者select name from student where age between 20 and 25;
select name from student where like name ‘Y%‘;
REGEXP 或者RLIKE 後面可以跟上Regex:
select name from student where name relike ‘^[MNY].*$‘;匹配以M或者N或者Y開頭後面任一字元的。
IN的用法:年齡在18、20、25歲的同學名字
select name from student where age in (18,20,25);
顯示student表中cid是null的同學;
select name from student where cid is null;
不是null值的寫法:
select name from student where cid is not null;
根據查詢到的資料排序:
select name from student where cid is not null order by name;
資料存放區的三種格式:堆檔案、循序檔、hash
給欄位取別名;
select name as student_name from student where cid is not null order by name;
限制顯示的行數(limit);
例子: select name as student_name from student as xiaohai limit 2
限制只顯示兩行。
略過前兩行:
select name as student_name from student as xiaohai limit 2,3
略過前兩行顯示三行。
彙總計算:
所有同學的平均年齡:
select avg(age) from student;
select max(age) from student;
select min(age) from student;
select sum(age) from student; 求和
select count(age) from student; 求個數
group by :分組
select age,gender from student group by gender;
按照性別把student表分組(即男,女) 顯示age gender
select avg(age) from student group by gender;
按照性別把表student分組,分別計算出他們的的平均值。
分組的主要目的是彙總:
注意:對group by 條件進行過濾必須用having 它的作用相當於where
select count(cid1) as persons,cid from student group by cid1 having persons>=2;
having的作用是對group by的結果做再次過濾
本文出自 “wangconggao” 部落格,請務必保留此出處http://wangconggao.blog.51cto.com/3253868/1579692
mysql單表查詢