DQL查詢資料語言(重點)
Select (核心)
1.查詢所有欄位
select * from `表名`;
2.查詢指定欄位
select `欄位1`,`欄位2` from `表名`;
3.給結果取一個名字
select `欄位1` as 名字1,`欄位2` as 名字2 from `表名`;
4.去重distinct
select distinct `欄位` from `表名`;(例:select distinct `age` from `students`;)
where條件陳述句
檢索符合條件的值
| 運算子 | 語法 | 描述 |
| and && | a and b / a && b |
邏輯與 兩個都為真結果為真 |
| or || | a or b / a || b | 邏輯或 一個為真結果為真 |
| not ! | is not / != | 邏輯非 真偽反轉 |
select `classhour` from `subject` where `classhour` = 110 and `gradeid` =1;
select `classhour` from `subject` where `classhour` !=110; (select `classhour` from `subject` where not `classhour`=110;)
模糊查詢
| 運算子 | 語法 | 描述 |
| is null | a is null | 如果a為null,結果為真 |
| is not null | a is not null | 如果a不為null,結果為真 |
| between | a between b and c | 若a在b和c之間,結果為真 |
| like | a like b | SQL匹配,如果a匹配b,結果為真 |
| in | a in (a1,a2,a3...) | a在數列內,結果為真 |
1.like例子
查詢表中姓胡的同學 like結合(%代表任意字符,_代表一個字符)
select `name` from `students` where `name` like '胡%';
查詢姓胡的同學,名字后面只有一個字
select `name` from `students` where `name` like '胡_';
查詢姓胡的同學,名字后面有兩個字
select `name` from `students` where `name` like '胡__';
查詢名字中間有太字的同學 %太%
select `name` from `students` where `name` like '%太%';
2.in例子
查詢學號為10001,10002,10003的同學
select `name` from `students` where id in (10001,10002,10003);
3.null和not null
查詢班級號為空的人
select `name` from `students` where `gradid` is null;
查詢學科表內學時在100和140之間的人
select `classhour` from `subject` where `classhour` between 100 and 140;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/274019.html
標籤:其他
下一篇:MySQL存盤程序了解一下
