個人筆記 MySQL查詢語句

2022-08-25 19:18:23 字數 1566 閱讀 8220

最近學習了mysql,分享一點其中查詢語句的使用

語法:select  顯示的內容  from  查詢的範圍   where   查詢的條件 (顯示全部內容用『*』號)

一、as 別名(可省略as)

例:select   stuname  as  '學生姓名'   form   t_student   where  stuname='張三',此句的意思是將學生表中姓名為『張三』的學生篩選出來,並將選出來的列重新命名為『學生姓名』。

二、distinct  篩選重複的資料

三、篩選條件有:

and(並且),用&&也可以;

or(或者);

between...and...(兩段範圍之間);

in(包含);

not in(不包含);

這裡需要記住一點是and和or同時使用時and的優先順序更高哦。

四、模糊查詢

1. 使用百分號『%』

例:select * from t_student where s_name='張' --=找到所有『張』這個字;

select * from t_student where s_name='張三' -- = 全匹配 不會幫你自動模糊查詢;

select * from t_student where s_name like '李%' -- like % 不限制字元(0-n個字元)的模糊查詢;

select * from t_student where s_name like '%李%' -- like % 可以查到包含李的;

select * from t_student where s_name like '%李' -- like % 可以查到李,以李結尾, 但是 不能是李**;

2. 使用佔位符:下劃線_

select * from t_student where s_name like '李__' -- like _ 限制字元數的模糊查詢  乙個下劃線代表乙個字元。

3. 空值/非空值

-- 查詢年紀為空的學生資訊

select * from t_student where age=" " -- ''代表為空白的值

select * from t_student where age is null -- 代表空值 沒有輸入資料

-- 不是空

select * from t_student where age is not null

五、排序order by

例:select * from t_student order by age -- 預設是公升序

select * from t_student order by age desc -- 降序

select * from t_student order by age asc -- 公升序

六、返回限定行

select * from t_student limit 2-- limit 數字=從第一行資料開始 數兩行

select * from t_student limit 2,2-- limit 數字n,數字m=從第n行資料開始(跟陣列下標計算方式一樣) 數m行 跟字串substr類似

mysql 查詢語句

在pdo中有很多模式能用,在使用的時候在用 bindvalue 的時候 在select 中有in 的 語句無法實現,在傳入的時候 select from users where id in 1,2,3 當1,2,3 用 pdo param str 的時候,會出現這種情況 select from ue...

MySQL查詢語句

建立水果表 create table fruits f id char 10 not null,s id int notnull,f name char 255 not null,f price decimal 8,2 not null,primary key f id 插入資料 insert in...

MYSQL查詢語句

內連線 取的兩個表的 有能連線的字段 的交集,即欄位相同的。利用內連線可獲取兩表的公共部分的記錄。select st.sno,st.sname,st.s st.age,st.sdept,co.cname,sc.grade from student st,course co,score sc wher...