从多个表中选择列?

时间:2014-07-22 22:35:05

标签: mysql sql laravel laravel-4

我需要从3个单独的表中检索数据,因为我已经在我使用的数据库中添加了一个附加表。

我的布局目前看起来像这样:

 _____________       _____________      ___________      ___________
| users       |     |    links    |    |users_links|    | tags      |
|-------------|     |-------------|    |-----------|    |-----------|
| id          |     | id          |    | id        |    | id        |
| name        |     | long_url    |    | link_id   |    | user_id   |
| username    |     | short_url   |    | user_id   |    | link_id   |
| email       |     | user_id     |    | privacy   |    | tag       |
| password    |     | page_title  |    | notes     |    |           |
| created_at  |     | updated_at  |    |           |    |           |
| updated_at  |     | clicks      |    |-----------|    |-----------|
---------------     | page_title  |
                    --------------            

以前我从两个表中收集了我的数据(工作正常),但在感觉需要允许标记之后,这需要一个额外的表。

我的原始查询如下所示:

    public static function previouslyShortened($username)
    {
            //Gets all related info for the info for member page
            $query = DB::select("select users.id as user_id,
                    links.id,
                    links.long_url,
                    links.short_url,
                    links.updated_at,
                    links.clicks,
                    links.page_title,
                    users_links.privacy,
                    users_links.notes,
                    from users
                    right outer join users_links on users.id = users_links.user_id
                    join links on links.id = users_links.link_id
                    where users.username = '$username'
                    ". (Auth::check() && Auth::user()->username == $username ? "" : "and users_links.privacy = 0")
            );

            $result = array('username' => $username, 'links' => $query);
            return $result;
    }

我还希望在查询中的表tags.id中包含tags.tagtags,并将其作为正在创建的对象的一部分返回。

我尝试创建另一个右外连接但尝试错误:

Syntax error or access violation - Not unique table alias. 

我该怎么做?


编辑:

我要求修复此尝试,所以这是:

    public static function previouslyShortened($username)
    {
            //Gets all related info for the info for member page
            $query = DB::select("select users.id as user_id,
                    links.id,
                    links.long_url,
                    links.short_url,
                    links.updated_at,
                    links.clicks,
                    links.page_title,
                    users_links.privacy,
                    users_links.notes,
                    tags.id,
                    tags.tag
                    from users
                    right outer join users_links on users.id = users_links.user_id
                    join links on links.id = users_links.link_id
                    right outer join tags.user_id on users_links.id = tags.user_id
                    join tags on users_links.user_id = tags.user_id
                    where users.username = '$username'
                    ". (Auth::check() && Auth::user()->username == $username ? "" : "and users_links.privacy = 0")
            );

            $result = array('username' => $username, 'links' => $query);
            return $result;
    }

2 个答案:

答案 0 :(得分:1)

删除错误,您可以尝试以下代码

public static function previouslyShortened($username)
{
        //Gets all related info for the info for member page
        $query = DB::select("select users.id as user_id,
                links.id,
                links.long_url,
                links.short_url,
                links.updated_at,
                links.clicks,
                links.page_title,
                users_links.privacy,
                users_links.notes,
                tags.id,
                tags.tag
                from users
                left join users_links on users.id = users_links.user_id
                left join links on links.id = users_links.link_id
                left join tags on users.id = tags.user_id
                where users.username = '$username'
                ". (Auth::check() && Auth::user()->username == $username ? "" : "and users_links.privacy = 0") . " group by links.id"
        );
        $result = array('username' => $username, 'links' => $query);
        return $result;
}

答案 1 :(得分:1)

在我们的聊天中,这是来自声明:

from users 
inner users_links on users.id = users_links.user_id 
inner join links on links.id = users_links.link_id 
left join tags as t1 on users_links.id = t1.user_id 
left tags as t2 on users_links.user_id = t2.user_id