目錄
1 基本查詢
1.1 查詢相關(guān)列 (select * / 列名)
1.2 別名 (as)
1.3 去重 (distinct)
1.4?對列中的數(shù)據(jù)進(jìn)行運(yùn)算 (+、-、*、/)
2 條件查詢 (where)
2.1 等值查詢 (=)
2.2?非等值查詢 (>、<、>=、<=、!=、><)
2.3?邏輯判斷 (and、or、not)
2.4?區(qū)間判斷 (between and)
2.5?NULL 值判斷 (is null、is not null)
2.6?in 運(yùn)算符
2.7?模糊查詢 (like)
3 分支查詢
4 排序 (order by)
5 日期和時間函數(shù)
1 基本查詢
goods 表
drop table if exists goods;
create table goods (
id int(10) primary key auto_increment,
name varchar(14),
netprice float(7,2),
saleprice float(7,2),
weight float(7,2),
stockdate date
)charset=utf8; #單條插入
insert into goods(name,netprice, saleprice, weight, stockdate) values('香蕉', 2.5, 3.8, 24, '2024-02-13');#多條插入
insert into goods(name,netprice, saleprice, weight, stockdate) values
('蘋果', 4.5, 7.2, 15, '2024-02-12'),
('蘋果', 4.5, 7.5, 65, '2024-02-14'),
('橘子', 3.2, 4.5, 52, str_to_date('02-12-2024', '%m-%d-%Y')),
('橘子', 2.8, 4.5, 76, '2024-02-13'),
('橘子', 3.1, 5.2, 63, '2024-02-14'),
('葡萄', 2.1, 4.7, 26, str_to_date('2024/02/14', '%Y/%m/%d'));
1.1 查詢相關(guān)列 (select * / 列名)
生產(chǎn)環(huán)境下,優(yōu)先使用列名查詢。* 的方式雖然看起來便捷,但實(shí)際上需要轉(zhuǎn)換成全列名,效率低,可讀性差 select * from goods;
select id, name, stockdate from goods; ? ?? 關(guān)鍵字順序 select ? ? ? ? ... from ? ? ? ? ... 以上語句執(zhí)行順序: - from
- select
|
1.2 別名 (as)
?通過 as 可以對列名取別名,as 可以省略(列名和別名之間用空格隔開) select id as '商品id', name as '商品名', netprice '進(jìn)價', saleprice '售價' from goods;  |
1.3 去重 (distinct)
?通過 distinct 將查詢結(jié)果中的重復(fù)行去除 select name from goods;
select distinct name from goods; ? |
1.4?對列中的數(shù)據(jù)進(jìn)行運(yùn)算 (+、-、*、/)
?可以對列中的數(shù)據(jù)進(jìn)行加 +、減 -、乘 *、除 / select id '商品id', name '商品名', (saleprice - netprice) * weight as '利潤' from goods;  |
2 條件查詢 (where)
通過 where 進(jìn)行條件查詢
關(guān)鍵字順序
select
? ? ? ? ...
from
? ? ? ? ...
where
? ? ? ? ...
以上語句執(zhí)行順序:
- from
- where
- select
2.1 等值查詢 (=)
select * from goods where name = '蘋果';  |
2.2?非等值查詢 (>、<、>=、<=、!=、><)
# 從商品中選擇 weight > 52 的商品
select id, name, netprice, saleprice, weight from goods where weight > 52;# 從商品中選擇 weight < 52 的商品
select id, name, netprice, saleprice, weight from goods where weight < 52;# 從商品中選擇 weight >= 52 的商品
select id, name, netprice, saleprice, weight from goods where weight >= 52;# 從商品中選擇 weight <= 52 的商品
select id, name, netprice, saleprice, weight from goods where weight <= 52;# 從商品中選擇 weight != 52 的商品,此外 != 還可以這樣用,如 name != '蘋果'
select id, name, netprice, saleprice, weight from goods where weight != 52;# 從商品中選擇 weight <> 52 的商品,<> 等價于 !=
select id, name, netprice, saleprice, weight from goods where weight <> 52;
 ?     |
