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

itinerix's avatar

SQL: search for multiple conditions on same column?

In SQL, how to select all rows that satisfy multiple conditions of the same column? ("AND" wouldn't work)

Example: "select all users who speak (english AND spanish AND german)" where language is a column

Thanks!

0 likes
17 replies
Sinnbeck's avatar

Use orWhere

Language::where('language', 'english')->orWhere('language', 'german')->get();
itinerix's avatar

Thanks! However the query is an AND, not an OR.

How to select users who speak english AND spanish AND german?

MichalOravec's avatar

For example?

If it's like spaninsh german english then

$users = User::where('language', 'like', '%english%')
    ->where('language', 'like', '%spanish%')
    ->where('language', 'like', '%german%')
    ->get();
bugsysha's avatar

What do you have in that language column?

itinerix's avatar

Regardless of what's in the language column (text or id), chaining multiple "where" doesn't return anything

bugsysha's avatar

Regardless of what's in the language column (text or id), chaining multiple "where" doesn't return anything

šŸ¤£šŸ¤¦šŸ»ā€ā™‚ļø

OK, I see that you do not want help.

itinerix's avatar

I tried language column as VARCHAR 255 and as INT id, in both cases chaining multiple "where" doesn't return anything

Sinnbeck's avatar

I don't see how that solution is related to the original question. But good that you found a solution

bugsysha's avatar

Sometimes solutions are just too specific to the app/setup.

MichalOravec's avatar

@itinerix Next time when you want to help, be more clear...

$languageIds = [1, 2, 3];

$users = User::whereHas('languages', function($query) use ($languageIds) {
    $query->distinct()->whereIn('id', $languageIds);
}, '=', count($languageIds))->get();

Please or to participate in this conversation.