Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

calin.ionut's avatar

query table based on multiple values

I have this table:

 -------------------------------------------------------------------------
| p_id | i_id | o_id | v_id |
 --------------------------------------------------------------------------
|65     |  1  |   2    |  12  |    
|65     |  1  |   4    |  9    |  
|65     |  2  |   2    |  3    |  	
|65     |  2  |   4    |  9    |  	
|65     |  3  |   2    |  4    |  	
|65     |  3  |   4    |  10  |	

How to query if I have p_id and 2 pairs of o_id and v_id ?

I need to know what is the i_id if I have this values:

p_id = 65, o_id=2, v_id=3 o_id=4, v_id=9

I was thinking that if I try this:

$variantValues = VariantValue::where('p_id', 65)
        ->where(array(['o_id', 2], ['v_id', 3]))
	->where(array(['o_id', 4], ['v_id', 9]))
        ->get();

will work....and then get from the first entry the i_id...but is not working.

0 likes
8 replies
calin.ionut's avatar

@michaloravec it't not working .... it return 3 entries

|65     |  1  |   4    |  9    |  
|65     |  2  |   2    |  3    |  	
|65     |  2  |   4    |  9    |  

instead of this

|65     |  2  |   2    |  3    |  	
|65     |  2  |   4    |  9    |  

MichalOravec's avatar

@calin.ionut Ok I add ->distinct() to the query

$variantValues = VariantValue::where('p_id', 65)->where(function ($query) {
    $query->where('o_id', 2)->where('v_id', 3);
})->orWhere(function ($query) {
    $query->where('o_id', 4)->where('v_id', 9);
})->distinct()->get();

But you have to add i_id to your where also.

https://laravel.com/docs/7.x/queries#selects

But you need get i_id, but if you have more rows in database, it's imposible to get what you want.

calin.ionut's avatar

@michaloravec this is the ideea .... that I don't have the i_id :)

the same thing.....3 entries.

I need something to get only where there is the same i_id value (in this case 2).

whereColumn('i_id','=','i_id')

but I get the same 3 entries.

MichalOravec's avatar

@calin.ionut Try this

$variantValues = DB::table('variant_values as t1')->where('t1.p_id', 65)->where(function ($query) {
    $query->where('t1.o_id', 2)->where('t1.v_id', 3);
})->orWhere(function ($query) {
    $query->where('t1.o_id', 4)->where('t1.v_id', 9);
})->join('variant_values as t2', 't2.i_id', '=', 't1.i_id')->select('t1.*')->distinct()->get();

And change variant_values to table name of your VariantValue.

calin.ionut's avatar

the same 3 entries:

Illuminate\Support\Collection {#567
  #items: array:3 [
    0 => {#570
      +"p_id": 65
      +"i_id": 1
      +"o_id": 4
      +"v_id": 9
    }
    1 => {#573
      +"p_id": 65
      +"i_id": 2
      +"o_id": 4
      +"v_id": 9
    }
    2 => {#548
      +"p_id": 65
      +"i_id": 2
      +"o_id": 2
      +"v_id": 3
    }
  ]
}
MichalOravec's avatar
Level 75

@calin.ionut Ok do it with collections

$variantValues = VariantValue::where('p_id', 65)->where(function ($query) {
    $query->where('o_id', 2)->where('v_id', 3);
})->orWhere(function ($query) {
    $query->where('o_id', 4)->where('v_id', 9);
})->distinct()->get();

$i_id = $variantValues->pluck('i_id')->duplicates()->first();

Please or to participate in this conversation.