I have three tables and in those tables these basic fields
contacts
con_id (primary key)
con_name
con_work_id
con_country_id
work
work_id (primary key)
work_company_name
work_country_id
country
country_id (primary key)
country_name
I'm trying to run a query which displays the con_name, work_company_name and then the country name for BOTH the contact and the work company.
I've tried this;
SELECT *
FROM contacts
LEFT JOIN work ON contacts.con_work_id = work.work_id
LEFT JOIN country ON contacts.con_country_id = country.country_id
LEFT JOIN country ON work.work_country_id = country.country_id
But of course this doesn't work because the last join causes a clash with the second one.
I'm almost there, but can't get the query to display the country_name associated with both the contact AND the work company.
I'd appreciate a way forward.
Many thanks,
Wonder
The following should work:
SELECT *
FROM contacts
LEFT JOIN work ON contacts.con_work_id = work.work_id
LEFT JOIN country c1 ON contacts.con_country_id = c1.country_id
LEFT JOIN country c2 ON work.work_country_id = c2.country_id
The trick is to add an alias to the table, so that it is possible to distinguish the two.
Related
I have 3 tables in my database as follows:
labs
id
lab_name
lab_owner_id (references id in labs_members table)
lab_manager_id (references id in labs_members table)
labs_members
id
person_id (references id in labs_person table)
labs_person
id
first_name
last_name
I need to construct a query to get all of the information from the labs table including the first and last names of the lab owner and lab manager.
I've tried variations of the following query:
SELECT labs.id, labs.lab_name, labs.lab_owner_id, labs.lab_manager_id
FROM labs
LEFT JOIN labs_members on labs.lab_owner_id = labs_members.id
LEFT JOIN labs_members on labs.lab_manager_id = labs_members.id
LEFT JOIN labs_person on labs_person.id = labs_members.person_id
ORDER BY labs.lab_name ASC
However, I haven't had any success getting both the lab manager and lab owner names at the same time. I get an error, or either the lab manager name OR the lab owner name (but not both).
Any assistance is greatly appreciated. Thanks!
You are very very close. One more join to the person table and some table aliases and you'll be there:
SELECT labs.id, labs.lab_name, labs.lab_owner_id, labs.lab_manager_id, owner_person.first_name, owner_person.last_name, manager_person.first_name, manager_person.last_name
FROM labs
LEFT JOIN labs_members as owner on labs.lab_owner_id = labs_members.id
LEFT JOIN labs_members as manager on labs.lab_manager_id = labs_members.id
LEFT JOIN labs_person as owner_person on owner_person.id = owner.person_id
LEFT JOIN labs_person as manager_person on manager_person.id = manager.person_id
ORDER BY labs.lab_name ASC
What currently happens is it just selects students from CA and students who like to skateboard. I need it to return only students who are both from CA and play soccer.
SELECT *
FROM schooldata a
INNER JOIN studentinfo b
ON b.schooldata_id = a.id
WHERE a.state = "ca"
AND ( activity = "soccer"
OR activity = "skateboard" )
You will have to do inner join based on lastname and firstname column ex:-b.lastname=a.lastname and b.firstname=a.firstname . ideally you should be maintaining primary key column of type integer in schooldata table and its foreign key reference in studentinfo and join based on those columns.
You should use join clauses. And I think base on your question. Inner Join is the best clause you should use.
SELECT column_name(s)
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;
I am trying to optimise my php by doing as much work on the MySQL server as possible. I have this sql query which is pulling data out of a leads table, but at the same time joining two tags tables to combine the result. I am looking to add a company which is linked through a relations table.
So the table that holds the relationship between the two is relations_value which simply states (I add example data)
parenttable (companies) | parentrecordid (10) | childtable (leads) | childrecordid (1)
the companies table has quite a few columns but the only two relevant are;
id (10) | companyname (my company name)
So this query currently grabs everything I need but I want to bring the companyname into the query:
SELECT leads.id,
GROUP_CONCAT(c.tag ORDER BY c.tag) AS tags,
leads.status,
leads.probability
FROM `gs_db_1002`.leads
LEFT JOIN ( SELECT *
FROM tags_module
WHERE tagid IN ( SELECT id
FROM tags
WHERE moduleid = 'leads' ) ) as b
ON leads.id = b.recordid
LEFT JOIN `gs_db_1002`.tags as c
ON b.tagid = c.id
GROUP BY leads.id,
leads.status,
leads.probability
I need to be able to go into the relations_values table and pull parenttable and parentrecordid by selecting childtable = leads and childrecordid = 1 and somehow join these so that I am able to get companyname as a column in the above query...
Is this possible?
I have created a sqlfiddle: sqlfiddle.com/#!2/023fa/2 So I am looking to add companies.companyname as column to the query.
I don't know what your primary keys and foreign keys are that link each table together.. if you could give a better understanding of what ID's are linked to eachother it would make this a lot easier... however i did something that does return the correct result... but since all of the ID's are = 1 then it could be incorrect.
SELECT
leads.id, GROUP_CONCAT(c.tag ORDER BY c.tag) AS tags,
leads.status, leads.probability, companyname
FROM leads
LEFT JOIN (
SELECT * FROM tags_module WHERE tagid IN (
SELECT id FROM tags WHERE moduleid = 'leads' )
) as b ON leads.id = b.recordid
LEFT JOIN tags as c ON b.tagid = c.id
LEFT JOIN relations_values rv on rv.id = b.recordid
LEFT JOIN companies c1 on c1.createdby = rv.parentrecordid
GROUP BY leads.id,leads.status, leads.probability
The are two tables "costs" and "contacts". Names off all sellers and buyers are in "contacts" table. With following query i retrieve the id of seller and buyer for each item but I want to get their names from "contacts" table
SELECT
costs.id as ID,
costs.idContactPayedBy,
costs.idContactPayedTo
FROM costs
WHERE
costs.idbuilding=286
but I want to get seller and buyers names from contacts table
SELECT
costs.id as ID,
contacts.lastname as seller,
contacts.lastname as buyer
FROM costs , contacts
WHERE
costs.idbuilding=286
and costs.idContactPayedBy = contacts.id
and costs.idContactPayedTo = contacts.id
so the desired result is like this
ID Seller Buyer
21 jackson Brown
29 Bush wilson
SELECT
c.id as ID,
cntby.lastname as seller,
cntto.lastname as buyer
FROM costs AS c
INNER JOIN contacts AS cntby ON c.idContactPayedBy = cntby.id
INNER JOIN contacts AS cntto ON c.idContactPayedTo = cntto.id
WHERE c.idbuilding=286
Note 1: Use INNER JOIN only if idContactPayed[By/To] columns are mandatory (NOT NULL). If these columns allows nulls then you should use LEFT OUTER JOIN. In my opinion, both columns should be mandatory.
Note 2: As a matter of style: please avoid old style joins (ANSI 86/89): FROM table1 a, table2 b WHERE <join condition>.
The problem is that I'm using a table that has more than one reference to another table; basically I'm storing three different values for the original artist of a song, if it is featuring another artist, and if it is a remix or not. Each of these link to the artist table and what I want is to be able to return the artist name (no other data) for each of these columns. I have this so far:
SELECT `songName`, `songTrackNumber`, `artist`.`artistName`,
songFeaturingArtist, songRemixByArtist
FROM `song`
LEFT JOIN artist ON songArtist = artistID
I'm not entirely sure how to convert the other two fields from their numeric ID to the name of the artist. Any pointers would be greatly appreciated.
Join table Artist on table song three times to get the artistName for all the other columns on table song.
SELECT a.songName,
a.songTrackNumber,
b.artistName AS ArtistName,
c.artistName AS FeaturingArtist,
d.artistName AS RemixByArtist
FROM song a
LEFT JOIN artist b ON a.songArtist = b.artistID
LEFT JOIN artist c ON a.songFeaturingArtist = c.artistID
LEFT JOIN artist d ON a.songRemixByArtist = d.artistID
Just keep adding joins, but by using table aliases to keep MySQL happy:
SELECT `songName`, `songTrackNumber`,
`a1`.`artistName` AS mainArtistName,
`a2`.`artistName` AS songFeaturingArtistName,
`a3`.`artistName` AS songRemixByArtistName
FROM `song`
LEFT JOIN artist a1 ON songArtist = a1.artistID
LEFT JOIN artist a2 ON songFeaturingArtist = a2.artistID
LEFT JOIN artist a3 on songRemixByArtist = a3.artistID
For clarity you could change the aliases to something more useful than a simple numeric suffix :)