2.3?邏輯判斷 (and、or、not)
# 選擇 weight > 52 且 name != '蘋果' 的商品
select id, name, netprice, saleprice, weight from goods where weight > 52 and name != '蘋果';# 選擇 weight > 52 或 not name = '蘋果' 的商品(not name = '蘋果' 等價于 name != '蘋果')
select id, name, netprice, saleprice, weight from goods where weight > 52 or not name = '蘋果';   |
2.4?區(qū)間判斷 (between and)
# 選擇 24 <= weight <= 50 的商品
select id, name, netprice, saleprice, weight from goods where weight between 24 and 50;  |
2.5?NULL 值判斷 (is null、is not null)
# 查詢表 goods 中 weight 為 null 的商品
select * from goods where weight is null;# 查詢表 goods 中 weight 不為 null 的商品
select * from goods where weight is not null;  |
2.6?in 運(yùn)算符
- in 運(yùn)算符語法:
- where 列名 in (value1, value2, ...)
- where 列名 not in?(value1, value2, ...)
- 只要列值在列表項(xiàng) (value1, value2, ...) 中,則該行符合條件
- in 列表項(xiàng)不僅支持?jǐn)?shù)字,也支持字符甚至?xí)r間日期類型等,并且可以將這些不同類型的數(shù)據(jù)項(xiàng)混合排列而無須跟 column(列) 的類型保持一致,如 in (1, 2,'str')
- in 可以和 and、or、>、<= 等運(yùn)算符結(jié)合使用
- in 列表項(xiàng)可以是子查詢
- 如果 in?的列表項(xiàng)是確定的,那么可以用多個 or?來代替。一般認(rèn)為,如果是對索引字段進(jìn)行操作,使用 or?效率高于 in,但對于列表項(xiàng)不確定的時候(如需要子查詢得到結(jié)果),就必須使用 in?運(yùn)算符。另外,對于子查詢表數(shù)據(jù)小于主查詢的時候,也是適用 in?運(yùn)算符的
select id, name, netprice, saleprice from goods where name in ('蘋果', '香蕉');  |
2.7?模糊查詢 (like)
- “_” 表示單個任意字符
- “%” 表示任意個任意字符( 0 ~ n 個任意字符)
# 查詢表 goods 中 stockdate 為 '2024-02-x4' 的商品
select id, name, stockdate from goods where stockdate like '2024-02-_4';# 查詢表 goods 中 saleprice 為 7.xx 的商品
select id, name, saleprice from goods where saleprice like '7.%';# 查詢表 goods 中 name 不為 '蘋x' 的商品
select id, name stockdate from goods where name not like '蘋_';
   |
3 分支查詢
分支查詢語法: case ? ? ? ? when 條件1 then 結(jié)果1 ? ? ? ? when 條件2 then 結(jié)果2 ? ? ? ? ... ? ? ? ? else 結(jié)果n end select id, name, stockdate,casewhen stockdate = '2024-02-12' then '前天'when stockdate = '2024-02-13' then '昨天'when stockdate = '2024-02-14' then '今天'else '不知道'end as time
from goods;  |
4 排序 (order by)
order by 列名 asc?/ desc;
asc 表示升序,desc 表示降序,默認(rèn)是升序
關(guān)鍵字順序
select
? ? ? ? ...
from
? ? ? ? ...
where
? ? ? ? ...
order by
? ? ? ? ...
以上語句執(zhí)行順序:
- from
- where
- select
- order by
# 按照 weight 重量升序
select id, name, netprice, saleprice, weight from goods order by weight;
# 按照 weight 重量降序
select id, name, netprice, saleprice, weight from goods order by weight desc;
先按照 netprice 進(jìn)價降序,如果 netprice 相等,則按照 id 降序
select id, name, netprice, saleprice, weight from goods order by netprice desc, id asc;



