1

Getting following error

SQLSTATE[HY000]: General error: 1390 Prepared statement contains too many placeholders

My query has become due to data in the table recently

SELECT
  *
FROM
  table
WHERE col1 = 'some-val'
  AND col2 NOT IN ('va1', 'val2', 80k values... )

I am using Laravel 5.6

$data_will_be_skipped = OtherModel::select('code')
        ->where('col1', 0)
        ->orWhere('col2', 1)
        ->groupBy('col3')
        ->pluck('col3')->toArray();

$data_will_be_skipped is now approx 80k arrays

Model::where('col1', 'some-val')->whereNotIn('col2', $data_will_be_skipped)->get();
4
  • 2
    Where are you getting the values for not in array ? if they are from some tables in the DB only. consider using joins instead of not in. Whether using laravel or not, not in itself has packet size limits Commented Oct 14, 2018 at 10:11
  • Your current approach is not efficient from indexing perspectives as well Commented Oct 14, 2018 at 10:12
  • yes not-in values are being get by some other table in the db.
    – zarpio
    Commented Oct 14, 2018 at 10:12
  • Please update your question to add more details about which tables are you getting them from. What is the relationship between the two tables ? What is the logic behind getting these values from the other table ? Commented Oct 14, 2018 at 10:21

2 Answers 2

3

MySQL has a limit of 65,535 parameters in prepared statements.

Use a subquery instead:

$data_will_be_skipped = OtherModel::select('col3')
    ->where('col1', 0)
    ->orWhere('col2', 1);
Model::where('col1', 'some-val')
    ->whereNotIn('col2', $data_will_be_skipped)
    ->get();
5
  • Awesome :-) you are champ!
    – zarpio
    Commented Oct 15, 2018 at 10:17
  • sure, I was just accepting and I had to attend the meeting.
    – zarpio
    Commented Oct 15, 2018 at 21:11
  • I am still getting error: SQLSTATE[HY000]: General error: 1390 Prepared statement contains too many placeholders (SQL: select callDate, uniqueid from cdr where uniqueid not in (1594624402.0, 1594624402.4, 1594624425.18, 1594624425.22, 1594624425.32, 1594624448.52, 1594624578.78, 1594624578.82, 1594624620.111, 1594624620.115, 1594624620.119, 1594627078.156.......................... Commented Dec 10, 2020 at 4:45
  • @JaberAlNahian - I'm having the same problem. Did you get a solution?
    – Tom
    Commented Aug 27, 2021 at 22:39
  • @Tom I forgot, maybe I had to increase limit Commented Aug 29, 2021 at 8:58
0

No matter how many items u have in array to check in wherein

the simple way to do it

$data = model::whereRaw("id not in (".implode(',',$data_will_be_skipped).")")->get();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.