3

我需要修改以下 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_name通过匹配searches.project_id来检索projects.id.

4

1 回答 1

6

查看SELECT语法JOIN语法

但是,最简单的答案 - 添加另一个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

[编辑]我添加了-- +评论以指出我的补充。

于 2008-11-25T02:12:20.643 回答