簡單查詢
查詢所有字段
select * from 表名;
例:select * from students;
查詢指定字段
select 列1,列2,... from 表名;
例:select name from students;
使用 as 給字段起別名
select id as 序號, name as 名字, gender as 性別 from students;
可以通過 as 給表起別名
-- 如果是單錶查詢 可以省略表明
select id, name, gender from students;
-- 表名.欄位名
select students.id,students.name,students.gender from students;
-- 可以通過 as 給表起別名
select s.id,s.name,s.gender from students as s;
消除重複行
在select後面列前使用distinct可以消除重複的行
select distinct 列1,... from 表名;
例:select distinct gender from students;
條件使用where子句對錶中的資料篩選,結果為true的行會出現在結果集中
語法如下:
select * from 表名 where 條件;
例:select * from students where id=1;
where後面支援多種運算子,進行條件的處理
比較運算子
邏輯運算子
模糊查詢
範圍查詢
空判斷比較運算子
等於: =
大於: >
大於等於: >=
小於: <
小於等於: <=
不等於: != 或 <>
例1:查詢編號大於3的學生
select * from students where id > 3;
例2:查詢編號不大於4的學生
select * from students where id <= 4;
例3:查詢姓名不是「黃蓉」的學生
select * from students where name != '黃蓉';
例4:查詢沒被刪除的學生
select * from students where is_delete=0;
邏輯運算子
andor
not例5:查詢編號大於3的女同學
select * from students where id > 3 and gender=0;
例6:查詢編號小於4或沒被刪除的學生
select * from students where id < 4 or is_delete=0;
模糊查詢
like
%表示任意多個任意字元
_表示乙個任意字元
例7:查詢姓黃的學生
select * from students where name like '黃%';
例8:查詢姓黃並且「名」是乙個字的學生
select * from students where name like '黃_';
例9:查詢姓黃或叫靖的學生
select * from students where name like '黃%' or name like '%靖';
範圍查詢
in表示在乙個非連續的範圍內
例10:查詢編號是1或3或8的學生
select * from students where id in(1,3,8);
between ... and ...表示在乙個連續的範圍內
例11:查詢編號為3至8的學生
select * from students where id between 3 and 8;
例12:查詢編號是3至8的男生
select * from students where (id between 3 and 8) and gender=1;
空判斷注意:null與''是不同的
判空is null
例13:查詢沒有填寫身高的學生
select * from students where height is null;
判非空is not null
例14:查詢填寫了身高的學生
select * from students where height is not null;
例15:查詢填寫了身高的男生
mysql分頁和條件查詢 資料庫 條件查詢和分頁
productdao dao newproductdao 目的 就是想辦法封裝乙個pagebean 並返回 pagebean pagebean newpagebean 1 當前頁private int currentpage pagebean.setcurrentpage currentpage 2...
Xutils 按條件查詢
查parententity db.findbyid parent.class,parent.getid parententity db.findfirst entity 通過entity的屬性查詢 listlist db.findall entity 通過entity的屬性查詢 listlist d...
資料庫mysql基礎查詢之條件查詢
有時候,我們想查詢出特定條件的資料,那該如何查詢呢?答案是使用where關鍵字 我們還是以如下的表為例 where關鍵字後面可以新增條件表示式。舉例子 上表中,我們要查詢出last name為 k ing的資料 select from employeeswhere last name k ing 通...