Skip to content

join and lists: Column 'name' in field list is ambiguous #3280

@HendrikJan

Description

@HendrikJan

When using the following query:

        $tags = DB::table('tag')
            ->leftJoin('cursus_tag', 'cursus_tag.tag_id', '=', 'tag.id')
            ->leftJoin('cursus', 'cursus.id', '=', 'cursus_tag.cursus_id')
            ->whereIn('cursus.curriculumdeel_id', array($cd->id, $cd2->id))
            ->lists('tag.name');

I get the error "Column 'name' in field list is ambiguous".

The SQL query (mySql) is this one:

select `name` from `tag`
left join `cursus_tag` on `cursus_tag`.`tag_id` = `tag`.`id`
left join `cursus` on `cursus`.`id` = `cursus_tag`.`cursus_id`
where `cursus`.`curriculumdeel_id` in (3, 1))

The problem being that the select statement should be select 'tag.name' from 'tag'.

A workaround is to change the query to this:

        $tags = DB::table('tag')
            ->select('tag.name')
            ->leftJoin('cursus_tag', 'cursus_tag.tag_id', '=', 'tag.id')
            ->leftJoin('cursus', 'cursus.id', '=', 'cursus_tag.cursus_id')
            ->whereIn('cursus.curriculumdeel_id', array($cd->id, $cd2->id))
            ->lists('tag.name');

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions