This article Source: GitHub · Click here || GitEE · Click here
First, join queries
Diagrammatic representation
1, construction of the table statement
Departments and employees table:
CREATE TABLE `tb_dept` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`deptName` varchar(30) DEFAULT NULL COMMENT '部门名称',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
CREATE TABLE `tb_emp` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`empName` varchar(20) DEFAULT NULL COMMENT '员工名称',
`deptId` int(11) DEFAULT '0' COMMENT '部门ID',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
2 or seven join query
- Figure 1: left outer connector
select t1.*,t2.empName,t2.deptId
from tb_dept t1 LEFT JOIN tb_emp t2 on t1.id=t2.deptId;
- Figure 2: right outer connector
select t1.*,t2.empName,t2.deptId
from tb_dept t1 RIGHT JOIN tb_emp t2 on t1.id=t2.deptId;
- Figure 3: within the connector
select t1.*,t2.empName,t2.deptId
from tb_dept t1 inner join tb_emp t2 on t1.id=t2.deptId;
- Figure 4: Left connector
Queries tb_dept table unique place.
select t1.*,t2.empName,t2.deptId
from tb_dept t1 LEFT JOIN tb_emp t2 on t1.id=t2.deptId
WHERE t2.deptId IS NULL;
- Figure 5: right connection
Queries tb_emp table unique place.
select t1.*,t2.empName,t2.deptId
from tb_dept t1 RIGHT JOIN tb_emp t2 on t1.id=t2.deptId
WHERE t1.id IS NULL;
- Figure 6: fully connected
select t1.*,t2.empName,t2.deptId
from tb_dept t1 LEFT JOIN tb_emp t2 on t1.id=t2.deptId
UNION
select t1.*,t2.empName,t2.deptId
from tb_dept t1 RIGHT JOIN tb_emp t2 on t1.id=t2.deptId
- Figure 7: Full not connected
Query two tables unrelated data.
select t1.*,t2.empName,t2.deptId
from tb_dept t1 RIGHT JOIN tb_emp t2 on t1.id=t2.deptId
WHERE t1.id IS NULL
UNION
select t1.*,t2.empName,t2.deptId
from tb_dept t1 LEFT JOIN tb_emp t2 on t1.id=t2.deptId
WHERE t2.deptId IS NULL
Second, the date and time query
1, construction of the table statement
CREATE TABLE `ms_consume` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`user_id` int(11) NOT NULL COMMENT '用户ID',
`user_name` varchar(20) NOT NULL COMMENT '用户名',
`consume_money` decimal(20,2) DEFAULT '0.00' COMMENT '消费金额',
`create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COMMENT='消费表';
2, Statistics Date Case
- Date range's first data
Scene: product daily operations activities, often seen this rule: activity time, upfront spending how much, how many concessions.
SELECT * FROM
(
SELECT * FROM ms_consume
WHERE
create_time
BETWEEN '2019-12-10 00:00:00' AND '2019-12-18 23:59:59'
ORDER BY create_time
) t1
GROUP BY t1.user_id ;
- The time difference between the date
Scene: Countdown common scene
SELECT t1.*,
timestampdiff(SECOND,NOW(),t1.create_time) second_diff
FROM ms_consume t1 WHERE t1.id='9' ;
- Today's data query
-- 方式一
SELECT * FROM ms_consume
WHERE DATE_FORMAT(NOW(),'%Y-%m-%d')=DATE_FORMAT(create_time,'%Y-%m-%d');
-- 方式二
SELECT * FROM ms_consume
WHERE TO_DAYS(now())=TO_DAYS(create_time) ;
- Time statistics
Scene: Statistical nearly seven days, the consumer is greater than the number of times the user twice.
SELECT user_id,user_name,COUNT(user_id) userIdSum
FROM ms_consume WHERE create_time>date_sub(NOW(), interval '7' DAY)
GROUP BY user_id HAVING userIdSum>1;
- The average date range
Scene: the average consumption specified date range, and sorting.
SELECT * FROM
(
SELECT user_id,user_name,
AVG(consume_money) avg_money
FROM ms_consume t
WHERE t.create_time BETWEEN '2019-12-10 00:00:00'
AND '2019-12-18 23:59:59'
GROUP BY user_id
) t1
ORDER BY t1.avg_money DESC;
Third, the tree-table query
1, construction of the table statement
CREATE TABLE ms_city_sort (
`id` INT (11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`city_name` VARCHAR (50) NOT NULL DEFAULT '' COMMENT '城市名称',
`city_code` VARCHAR (50) NOT NULL DEFAULT '' COMMENT '城市编码',
`parent_id` INT (11) NOT NULL DEFAULT '0' COMMENT '父级ID',
`state` INT (11) NOT NULL DEFAULT '1' COMMENT '状态:1启用,2停用',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
PRIMARY KEY (id)
) ENGINE = INNODB DEFAULT CHARSET = utf8 COMMENT = '城市分类管理';
2, direct SQL queries
SELECT t1.*, t2.parentName
FROM ms_city_sort t1
LEFT JOIN (
SELECT
m1.id,m2.city_name parentName
FROM
ms_city_sort m1,ms_city_sort m2
WHERE m1.parent_id = m2.id
AND m1.parent_id > 0
) t2 ON t1.id = t2.id;
3, function queries
- Queries parent name
DROP FUNCTION IF EXISTS get_city_parent_name;
CREATE FUNCTION `get_city_parent_name`(pid INT)
RETURNS varchar(50) CHARSET utf8
begin
declare parentName VARCHAR(50) DEFAULT NULL;
SELECT city_name FROM ms_city_sort WHERE id=pid into parentName;
return parentName;
end
SELECT t1.*,get_city_parent_name(t1.parent_id) parentName FROM ms_city_sort t1 ;
- Queries root child
DROP FUNCTION IF EXISTS get_root_child;
CREATE FUNCTION `get_root_child`(rootId INT)
RETURNS VARCHAR(1000) CHARSET utf8
BEGIN
DECLARE resultIds VARCHAR(500);
DECLARE nodeId VARCHAR(500);
SET resultIds = '%';
SET nodeId = cast(rootId as CHAR);
WHILE nodeId IS NOT NULL DO
SET resultIds = concat(resultIds,',',nodeId);
SELECT group_concat(id) INTO nodeId
FROM ms_city_sort WHERE FIND_IN_SET(parent_id,nodeId)>0;
END WHILE;
RETURN resultIds;
END ;
SELECT * FROM ms_city_sort WHERE FIND_IN_SET(id,get_root_child(5)) ORDER BY id ;
Fourth, the source address
GitHub·地址
https://github.com/cicadasmile/mysql-data-base
GitEE·地址
https://gitee.com/cicadasmile/mysql-data-base