0

This is my Categories Table:

id  | name
 1    a
 2    b
 3    c
 4    d

In which I need to get the values of 1 and 2 in comma separated manner, i.e. a,b.

I tried something like this:

$ids = array('0'=>1,'1'=>2);
\DB::select('SELECT group_concat(name) as name from category where id = ?', ($ids));

But it always returns the first value (i.e., a)

I need the resultant values to be like this: a,b.

How could I do this either using normal query or by Laravel?

Thank you.

Rodia
  • 1,383
  • 7
  • 20
  • 28
Suganya Rajasekar
  • 665
  • 1
  • 14
  • 35

4 Answers4

3

What you want is a kind of concatenation that can be achieved by using GROUP_CONCAT like:

SELECT GROUP_CONCAT(name) FROM category where id IN(1,2)

Reference

Explanation: here GROUP_CONCAT(name) concat name as name1, name2 ... for the WHERE condition matched

Mayank Pandeyz
  • 24,624
  • 3
  • 35
  • 55
1

Try this using simple concat. You can concat as much as columns separated by commas.

Query - select concat(id,",",name) as name from category where id = ?

Rahul
  • 763
  • 1
  • 10
  • 42
1

Try this

$data = \DB::table("articles")
   ->select("title")
   ->whereRaw("find_in_set('find value',colum_name)")
   ->get();
Amit Meena
  • 171
  • 2
  • 5
0

POST or GET data field user_id = 1,2,3;

$user_array = explode(',',Input::get('user_id')); OR $user_array = explode(',',Input::POST('user_id'));
$all_user =   User::WhereIn('user_id',$user_array)->get();

Get all user which have User ID 1,2 and 3.

Prakash Pazhanisamy
  • 981
  • 1
  • 14
  • 25
abhishek kumar
  • 389
  • 3
  • 9