1、select * from user where mobile like '11110000%';
2倔监、select * from vehicle_policy where user_id in (select user_id from user where mobile = '11110000775');
3、select * from api_log limit 50;
4、select * from user where CREATED_DATE >= '2016-06-16';
5、-- GROUP BY 是分組之后對(duì)每個(gè)小分組做統(tǒng)計(jì)
SELECT DEPT, MAX(SALARY) AS MAXIMUM
FROM employee
GROUP BY DEPT;
7爹凹、SELECT DEPT, EDLEVEL, MAX( SALARY ) AS MAXIMUM
FROM employee
WHERE HIREDATE > '2010-01-01'
GROUP BY DEPT, EDLEVEL
ORDER BY DEPT, EDLEVEL;
8、select count(user_id) c, user_id, created_date from policy group by user_id order by c desc;
9镶殷、select count(*) from policy group by user_id;
10禾酱、-- 連接字段
-- concat_ws有分隔符 ?concat 沒有分隔符
SELECT A.QTING_TASK_ID, concat_ws('|',A.QTING_TASK_ID, A.USER_ID) AS TASK_PRIMARY_KEY FROM qting_task A where user_id = 'YH20151208000006';
11、select * from policy order by user_id desc;
12、UPDATE policy set status = 1 where status = 2 and BI_START_DATE < now() and BI_END_DATE > now();
13颤陶、INSERT INTO msg_t_text
(CONTENT, TITLE, BUSINESS_MODULE)
values
('保單號(hào):%s', '購(gòu)險(xiǎn)返現(xiàn)解凍', '5000'),
('保單號(hào):%s', '購(gòu)險(xiǎn)返現(xiàn)退保', '5001');
INSERT INTO msg_t_text?(CONTENT, BUSINESS_MODULE)?values('保單號(hào):%s', '5002');
14颗管、select sum(balance) from wallet where user_id = 'YH20160701000002364198';
15、SET @PHONE = '13246631865';
DELETE FROM callcenter_t_task WHERE ?MOBILE = @ PHONE;
16指郁、-- news_table 有字段(nid,title,type_id)
-- name_table有字段(type_id,type_name)
select a.title,b.type_name
from news_table as a,name_table as b
where a.type_id=b.type_id ;
17忙上、SQL多表連接查詢
(1)外連接:
外連接可分為:左連接、右連接闲坎、完全外連接疫粥。
①左連接? left join 或 left outer join:
SQL語句:select * from student left join course on student.ID=course.ID
執(zhí)行結(jié)果:
左外連接包含left join左表所有行,如果左表中某行在右表沒有匹配腰懂,則結(jié)果中對(duì)應(yīng)行右表的部分全部為空(NULL).
注:此時(shí)我們不能說結(jié)果的行數(shù)等于左表數(shù)據(jù)的行數(shù)梗逮。當(dāng)然此處查詢結(jié)果的行數(shù)等于左表數(shù)據(jù)的行數(shù),因?yàn)樽笥覂杀泶藭r(shí)為一對(duì)一關(guān)系绣溜。
②右連接? right join 或 right outer join
SQL語句:select * from student right join course on student.ID=course.ID
執(zhí)行結(jié)果:
右外連接包含right join右表所有行慷彤,如果左表中某行在右表沒有匹配,則結(jié)果中對(duì)應(yīng)左表的部分全部為空(NULL)怖喻。
注:同樣此時(shí)我們不能說結(jié)果的行數(shù)等于右表的行數(shù)底哗。當(dāng)然此處查詢結(jié)果的行數(shù)等于左表數(shù)據(jù)的行數(shù),因?yàn)樽笥覂杀泶藭r(shí)為一對(duì)一關(guān)系锚沸。
③完全外連接? full join 或 full outer join (mysql不支持: full outer join跋选;不支持加條件:on student.id = course.id;只能:select * from student full join course:但是結(jié)果是笛卡爾積哗蜈?前标?——只能說:mysql不支持完全外連接;)
SQL語句:select * from student full join course on student.ID=course.ID
執(zhí)行結(jié)果:
完全外連接包含full join左右兩表中所有的行距潘,如果右表中某行在左表中沒有匹配炼列,則結(jié)果中對(duì)應(yīng)行右表的部分全部為空(NULL),如果左表中某行在右表中沒有匹配音比,則結(jié)果中對(duì)應(yīng)行左表的部分全部為空(NULL)俭尖。
(2)內(nèi)連接? join?或 inner join
SQL語句:select * from student inner join course on student.ID=course.ID
執(zhí)行結(jié)果:
inner join 是比較運(yùn)算符,只返回符合條件的行洞翩。
此時(shí)相當(dāng)于:select * from student,course where student.ID=course.ID
(3)交叉連接 cross join
1.概念:沒有 WHERE 子句的交叉聯(lián)接將產(chǎn)生連接所涉及的表的笛卡爾積稽犁。第一個(gè)表的行數(shù)乘以第二個(gè)表的行數(shù)等于笛卡爾積結(jié)果集的大小。
SQL語句:select * from student cross join course
執(zhí)行結(jié)果:
如果我們?cè)诖藭r(shí)給這條SQL加上WHERE子句的時(shí)候菱农,比如SQL:
select * from student cross join course where student.ID=course.ID
此時(shí)將返回符合條件的結(jié)果集缭付,結(jié)果和inner join所示執(zhí)行結(jié)果一樣柿估。
(4)兩表關(guān)系為一對(duì)多循未,多對(duì)一或多對(duì)多時(shí)的連接語句
當(dāng)然上面兩表為一對(duì)一關(guān)系,那么如果表A和表B為一對(duì)多、多對(duì)一或多對(duì)多的時(shí)候的妖,我們又該如何寫連接SQL語句呢绣檬?
其實(shí)兩表一對(duì)多的SQL語句和一對(duì)一的SQL語句的寫法都差不多,只是查詢的結(jié)果不一樣嫂粟,當(dāng)然兩表也要略有改動(dòng)娇未。
比如:
表1的列可以改為:
Sno Name Cno
表2的列可以改為:
Cno CName
這樣兩表就可以寫一對(duì)多和多對(duì)一的SQL語句了,寫法和上面的一對(duì)一SQL語句一樣星虹。
下面介紹一下當(dāng)兩表為多對(duì)多的時(shí)候我們?cè)撊绾谓ū硪约靶㏒QL語句零抬。新建三表:
一個(gè)學(xué)生可以選擇多門課程,一門課程可以被多個(gè)學(xué)生選擇宽涌,因此學(xué)生表student和課程表course之間是多對(duì)多的關(guān)系平夜。
當(dāng)兩表為多對(duì)多關(guān)系的時(shí)候,我們需要建立一個(gè)中間表student_course卸亮,中間表至少要有兩表的主鍵忽妒,當(dāng)然還可以有別的內(nèi)容。
SQL語句:
select s.Name,C.Cname from student_course_2 as sc left join student2 as s on s.Sno=sc.Sno left join course2 as c on c.Cno=sc.Cno where c.cname = '足球';
或者:select * from student2 s2 right join student_course_2 sc2 on s2.sno=sc2.sno left join course2 c2 on sc2.cno=c2.cno;
執(zhí)行結(jié)果:
18兼贸、建表
CREATE TABLE employee (
id int(8) NOT NULL,
name char(30),
dept char(30),
salary double,
edlevel int(8),
hiredate date)
ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
19段直、存儲(chǔ)過程
drop procedure updatemobile;
CREATE PROCEDURE updatemobile()
BEGIN
????????DECLARE phone BIGINT;
????????SET phone = 0;
????????WHILE phone <= 3?DO
????SELECT phone;
????????SET phone = phone + 1;
????????END WHILE;
END
CALL updatemobile();
20、HAVING 子句(分組合計(jì))
在SQL中增加 HAVING 子句原因是溶诞,WHERE 關(guān)鍵字無法與合計(jì)函數(shù)一起使用鸯檬。"Orders" 表:
現(xiàn)在,我們希望查找訂單總金額少于2000的客戶很澄。
我們使用如下SQL語句:
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
HAVING SUM(OrderPrice)<2000
結(jié)果集類似:
21京闰、-- 查看哪些表被鎖了;
show OPEN TABLES where In_use > 0;
-- 查看哪些事務(wù)在鎖表甩苛;
SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS;
-- 查看鎖表的進(jìn)程蹂楣,然后kill掉;
show processlist;
kill id;
22讯蒲、個(gè)表數(shù)據(jù)如下:
-- 選了所有課程的學(xué)生
select * from student2 where sno
in(
select t.sno from (select sno, count(sno) c from student_course_2 group by sno) t
where
t.c = (select count(1) from course2)
);
-- 選了所有課程的學(xué)生以及所選的課程
select * from
(select * from student2 where sno
in(
select t.sno from (select sno, count(sno) c from student_course_2 group by sno) t
where
t.c = (select count(1) from course2)
)
) t2 cross join course2 order by t2.sno;
23痊土、INSERT INTO vehicle_region(VEHICLE_REGION_ID, VEHICLE_ID, REGION_ID, DATA_TYPE, IS_DEFAULT)
SELECT UUID(), shadow_VEHICLE_ID, '440300', 'shadow', 1 FROM shadow_t_vehicle WHERE plate_no LIKE '粵B%' AND shadow_VEHICLE_ID
NOT IN(SELECT vehicle_id FROM vehicle_region);
完畢。