Reputation: 33
I need help with SQL query. I've been trying to figure it out for a week.
I need it to get data from both tables, but if the date and sport are the same as in the second table, the data will only be taken from the first table.
Each table can have 4 sports with the ID from the connected table.
So the date can be the same 4 times, but each time with a different sport. That means if in the first table the date is 2020-01-01 and sport is ID 1 and in the second table the date is 2020-01-01 and sport ID 2 the data will be taken from both tables. But if in the first table the date is 2020-01-01 and sport ID 1 and in the second table the date is 2020-01-01 and sport ID 1, the data is taken only from the first table.
Is it possible to do it somehow? I will be happy for any idea. Thank you!
I use MariaDB. First table is named reservation and second one reservation_r.
Example with result:
Upvotes: 1
Views: 94
Reputation: 1269753
I think you want union all
with some filtering:
select r.*
from reservation r
union all
select rr.*
from reservation_r rr
where not exists (select 1
from reservation r
where r.customer_idcustomer = rr.customer_idcustomer and
r.date = rr.date and
r.sport_idsport = rr.sport_idsport
);
Upvotes: 1