查詢表中資料(簡單查詢)
- 往表中添加資料,文法格式:
-
-- 往text3表中添加資料 insert into text3 values("11","22","33","44");
-
- 簡單查詢
- 執行順序:
- from–>where–>group by -->having–>select–>order by
- 查詢指定表中所有資料
-
-- 查詢指定表中所有資料 select * from 表名; select * from text3;
-
-- 查詢部分字段,字段名中間用逗号隔開 select 字段名,字段名 from 表名; select name,price from text3;
-
-- 可以用as給字段名起一個别名,友善檢視 如: select 字段名 as "新的字段名", 字段名 as "新的字段名" from text3 select name as "書名", price as "價格" from text3
- 使用去重關鍵字 distinct
-
-- 去除重複操作,将不重複的值篩選出來 select distinct 字段名 from 表名; select distinct price from text3;
- 執行順序:
- 運算查詢:把text3表中的價格添加100
-
select name,price + 100 from text3
-
條件查詢
如果查詢語句中沒有設定條件,就會查詢所有的行資訊,實際應用中,一定要指定查詢條件,對記錄進行過濾
- 文法格式:
-
-- 先取出表中的每條資料,滿足條件的資料就傳回,不滿足就過濾掉 select 列名 from where 條件表達式
-
- 比較運算符
-
運算符 說明 ,>, < ,<=,>=,=,<>,!= 大于,小于,大于(小于)等于,不等于 (<>這個也為不等于符号) between…and… 顯示在某一個區間的值
例如:100-200之間:between 100 and 200
in(集合) 集合表示多個值,使用逗号分隔,例如:name in (悟空,八戒)
in中的每個資料都會最為一次條件,隻要滿足條件就會顯示
like"%張%" 模糊查詢,查詢所有名字裡帶張的,例如
where name like"%張%“;
如果隻想查詢名字由某個字開頭的,執行下面指令where name like"王%”;
is null 查詢某一列為null的值,注意:不能寫 = null -
運算符 說明 And && 多個條件同時成立 Or || 多個條件任一成立 Not 條件不成立,取反 - 實際運用
-
-- 條件運算查詢 -- 查詢某個比表中某一列為null的值,關鍵字is null select * from student where sid is null; -- 查詢某個表中某一列大于某個值的值 select * from text3 where price > 70; -- 查詢某個表中某一列小于等于某個值的值 select * from text3 where price <= 70; -- 查詢某個表中某一列不等于某個值的值,以下兩種方法一緻 select * from text3 where price <> 72; select * from text3 where price != 72; -- 查詢某個表中某一列的值在某個區間之間, 關鍵字between...and... select * from text3 where price between 30 and 60; -- 模糊查詢,查找某個表某一列中有關寫入的字元的所有選項,關鍵字 like"%查詢的字元%" select * from text3 where content like "%東野圭吾%"; -- 模糊查詢2,查找某個表某一列中有關寫入的字元開頭的所有選項,關鍵字 like"查詢字元的開頭%" select * from text3 where content like "東%"; -- 集合in,查詢某個表中某一列存在集合裡的所有字元或數字 select * from text3 where content in ("東野圭吾","吳承恩"); select * from text3 where price in (102,72,34);
-