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!
Use orWhere
Language::where('language', 'english')->orWhere('language', 'german')->get();
Thanks! However the query is an AND, not an OR.
How to select users who speak english AND spanish AND german?
What exactly do you store in language column?
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();
@michaloravec Sorry, chaining "where" like this doesn't seem to work
What do you have in that language column?
Regardless of what's in the language column (text or id), chaining multiple "where" doesn't return anything
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.
I tried language column as VARCHAR 255 and as INT id, in both cases chaining multiple "where" doesn't return anything
I don't see how that solution is related to the original question. But good that you found a solution
Sometimes solutions are just too specific to the app/setup.
@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.