5 日期和時間函數(shù)
時間函數(shù) | 描述 |
---|
sysdate() | 獲得當(dāng)前系統(tǒng)時間(年、月、日、時、分、秒) | curdate() | 獲得當(dāng)前日期 | curtime() | 獲得當(dāng)前時間 | week(date) | 獲得指定日期是一年中的第幾周 | year(date) | 獲得指定日期的年份 | month(date) | 獲得指定日期的月份 | day(date) | 獲得指定日期是月份的第幾天 | hour(date) | 獲得指定時間的小時值 | minute(date) | 獲得指定時間的分鐘值 | second(date) | 獲得指定時間的秒值 | datediff(enddate,startdate) | 獲得兩個時間相隔的天數(shù) enddate - startdate 注意和 SQL Server 中的 datediff(datepart,startdate,enddate) 不同 | adddate(date,n) | 在指定日期后面加 n 天 | str_to_date(str,fmt) | 將不同時間格式的字符串轉(zhuǎn)為的特定格式的日期? ('%Y-%m-%d') | date_format(date,fmt) | 將時間轉(zhuǎn)為不同時間格式的字符串 | 時間格式 | 描述 |
---|
%Y | 年,4 位 | %y | 年,2 位 | %X | 年,其中的星期日是周的第一天,4 位,與 %V 使用 | %x | 年,其中的星期一是周的第一天,4 位,與 %v 使用 | %b | 縮寫月名 | %c | 月,數(shù)值 | %M | 月名 | %m | 月,數(shù)值(00-12) | %j | 年的天 (001-366) | %D | 帶有英文前綴的月中的天 | %d | 月的天,數(shù)值(00-31) | %e | 月的天,數(shù)值(0-31) | %U | 周 (00-53) ,星期日是一周的第一天 | %u | 周 (00-53) ,星期一是一周的第一天 | %V | 周 (01-53) ,星期日是一周的第一天,與 %X 使用 | %v | 周 (01-53) ,星期一是一周的第一天,與 %x 使用 | %w | 周的天 (0=星期日, 6=星期六) | %a | 縮寫星期名 | %W | 星期名 | %T | 時間,24-小時 (hh:mm:ss) | %r | 時間,12-小時(hh:mm:ss AM 或 PM) | %p | AM 或 PM | %H | 小時 (00-23) | %h | 小時 (01-12) | %I | 小時 (01-12) | %k | 小時 (0-23) | %l | 小時 (1-12) | %i | 分鐘,數(shù)值(00-59) | %S | 秒(00-59) | %s | 秒(00-59) | %f | 微秒 |
# 獲得當(dāng)前系統(tǒng)時間、當(dāng)前日期、當(dāng)前時間
select sysdate(), curdate(), curtime();# 獲得指定日期是一年中的第幾周、指定日期的年份、月份、指定日期是月份的第幾天
select week('2024-02-14') week, year('2024-02-14') year, month('2024-02-14') month, day('2024-02-14') day;# 獲得指定時間的小時值、分鐘值、秒值
select hour('16:33:24'),minute('16:33:24'), second('16:33:24');# 獲得兩個時間之間相隔的天數(shù),只計算日期,這里是2024-03-02 減去 2024-02-29 得到兩天
select datediff('2024-03-02 08:00:00','2024-02-29 10:00:00') day;# 將不同時間格式的字符串轉(zhuǎn)為的特定格式的日期
select str_to_date('2024/02-13 13:4356','%Y/%m-%d %H:%i%s');# 將時間轉(zhuǎn)為不同時間格式的字符串
select date_format(sysdate(), '%Y/%m/%d %H:%i:%s');# 在 '2024-02-28 23:59:59' 加 1 / 2 天
select adddate('2024-02-28 23:59:59',1);
select adddate('2024-02-28 23:59:59',2);
    |