Showing posts with label group_concat. Show all posts
Showing posts with label group_concat. Show all posts

Saturday, December 16, 2017

MYSQL Group Concat Select Some Selected Rows Only | Use Sort In Group Concat | Sort MySQL Rows in Group Concat

Its so simple. Just need to do below thins:
SELECT SUBSTRING_INDEX(GROUP_CONCAT(x.id ORDER BY x.nx DESC), ',', 2) as row_name from some_table GROUP BY some_field
It will select First two values only.
It total value of GROUP_CONCAT is "1,2,3,4,5" Then Using SUBSTRING_INDEX would be like "1,2"
You can use DISTINCT in GROUP_CONCAT function like
SUBSTRING_INDEX(DISTINCT(GROUP_CONCAT(x.id ORDER BY x.nx DESC)), ',', 2)

Saturday, February 11, 2017

Laravel group concat operation

It is important to get some concat value. You can use GROUP_CONCAT in Laravel easily. Below is a code snippet to do the trick:


$result = DB::table('table_name')
       ->select(DB::raw('group_concat(field_name) as field_name_alias'))
       ->where('condition_field', '=', $condition_value )
       ->first();

Wednesday, September 4, 2013

Concatenate/concat/group_concat two columns/rows with MySQL query

Two/more rows concatenate-
You can use GROUP_CONCAT.
As in:
select person_id, group_concat(hobbies separator ', ')
    from peoples_hobbies group by person_id;
Death: As Dag stated in his comment, there is a 1024 byte limit on result. to solve this run this query before your query:
set group_concat_max_len=2048
Off course, you can change 2048 accourding to your needs.

Two/more columns concatenate-
You can use the CONCAT function like this:
SELECT CONCAT(SUBJECT, ' ', YEAR) FROM table