Hi all, I'm trying to construct a query but i'm only a beginner at SQL and i'm having
a little difficulty, wondered if there might be someone a little more experienced
who could give me a hand? The database is MySQL.
The tables and columns that i need to get are below, my limited (so far) knowledge
of SQL would allow me to do something like:
SELECT (the columns in the events table), (the columns in the venues table) ,CustomerId
FROM events, venues, bookings
WHERE venues.VenueId = events.VenueId
ORDER BY events.EventDate
Then i get a little lost in the WHERE clause, should it be AND bookings.EventId = events.EventId ?
(I want to get the events even if there are no bookings yet)
tables & columns =
events
.EventId
.VenueId
.EventDate
.EventAgeRangeFemaleFrom
.EventAgeRangeFemaleTo
.EventAgeRangeMaleFrom
.EventAgeRangeMaleTo
.EventStatus
.EventNotes
venues
.VenueId must = events.VenueId
.VenueCapacity
.VenueName
.VenueTown
bookings
.CustomerId
.EventId must = events.EventId
customers
.CustomerId must = bookings.CustomerId
.CustomerGender
These are all the fields that i need to get in my query.
It would seem to be a straightforward query but the complication comes
where for each event there will be a number of customers (hopefully), referred to in the
bookings table. In the customers table there is a column for CustomerGender.
This can be one of two values Male or Female. I need to count how many of each
there are (Male and Female) so that i can stop accepting bookings or say that
there are only a few places left etc.
I know that this works:
SELECT COUNT(CustomerGender) AS MaleCount
FROM customers
WHERE customers.CustomerGender = 'Male';
but i would need two of em' one for Male and one for Female and i'm having trouble
figuring out how to include this in the main events query. Would these be subqueries?
Any help would be much appreciated.
Ian
a little difficulty, wondered if there might be someone a little more experienced
who could give me a hand? The database is MySQL.
The tables and columns that i need to get are below, my limited (so far) knowledge
of SQL would allow me to do something like:
SELECT (the columns in the events table), (the columns in the venues table) ,CustomerId
FROM events, venues, bookings
WHERE venues.VenueId = events.VenueId
ORDER BY events.EventDate
Then i get a little lost in the WHERE clause, should it be AND bookings.EventId = events.EventId ?
(I want to get the events even if there are no bookings yet)
tables & columns =
events
.EventId
.VenueId
.EventDate
.EventAgeRangeFemaleFrom
.EventAgeRangeFemaleTo
.EventAgeRangeMaleFrom
.EventAgeRangeMaleTo
.EventStatus
.EventNotes
venues
.VenueId must = events.VenueId
.VenueCapacity
.VenueName
.VenueTown
bookings
.CustomerId
.EventId must = events.EventId
customers
.CustomerId must = bookings.CustomerId
.CustomerGender
These are all the fields that i need to get in my query.
It would seem to be a straightforward query but the complication comes
where for each event there will be a number of customers (hopefully), referred to in the
bookings table. In the customers table there is a column for CustomerGender.
This can be one of two values Male or Female. I need to count how many of each
there are (Male and Female) so that i can stop accepting bookings or say that
there are only a few places left etc.
I know that this works:
SELECT COUNT(CustomerGender) AS MaleCount
FROM customers
WHERE customers.CustomerGender = 'Male';
but i would need two of em' one for Male and one for Female and i'm having trouble
figuring out how to include this in the main events query. Would these be subqueries?
Any help would be much appreciated.
Ian