I don't know why in bellow query I don't have data from joining table:
DB::table('tags_ref')
->join('tags', 'tags_ref.tag_id', '=', 'tags.id')
->select(DB::raw('count(tag_id) as repetition, tag_id'))
->groupBy('tag_id')
->orderBy('repetition', 'desc')
->get();
I'm getting proper results but I without data from "tags" table.
My result:
[{"repetition":6,"tag_id":1},{"repetition":5,"tag_id":14},{"repetition":4,"tag_id":42},{"repetition":4,"tag_id":32},{"repetition":4,"tag_id":103},{"repetition":4,"tag_id":4},{"repetition":3,"tag_id":13},{"repetition":3,"tag_id":83},{"repetition":3,"tag_id":15},{"repetition":3,"tag_id":61},{"repetition":3,"tag_id":105},{"repetition":3,"tag_id":60}]
What is missing in results is "tags_name".
My question is how to retrieve data from joining table.
Regards.
Updated query:
DB::table('tags_ref')
->join('tags', 'tags_ref.tag_id', '=', 'tags.id')
->select(DB::raw('count(tag_id) as repetition'), 'tags.*')
->groupBy('tag_id')
->orderBy('repetition', 'desc')
->get();
Error in query:
SQLSTATE[42000]: Syntax error or access violation: 1055 'farmazon_lar.tags.id' isn't in GROUP BY (SQL: select count(tag_id) as repetition, `tags`.* from `tags_ref` inner join `tags` on `tags_ref`.`tag_id` = `tags`.`id` group by `tag_id` order by `repetition` desc)
Tables structure: tags_ref: id, post_id, tag_id tags: id, name
Updated query:
DB::table('tags_ref')
->join('tags', 'tags_ref.tag_id', '=', 'tags.id')
->select(DB::raw('count(tag_id) as repetition'), 'tags.tag_name')
->groupBy('tag_id, tag_name')
->orderBy('repetition', 'desc')
->get();
Error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'tags.tag_name' in 'field list' (SQL: select count(tag_id) as repetition, `tags`.`tag_name` from `tags_ref` inner join `tags` on `tags_ref`.`tag_id` = `tags`.`id` group by `tag_id,tag_name` order by `repetition` desc)
groupByso you will get merged withgroupBytags.tag_nameinsideselecttag_namein groupBy and also changetags.*totags.tag_namein select