I'm almost new with SQL syntax and I need help to create a view on MySQL.
I have a table with a PK column called ID
, a column called total_seats
and another one is title
. In the second table, I have multiple rows, with a firstname
column and a FK that corresponds to the PK ( total_seats
) present in the first table.
I need to create a view where I can calculate the available_seats
( total_seats
minus occurrence in the second table) for each element present in the first table.
Actually I'm calculating the "occupied" seats but the join give me the result only for already taken event, so the result is that I don't see the available_seats
for the empty event.
SELECT b.ID_event, a.*,
COUNT(*) AS occupied FROM second_table b
LEFT JOIN first_table a ON b.ID_event = a.ID
GROUP BY ID_event
You could subtract the count
select a.ID_event, a.total_seats, count(*) as occupied, a.total_seats - count(*) difference
from first_table a
left join second_table ba ON b.ID_event = a.ID
group by a.ID_event, a.total_seats
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.