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

keevitaja's avatar

Mysql: multiple tables join

Hello!

I have 2 tables:

entities:

id

statuses:

id
entity_id
status

Each entity can have many statuses and i need to select only the entities that have 'paid' status and do not have 'cancel' status. How do i do a proper join for that?

Not using eloquent, but raw queries.

Example:

entities: id 7
entities: id 8
entities: id 9
entities: id 10
statuses: id 1, entity_id 7, status 'paid'
statuses: id 2, entity_id 7, status 'canceled'
statuses: id 3, entity_id 8, status 'paid'
statuses: id 4, entity_id 10, status 'onhold'

Only entiry with the id of 8 will be selected once.

0 likes
1 reply
Dunsti's avatar

What about a simple WHERE-clause?

select en.id, st.id, st.status 
from entities as en 
inner join statuses as st 
on en.id = st.entity_id
where st.status = 'paid'

Please or to participate in this conversation.