Reputation: 28555
I have 2 tables:
Appointments
id serviceId
1 1
2 1
Services
id
1
The appointments table is linked to the services table by use of the serviceId column. When retrieving data from the appointments table, i'd like to also retrieve the data from the services table, but the number of columns in each table does not match.
I've tried this:
SELECT appointments.*, services.* FROM appointments
INNER JOIN services ON id = appointments.serviceId
WHERE id = ?
But this does not work. First of all, in the join, how can I reference an ID from the appointments table that I have not even retrieved the data for yet? Second, how can I get all the data from both tables and then retrieve the data if the two column names match?
This won't work:
results.getInt("id");
Because both tables have an id field.
Upvotes: 0
Views: 67
Reputation: 263723
specify the tables name in which ID
belongs,
SELECT appointments.*, services.*
FROM appointments
INNER JOIN services
ON Services.id = appointments.serviceId
-- WHERE id = ? -- specify also here as the two tables have column ID
or add ALIAS
around them
SELECT appointments.id as AppID,
appointments.serviceId as AppServiceID
services.id AS ServiceID
FROM appointments
INNER JOIN services
ON Services.id = appointments.serviceId
-- WHERE id = ?
when you get their values, use their alias now, something like this,
results.getInt("AppID");
Upvotes: 1