本篇博主给大家带来MySQL之、CRUD、常见函数及union查询,希望能够帮助到大家
SELECT
{*|<字段名>}
[FROM<表1>,<表2>.....
[WHERE<表达式>
[GROUP BY <group by definition>
[HAVING <expression> [{<operator><expression>}...]]
[ORDE BY <order by definition>]
[LIMIT[<offset>]<row count>]]
INSERT...VALUES语句:INSERT INTO<表名>[<列名1>[....<列名n>]] VALUES(值1)[...(值n)]
INSERT....SET语句:INSERT INTO<表名>SET<列名1>=<值1>,<列名2>=<值2>
UPDATE<表名>SET字段1=值1[,字段2=值2....][WHERE子句][ORDER BY 子句][LIMIT 子句]
DELETE FROM<表名>[WHERE子句][ORDER BY 子句][LIMIT 子句]
LOWER('SQL Course')-----sql course
UPPER('SQL Course')------SQL COURSE
CONCAT('Hello','World')-----HelloWorld
SUBSTR('HelloWorld',1,5)--------hello
LENGTH('HelloWorld')------10
INSTR('HelloWorld','W')------6
TRIM('H'FROM'HElloWorld')----elloWorld
REPLACE('abcd','b','m')-----amcd
ROUND(45.936,2)---45.92
TRUNC(45.926,2)----45.92
MOD(1600,300)----100
now()
STR_TO_DATE('9-13-1999','%m-%d-%Y')-----1999-09-13
DATE_FROMAT('2018/6/6','%Y年%m月%d日')---2018年06月06日
CASE expr WHEN comparison_expr1 THEN return_expr1[WHEN comparison_expr2 THEN return_expr2 WHEN comparison_exprn THEN return_exprn ELSE else_expr]END
SELECT t3.*,(CASE WHEN t1.cid='01' THEN t1.score END)语文,
(CASE WHEN t2.cid='02' THEN t2.score END)数学
FROM
(SELECT * FROM t_mysql_score sc WHERE sc.cid='01')t1,
(SELECT * FROM t_mysql_score sc WHERE sc.cid='02')t2,
t_mysql_student t3
WHERE t1.sid=t2.sid
AND t1.sid=t3.sid
②查询会议信息(包含会议信息表数据,主持人姓名、审批人姓名、会议状态):
SELECT a.id,a.title,a.content,a.canyuze,a.liexize,a.zhuchiren,b.'name',a.location,DATE_FROMMAT(a.startTime,'%Y-%m-%d %H:%i:%s')as startTime,
DATE_FROMAT(a.endTime,'%Y-%m-%d %H:%I:%s')as endTime
,a.state,(case a.state)
when 0 then '取消会议'
when 1 then '新建'
when 2 then '待审核'
when 3 then '驳回'
when 4 then '待开'
when 5 then '进行中'
when 6 then '开启投票'
else '结束会议' end)
as meetingState,
a.seatPic,a.remark,a.auditor,c.'name' as auditorName
FROM t_oa_meeting_info a
inner join t_oa_meeting_info a
inner join t_oa_user b on a.zhuchiren=b.id
left JOIN t_oa_user c on a.auditor=c.id where 1=1
SELECT * FROM t_oa_meeting_info where id>=1 and id <=8
SELECT * FROM t_oa_meeting_info where id>=6 and id<=10
SELECT * FROM t_oa_meeting_info where id>=1 and id<=8
UNION
SELECT * FROM t_oa_meeting_info where id>=6 and id<=10
结果678只出现了1次
UNION all:
SELECT * FROM t_oa_meeting_info where id>=1 and id<=8
UNION all
SELECT * FROM t_oa_meeting_info where id>=6 and id<=10
结果678重复出现
②单列:
初始数据:
SELECT * FROM t_oa_meeting_info where id>=1 and id <=8
SELECT * FROM t_oa_meeting_info where id>=6 and id<=10
UNION:
SELECT * FROM t_oa_meeting_info where id>=1 and id<=8
UNION
SELECT * FROM t_oa_meeting_info where id>=6 and id<=10
结果678只出现了1次
UNION all:
SELECT * FROM t_oa_meeting_info where id>=1 and id<=8
UNION all
SELECT * FROM t_oa_meeting_info where id>=6 and id<=10
结果678重复出现