两个sql交集_简单明了的sql基础语句
一,數(shù)據(jù)庫(kù)及表的增刪改查
查看所有數(shù)據(jù)庫(kù):show databases; 切換到xxx庫(kù):use xxx; 查看庫(kù)中所有的表:show tables; 查看表結(jié)構(gòu):desc 表名; 數(shù)據(jù)庫(kù)創(chuàng)建:create datebase 數(shù)據(jù)庫(kù)名 default character set = 'utf8' 數(shù)據(jù)庫(kù)刪除 drop database 數(shù)據(jù)庫(kù)名 創(chuàng)建表 CREATE TABLE `表名` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) DEFAULT NULL, `type` int(11) DEFAULT NULL, `create_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 刪除表 drop table 表名二 ,數(shù)據(jù)庫(kù)內(nèi)容的增刪改查
查詢:select 列名 from 表名
select name from students; 查詢 students 中 name 列的內(nèi)容修改:update 表名 set 修改的字段 = 修改的內(nèi)容 where 修改的位置
update user set name='張三' where id = '2020'; 修改 user 表中 id =‘2020’ 的這條數(shù)據(jù)中的name 字段 改為“張三”刪除:delete 列名 from 表名
delete from user where id ='2020'; 刪除user 表中id=‘2020’的行插入:insert into 表名 (字段1,字段2,...,字段n) values(值1,值2,...,值n)
insert into user (id,name,pwd) values (22,張三,"李四"); 單條插入 insert into user(user_name,age) values ('test’,100), ('test’,200), ('test’,300), …; 多條插入三,條件查詢
模糊查詢
select * from user where phone_num like '888%’ select * from user where phone_num like '%888’ select * from user where phone_num like '%888%'范圍查詢
select * from user where id between 10 and 12 ; 查詢id 在10-12 之間的 user 表中的數(shù)據(jù) select * from user where id in (10,20) 查詢 id 是10,12 user 表中的數(shù)據(jù) select * from user where id not in (10.20) 查詢id 不是10,12 user表中的數(shù)據(jù)聚合函數(shù)
聚合函數(shù) max(最大)、min(最小)、avg(平均)、sum(求和)、count(總數(shù))
示例 (user,morder 分別為表名)
select max(age) from user; select min(age) from user; select avg(age) from user; select sum(total_price) from morder; s elect count(*) from morder排序查詢
倒敘 desc;正序 asc
select * from user order by age desc select * from user order by age asc去重
select distinct(age) from user; 查詢user表格中年齡不相同的數(shù)據(jù)限制
select * from user limit 10;分組
group by 按照表中某一個(gè)或多個(gè)字段,將數(shù)據(jù)進(jìn)行分組,一般用于將數(shù)據(jù)進(jìn)行分類匯總
select id,sum(price) from orders group by customer分組過(guò)濾
having對(duì)分組之后的數(shù)據(jù)進(jìn)行過(guò)濾
SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer HAVING SUM(OrderPrice)<2000四,多表連接查詢
內(nèi)連接
以某字段為條件,取左表和右表中同時(shí)存在相同數(shù)據(jù)的行,相當(dāng)于兩個(gè)表的交集
select * from user u inner join morder m on u.id=m.id 內(nèi)連接,相當(dāng)于交集左連接
SELECT * from user u LEFT JOIN morder m ON U.ID = m.user_id以某字段為連接條件,取左表中的全部數(shù)據(jù)+右表與該字段對(duì)應(yīng)的數(shù)據(jù),可能會(huì)有某些數(shù)據(jù) 在左表存在但是右表不存在的情
右連接
右連接right join 以某字段為連接條件,取右表中的全部數(shù)據(jù)+左表與該字段對(duì)應(yīng)的數(shù)據(jù),可能會(huì)有某些數(shù)據(jù) 在右表存在但是左表不存在的情況
SELECT * from user u RIGHT JOIN morder m ON U.ID = m.user_id嵌套連接
多個(gè)select語(yǔ)句進(jìn)行嵌套,嵌套的位置可以在select后、from后、where后
select username,userid from user where id in (select user_id from morder where name='北京')總結(jié)
以上是生活随笔為你收集整理的两个sql交集_简单明了的sql基础语句的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: OpenMP和MPI的区别
- 下一篇: c编译器内存对齐