我试图查询一个庞大的数据库(大约2000万条记录)来获取一些数据.这是我正在处理的查询.
SELECT a.user_id, b.last_name, b.first_name, c.birth_date FROM users a
INNER JOIN users_signup b ON a.user_id a = b.user_id
INNER JOIN users_personal c ON a.user_id a = c.user_id
INNER JOIN
(
SELECT distinct d.a.user_id FROM users_signup d
WHERE d.join_date >= '2013-01-01' and d.join_date < '2014-01-01'
)
AS t ON a.user_id = t.user_id
尝试从数据库中检索其他数据时遇到一些问题.我想在结果表中添加2个附加字段:
>我能够获得出生日期,但我想在结果表中获得成员的年龄.数据在users_personal表中存储为“yyyy-mm-dd”.
>我想使用来自join_date&的数据,从名为user_signup的表中加入一个成员加入到左边(如果有的话)的总天数. left_date(格式:yyyy-mm-dd).
解决方法:
试试这个:
SELECT a.user_id, b.last_name, b.first_name, c.birth_date,
FLOOR(DATEDIFF(CURRENT_DATE(), c.birth_date) / 365) age,
DATEDIFF(b.left_date, b.join_date) workDays
FROM users a
INNER JOIN users_signup b ON a.user_id a = b.user_id
INNER JOIN users_personal c ON a.user_id a = c.user_id
WHERE b.join_date >= '2013-01-01' AND b.join_date < '2014-01-01'
GROUP BY a.user_id