-1

i am Performing the MySQL query with Where In conditions.

Here is my Query.

The query should be:

SELECT * FROM users WHERE id IN (44,44,33,44,33,0);

Query showing my correct result, no problem at all, but what i want to do is Can we Divide all the id with the individual conditions ?

Or can a query has multiple Where In for a single column? like

SELECT * FROM users WHERE id IN (44) AND id IN (45) AND id IN (46);

like this.

Is that possible ? ?

My query code for performing the query, its in Laravel.

$films = Film::with('genre')->with('languages')->with('likes')->with('comments')->with('likedBy');

        if(Input::get('sort')){
            $sort = Input::get('sort');
            switch ($sort){
                case 'old_new':
                    $films = $films->orderBy('created_at', 'asc');
                    break;
                case 'new_old':
                    $films = $films->orderBy('created_at', 'desc');
                    break;
                case 'views':
//                    $films = DB::table('films')
//                        ->leftJoin('film_views', 'films.id', '=', 'film_views.film_id')
//                        ->select(DB::raw('films.*, count(film_views.film_id) as views'))
//                        ->groupBy('films.id')
//                        ->orderBy('views' , 'desc')
//                        ;

                    $films = $films
                        ->leftJoin('film_views', 'films.id', '=', 'film_views.film_id')
                        ->select(DB::raw('films.*, count(film_views.film_id) as views'))
                        //->whereBetween('created_at', [$this->first_day_of_search, $this->final_day_of_search])
                        ->groupBy('films.id')
                        ->orderBy('views' , 'desc')
                    ;

                    break;
                case 'likes':
                    $films = $films
                        ->leftJoin('film_likes', 'films.id', '=', 'film_likes.film_id')
                        ->select(DB::raw('films.*, count(film_likes.film_id) as likes'))
                        ->groupBy('films.id')
                        ->orderBy('likes' , 'desc')
                       ;
                    break;
            }


        }
        if(Input::get('filter')) {
            $jsonFilter = Input::get('filter');
            $filters = json_decode($jsonFilter);

            foreach ($filters as $filter => $value){
                switch ($filter){
                    case "genre":
                        if($value){
                            $films = $films->whereHas('genre', function ($query) use($value) {
                                $query->whereIn('genre_id', $value);
                            });
                        }
                        break;
                    case "cert":
                       if($value){
                           $films = $films->whereIn('cert', $value);
                       }
                        break;
                    case "country":
                        if($value){
                            $films = $films->whereIn('country', $value);
                        }
                        break;
                    case "lang":
                       if($value){
                           $films = $films->whereHas('languages', function ($query) use($value) {
                               $query->whereIn('language_id', $value);
                           });
                       }
                        break;
                }
            }


        }



        $films = $films->paginate(5);

        return parent::api_response($films->toArray(), true, ['return' => 'all films'], 200);
Punit Gajjar
  • 4,937
  • 7
  • 35
  • 70

2 Answers2

2

I will post an answer if for no other reason than amusement. I believe that a WHERE IN clause is internally converted into a series of equality onditions separated by OR. So your original query

SELECT * FROM users WHERE id IN (44,44,33,44,33,0)

would be internally converted to this

SELECT * FROM users WHERE id = 44 OR id = 44 OR id = 33 OR id = 44 OR
                          id = 33 OR id = 0

So having a single number for your IN clause would be equivalent to a single equality condition.

By the way, you have the same numbers appearing multiple times, which doesn't make any sense.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

Yes, it is possible to have multiple WHERE IN.

SELECT * FROM users WHERE id IN (44) OR id IN (45) OR id IN (46);

This is correct.

However I don't understand why you want to do this because this is low in performance compared to having one IN

Punit Gajjar
  • 4,937
  • 7
  • 35
  • 70
Malinga
  • 505
  • 3
  • 14
  • 2
    This is about as ridiculous as it gets. – Drew Jun 28 '16 at 04:45
  • Sorry didnt get you – Malinga Jun 28 '16 at 04:50
  • I really should remove my downvote. The problem is the question, not this so much. But it is still ridiculous. – Drew Jun 28 '16 at 04:50
  • Let's say the `in` clause was 92 items. What might it look like? And why would put the db engine thru that? – Drew Jun 28 '16 at 04:52
  • @Drew: As i saw he asked "can a query has multiple Where In for a single column" I said yes, thats all – Malinga Jun 28 '16 at 04:53
  • Yeah, but you are supposed to be the `mysql` guru here. And you need to decipher question gibberish and steer it in the right direction. Why would anyone code like that, when it clogs the parser down? Why not just tack a hundred `and 1=1` onto the end for good effect. – Drew Jun 28 '16 at 04:54
  • @drew: Ya this is pointless. I know. But he might have some sets of ids where it comes from separate queries or something, that need to be added in separate IN s – Malinga Jun 28 '16 at 04:54
  • That would be even worse. – Jorge Campos Jun 28 '16 at 04:56
  • Downvote removed. No fault of yours the thing is so unclear. – Drew Jun 28 '16 at 04:59