查询两个表以查找日期之间存在的行

时间:2015-05-12 01:50:11

标签: mysql sql union

我查询两个表并返回一段时间之间存在的日期。

(

            SELECT DATE_FORMAT(date, '%Y-%m-%d') AS date
            FROM `users exercises`
            WHERE `userid` = $user->id AND (date >= '$begin' AND date <= '$end')
            GROUP BY day(date)
            ORDER BY date

            ) UNION ALL (

            SELECT DATE_FORMAT(date, '%Y-%m-%d') AS date
            FROM `users foods`
            WHERE `userid` = $user->id AND (date >= '$begin' AND date <= '$end')
            GROUP BY day(date)
            ORDER BY date

)

我目前正在使用UNION,但我觉得可能有更好的方法可以达到我想要的效果吗?

两个表都具有相同的结构:

id | userid | date (timestamp)

0 个答案:

没有答案