加入3个限制表

时间:2014-05-17 15:43:34

标签: mysql join

我已创建此查询以从表格时间轴中选择条目,并使用我的users表中的数据来丰富这些条目。现在我想从媒体表中选择(1)条目media.filename,其中media.album =' 0',我的查询结果现在返回我想要的但是它返回来自的所有条目媒体表我只需要一个。我可以在哪里提出我的条件或限制1?

SELECT dat, sourceinfo, users.firstname, users.lastname, users.token, users.prof,
media.filename from timeline
JOIN users ON users.user_id = timeline.userid2 JOIN media ON users.user_id = 
timeline.userid2 
WHERE (timeline.user_id = '25') and (dat BETWEEN DATE_ADD(NOW(), INTERVAL -1 
DAY) AND NOW())

非常感谢

1 个答案:

答案 0 :(得分:1)

LIMIT子句放在子查询中。

SELECT dat, sourceinfo, users.firstname, users.lastname, users.token, users.prof,
        media.filename
FROM timeline
JOIN users ON users.user_id = timeline.userid2
CROSS JOIN (SELECT filename
            FROM media
            WHERE album = 0
            LIMIT 1) AS media

或者您可以将子查询放在SELECT子句中:

SELECT dat, sourceinfo, users.firstname, users.lastname, users.token, users.prof,
       (SELECT filename
        FROM media
        WHERE album = 0
        LIMIT 1) AS filename
FROM timeline
JOIN users ON users.user_id = timeline.userid2