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

daugaard47's avatar

How to write query to exclude specific matching records

I have this mock table rich_guys. Notice there are records with the same slug 1|6 & 3|5. Also 2 slugs have a foo_id 3|6.

+----+------------------------------+------------+
| id | slug                         | foo_id     |
+----+------------------------------+------------+
|  1 | bill-gates                   |       NULL |
|  2 | jeff-bezos                   |       NULL |
|  3 | steve-jobs                   |         23 |
|  4 | elon-musk                    |       NULL |
|  5 | steve-jobs                   |       NULL |
|  6 | bill-gates                   |         64 |
|  7 | mark-zuckerberg              |       NULL |
+----+------------------------------+------------+

How would I write a query so records 1 and 5 are removed and the rest remain?

Simulated output:

+----+------------------------------+------------+
| id | slug                         | foo_id     |
+----+------------------------------+------------+
|  2 | jeff-bezos                   |       NULL |
|  3 | steve-jobs                   |         23 |
|  4 | elon-musk                    |       NULL |
|  6 | bill-gates                   |         64 |
|  7 | mark-zuckerberg              |       NULL |
+----+------------------------------+------------+

Talking it out, I'm thinking I'd need to do something like this, but not sure how to write the query.

get all richGuys  
if guy has foo_id and matching slug  
show guy with foo_id  
if guy has no foo_id and no matching slug  
show guy  
return richGuys

I'm looking for advice, or a tutorial with this kind of edge case scenario that would help me complete this task. Although I would not frown upon a helpful solution. =)

0 likes
7 replies
daugaard47's avatar

@Sinnbeck That might help, but in this case I think I would still need a distinct() with some kind of where or if clause. Right?

daugaard47's avatar

@Sinnbeck That's my problem lol. I'm not sure how I would go about writing it. I'm looking into SubQueries now. I think that might help me out. I'll show some code once I figure this out more.

Tray2's avatar

@daugaard47 Are you sure this is just one query, because it's sounds like more than one.

  1. get all richGuys
  2. if guy has foo_id and matching slug
  3. show guy with foo_id
  4. if guy has no foo_id and no matching slug
  5. show guy

Queries

  1. RichGuy::all()
  2. RichGuy::whereNotNull('foo_id, ')->where('slug', $request->slug)->get()
  3. RichGuy::where('foo_id', $request->foo_id)->get()
  4. RichGuy::whereNull('foo_id')->where('slug', '<>', $request->slug)->get()
  5. RichGuy::findOrFail($request->id)
daugaard47's avatar

@Tray2 I guess that's where I'm stuck. I using the same table so I thought I'd be able to do it in one query, but I was exploring using 2 queries and then merging them together.

Something like this:

$richGuysDefault = RichGuy::where('foo_id', null); // Get all records
$richGuysSpecific = RichGuy::where('foo_id',  $this->foo->foo_id); // Gets the records where there is a foo_id
$richGuys = $richGuysDefault->merge($richGuysSpecific); // Merge is not working here

$this->foo->foo_id is coming from another table that's being passed in to the page.

Then if I had the tables merged I could probably use a couple If statements to output the results I need in the view. Not sure how to merge that correctly though, also wasn't sure if that was the best way. Hints why I'm here asking for advice.

Tray2's avatar

@daugaard47 I see.

Something like this then?

SELECT *
FROM rich_guys
WHERE (foo_id IS NOT NULL AND slug IN ('bill-gates', 'steve-jobs'))
OR (foo_id IS NULL AND slug NOT IN ('bill-gates', 'steve-jobs'))
ORDER BY id;

Please or to participate in this conversation.