简体   繁体   中英

Selecting columns from multiple tables?

I need to retrieve data from 3 separate tables, as I've added an additional table to the database I'm using.

My layout currently looks like this:

 _____________       _____________      ___________      ___________
| 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  |
                    --------------            

Previously I collected my data from two tables (which worked fine), but after feeling the need to allow tagging, this required an additional table.

my original query looks like this:

    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;
    }

I would like to also include tags.id and tags.tag from the table tags within the query and return it as part of the object being created.

I have attempted this by trying to create another right outer join but I get the error:

Syntax error or access violation - Not unique table alias. 

How can I do this?


edit:

My attempt at the fix was requested so here it is:

    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;
    }

to remove the error you can try the following code

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;
}

From our chat, here is the from statement:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM