MySQL多表连接

时间:2008-11-25 02:01:51

标签: mysql join

我需要修改以下MySQL语句以包含来自第三个表的信息......但是我很喜欢连接。

select
    ss.*,
    se.name as engine,
    ss.last_run_at + interval ss.refresh_frequency day as next_run_at,
    se.logo_name    
from 
    searches ss join search_engines se on ss.engine_id = se.id
where
    ss.user_id='.$user_id.'
group by ss.id
order by ss.project_id, ss.domain, ss.keywords

我还需要通过将projects.project_namesearches.project_id匹配来检索projects.id

1 个答案:

答案 0 :(得分:6)

结帐SELECT SyntaxJOIN Syntax

但是,最简单的答案 - 添加另一个JOIN..ON语句。

select
        ss.*,
        se.name as engine,
        ss.last_run_at + interval ss.refresh_frequency day as next_run_at,
        se.logo_name,
        pr.project_name -- +
from 
        searches ss
        join search_engines se on ss.engine_id = se.id
        join projects pr on ss.project_id = pr.id -- +
where
        ss.user_id='.$user_id.'
group by ss.id
order by ss.project_id, ss.domain, ss.keywords

[编辑]我添加了-- +条评论以指出我的添加内容。