mysql外键约束查询语句_MySQL数据库 : 查询语句,连接查询及外键约束
查詢指定字段? ? ? ? select 字段1,字段2 from 表名;
消除重復行(重復指的是結果集中的所有完全重復行)? ? ? ? ? ? ?select distinct 字段1,字段2... from 表名
比較運算符(< > != = )? ? ? ? ? ? select * from 表名 where id>4
邏輯運算符(and or not in)? ? ? ? ? ? select * from 表名 where id>4(條件1) and gender=1(條件2);
模糊查詢(like %任意多個字符 _任意一個字符)? ? ? ? ? ? select * from 表名 where name like '馬%';
范圍查詢(in between...and...)? ? ? ? ? ? ? ? ? ?select * from 表名 where id in(1,2,3); 查詢id是1,2,3的行(非連續)
select * from 表名 where id between 1 and 5;(查詢id 1到5 的行,連續)
判空(is null) (非空 is not null)? ? ? ? ? ? ? ? ?select * from 表名 where 條件 is null;
優先級 (小括號,not,比較運算符,邏輯運算符(and比or先運算))
聚合函數
(count:求總行數)? ? ? ? ? ? ?select count(*) from 表名 where...
(max,min:最大值最小值)? ? ? ? ? ? ? select max(id) from 表名 where...
(sum,avg:求和求平均值)? ? ? ? ? ? ? select sum(id) from 表名 where...
分組(group by)? ? ? ? ? ? ? ? ? select gender count(*) from 表名 group by gender;(按性別分組并分別統計總數)
對分組結果進行篩選? ? ? ? ? ? ? ? (having) select gender count(*) from 表名 group by gender having gender=0;
where 是對 from 后的原始表進行數據篩選,having 是對group by 的結果進行篩選.
排序(order by默認升序asc降序desc)? ? ? ? ? ? ? ? ? ?select * from 表名 where 條件 order by id desc;
分頁(limit)select * from 表名 where 條件 limit 從第幾條數據開始,獲取多少條數據
完整select語句關鍵字順序:
select distinct *
from 表名 inner(left,right) join 表名 on 關系
where ...
group by ...
order by ...
limit start,count
外鍵約束 創建表的時候 最后添加? ? ? ? ? ? ? foreign key(約束字段) references 表名(字段名)
新建外鍵? ? ? ? ? ? ? ? ? ? alter table 表名 add constraint 外鍵名 foreign key(字段) references 外鍵表名(字段)
內連接查詢(inner join 連接表名 on 表名.字段=連接表名.字段) ps:兩張表必須要有一種關系
左連接(left join 以左表為準,左表數據全部顯示) 右連接同理(right join)
總結
以上是生活随笔為你收集整理的mysql外键约束查询语句_MySQL数据库 : 查询语句,连接查询及外键约束的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 香肠的功效与作用、禁忌和食用方法
- 下一篇: mysql注入写一句话_Mysql注入r