查询select
SELECT column_name,column_name
FROM table_name WHERE column_name operator value
插入insert into
INSERT INTO table_name
VALUES (value1,value2,value3,...);
or
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
更新update
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
字段需要``标注,值需要用''标注
INSERT INTO `guest`.`sign_guest` (
`realname`,
`phone`,
`email`,
`sign`,
`create_time`,
`eventt_id`
)
VALUES
(
'张三',
'456',
'456@qq.com',
'1',
'2019-03-26 04:52:59',
'1'
);
删除delete
DELETE FROM table_name
WHERE some_column=some_value;
查找某个字段出现次数大于2(having)
#查找名字重复出现2次以上的名字
SELECT s.`NAME` AS '学生',COUNT(s.`NAME`) AS '次数' FROM student s
WHERE s.CREATE_TIME>'2019-03-13 15:37:17' AND s.STUDENT_TYPE='ENROLLED'
GROUP BY s.`NAME` HAVING COUNT(s.`NAME`)>2;
输出
image.pngcase when then用法
select u.id,u.name,u.sex,
(case u.sex
when 1 then '男'
when 2 then '女'
else '空的'
end
)性别
from users u;
#也可以这样写(when后加处理逻辑)
select u.id,u.name,u.sex,
(case
when u.sex=1 then '男'
when u.sex=2 then '女'
else '空的'
end
)性别
from users u;
输出
image.png左、右、内连接
inner join、left join、right join
select * from A inner join B on A.name = B.name;
左连接以左表为主:左表四行,右表三行,左连接后新表共有四行,填充null
右连接以右表为主:左表四行,右表三行,右连接后新表共有三行
内连接取两表交集:左表四行,右表三行,内连接后新表取交集(可能是0/1/2/3行)
网友评论