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.
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");
Related
I have 4 tables containing id and names from different fields, and a master table that contains only ids, i need to create a query that return the names.
This is the structure (simplified)
table region = columns id, name
table country = columns id, name
table ethnics = columns id, name
table religion = columns id, name
table master = columns region, country,ethnics, religion
table master contains ONLY ids for each column, and i need to return the names that matches those ids, but i can't create the proper JOIN syntax.
Any hint?
Try this:
select region.name, country.name, ethnics.name, religion.name
from master
join region on (region.id = master.region)
join country on (country.id = master.country)
join ethnics on (ethnics.id = master.ethnics)
join religion on (religion.id = master.religion)
Then you can add any where clauses that you might need to filter the results.
I have two tables:
customer, company
The customer table contains company_id and email_address while the company table contains company_id and URL
I'd like to run a query to update all the customer table's company_id to the matching company_id based on a partial match -- specifically if the customer's email_address contains the URL.
For example, if customer has customer#google.com it checks the company table to see if that email contains the URL google.com, and if so, it gives the customer the matching company_id from the company table.
What's an efficient way to do this in single query? Thanks!
I think this would do exactly what you want:
UPDATE customer
LEFT JOIN company
ON (customer.email LIKE CONCAT('%#', company.URL, '%'))
SET customer.company_id = company.company_id
WHERE company.company_id IS NOT NULL;
http://sqlfiddle.com/#!9/89765/1
For an example lets say I have table INFO that contains columns:
ID - Name - Address
I also have a second table PURCHASES that contains columns:
Region - Name - Purchases
Multiple people can be in the same region, but each person only has one ID.
I want to write a query that will, based on a given ID in the INFO table, return all the rows in PURCHASES of people who live in the same region as the person with the specified ID.
I have done an Inner Join on Name for the two tables but can't figure out the best way to write a query.
Edit: My main problem is that there is no Region column in INFO. The only way to get the region is by joining to the PURCHASES table. Then I need the results of all rows containing that Region.
I am not sure if this is exactly what you want but you could probably twik it a bit to better fit your needs:
SELECT
Purchasse
FROM
PURCHASSE
INNER JOIN
INFO ON INFO.Name = PURCHASSE.Name
WHERE
INFO.ID = yourID
This should give you Purchasse for any given ID that the Name match for the two columns.
Try this:
Select * from PURCHASES LEFT OUTER JOIN INFO ON PURCHASES.NAME = INFO.NAME WHERE INFO.ID = givenID
I have two tables: teams, and persons.
table teams have three columns, id, name, leader
table persons have these columns: hash, team_id
teams.leader is a MD5 hash that must match persons.hash in order to determine which person is leader of a given team.
I need to run a query on MySQL that does the following:
1) Retrieve all the leaders of a team, and the team id:
SELECT `id`,`leader` FROM `teams`;
2) Use such information to update team_id on table persons
This is my current Query:
SELECT id FROM teams INNER JOIN persons ON teams.leader = persons.hash
but I haven't been able to come up with a solution that allows me update column team_id with the corresponding leader.
I've been thinking probably using cursors, but not sure about it.
Any ideas?
You can use the multiple table UPDATE syntax to join the tables:
UPDATE teams JOIN persons ON teams.leader = persons.hash
SET persons.team_id = teams.id
I have to join the data from two tables.
There are three table record,address and names.Id is the primary key in record table and foreign key in address and names table.There can be multiple name records for particular record.
RECORD(id (PK),text,search)
address(address_id(PK),type,street,city, id (FK))
name(name_id(PK),name,id (FK))
I want to get the data from these three tables in a way so that i will get the only first name record for each record id,not all the name records for record id's.
insert into RECORD values (1,record1,record1search);
insert into RECORD values (2,record2,record2search);
insert into name values (1,xxx,1);
insert into name values name(2,yyy,1)
insert into name values name(3,zzz,2)
i want to retriev the values in way:
record_id,text,namename_id:
1,record1,xxx,1
2,record2,zzz,3
I want the only first name record not the second or thirrd name record for particular record.
please help.
You will need an interim query on name by the record ID to find the FIRST ID and use THAT as basis to join to the actual name table. Also, you didn't provide any sample data for your address table, but that would be done in a similar fashion as the first names query performed here. Also, would the address table be associated to a specific named person and not just the ID? Will THAT be 1:many relationship to the Record ID?
select
r.id,
r.text,
n2.name,
n2.name_id
from
Record r,
join ( select n.id, min( n.name_id ) FirstNameID
from name n
group by n.id ) FirstNames
on r.id = FirstNames.id
join name n2
on FirstNames.FirstNameID = n2.name_id
SELECT RECORD.id, RECORD.text, name.name, name.name_id
FROM RECORD
LEFT JOIN address
ON address.id = RECORD.id
LEFT JOIN name
ON name.id = RECORD.id
GROUP BY RECORD.id