今天延续昨日的工作,新加了几个功能,其中比较蛋疼的还是sql查询。
今日的收获主要是学到了left join 比较复杂的使用方法。
在多个表查询数据的时候,可以通过left join 去左连接缓存表
写法类似这样
select t1.* from t1
left join
(select * from title where title.create_time > '2018-12-31') as t2
on t1.id = t2.group_id
其中的(select * from title where title.create_time > '2018-12-31') as t2
就是把select查询出来的结果集当做了一个t2的缓存表,在让缓存表t2和t1做左连接。
更复杂一些类似写成这样:
SELECT a.*, b.tottime AS 'Total time (company)'
FROM
(SELECT u.login, a.article, p.p_article, (SUM(p.p_going) + SUM(p.p_leaving) + SUM(p.p_working)) AS 'Total time (worker)'
FROM pos p, users u, articles a
WHERE u.login = p.p_login
AND REPLACE( u.login, '.', '_' ) = 'users_name'
AND p.p_datum >= '2013-04-09'
AND p.p_datum <= '2013-04-16'
AND p.p_article = a.id
GROUP BY a.article) a
LEFT JOIN
(SELECT a.article, p.p_article, (SUM(p.p_going) + SUM(p.p_leaving) + SUM(p.p_working)) AS tottime
FROM pos p, articles a
WHERE p.p_datum >= '2013-04-09'
AND p.p_datum <= '2013-04-16'
AND p.p_article = a.id
GROUP BY a.article) b
ON a.article = b.article /