有两个表,一个是“用户”,另一个是“放映” 用户可以在筛选中具有多个记录。 我需要从一个多重筛选记录中获取最早记录的日期。 并且看看自那以后最早的一次放映记录是否已经过了30天。 我该怎么做?
我得到了这样的查询:
SELECT users.id AS user_id, users.first_name, users.last_name, clients.name AS client,
users.social_security_number AS ssn, users.hiredate,
screening.id AS screening_id, screening.date AS screening_date, screening.maileddate AS screening_maileddate
FROM users
INNER JOIN clients
ON(
users.client_id = clients.id
AND users.client_id = '1879'
)
LEFT JOIN screening
ON(
users.id = screening.user_id
)
WHERE
(users.hiredate BETWEEN '2011-01-01' AND '2011-08-14'
OR users.hiredate IS NULL)
AND users.id IN(
SELECT users.id from users
INNER JOIN screening sall
ON(
sall.user_id = users.id
)
INNER JOIN screening s1
ON(
s1.user_id = users.id
AND s1.date BETWEEN '2011-05-15' AND '2011-11-15'
)
INNER JOIN screening s2
ON(
s2.user_id = users.id
AND s2.date BETWEEN '2011-05-15' AND '2011-11-15'
)
INNER JOIN screening s3
ON(
s3.user_id = users.id
AND s3.date BETWEEN '2011-05-15' AND '2011-11-15'
)
INNER JOIN screening s4
ON(
s4.user_id = users.id
AND s4.date BETWEEN '2011-05-15' AND '2011-11-15'
)
INNER JOIN screening s5
ON(
s5.user_id = users.id
AND s5.date BETWEEN '2011-05-15' AND '2011-11-15'
)
INNER JOIN screening s6
ON(
s6.user_id = users.id
AND s6.date BETWEEN '2011-05-15' AND '2011-11-15'
)
WHERE (s1.cholesterol IS NOT NULL
AND s2.ldl IS NOT NULL
AND s3.triglycerides IS NOT NULL)
AND (s4.glucose IS NOT NULL
OR s5.ha1c IS NOT NULL)
)
GROUP BY users.id
答案 0 :(得分:0)
使用FIRST(column_name)命令获取第一条记录的值。
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html