MYSQL之DQL
查詢語句 Q query
select 選擇 字段 from 表名 限定條件
select ename,job from 表名
使用*號代替所有的字段
select * from 表名
使用where限定
select * from 表名 where 字段="";
在查詢時給查詢的字段起別名 as作為
select 字段名 as 員工編號,字段名 as 員工姓名 from 表名;
- as可以省略但是要加空格 字段名 員工編號
這個表不是真實存在的,而是源數(shù)據(jù)表查詢拼接而成;
去除重復(fù)數(shù)據(jù) distinct
可以對多個字段去重 去重的效果是多個字段拼接的后果
distinct要寫在所有字段的前面
ifnull 如果是空,使用特定值替換
- 為每個員工的工資都加一千 在sql中數(shù)值+數(shù)值可以正常運算
- 數(shù)值+字符串,如果字符串可以轉(zhuǎn)換為數(shù)值可以正常運算,如果不能則結(jié)果為原數(shù)值
- 數(shù)值+null=null
- select ename 員工姓名 ,sal+1000 工資 from 表名
拼接字符串
select concat("我的編號是",empno,"我的工作是",job);
使用限定的邏輯表達(dá)式
and or not
select * from 表名 where 字段名 >= and 字段名 <
select * from 表名 where 字段名 between 100 and 200
- between 包頭包尾 相對于 >=和<=
查找不為空 not null
不等于<>
模糊查詢 查找出所有姓張的員工
select * from 表名 where name like "張%";
- %表示任意個 多個 一個字或者兩個字或者多個字 張三 張曉松 張某某某
- 查找姓張為兩個字的select * from 表名 where name like "張_";
查找三個字的 select * from 表名 where name like "___";
查找名字中包含一的員工
- select * from 表名 where name like "%一%";