I have two tables: table 1 = university and table 2 = school
I added university_id into the table 2 and I need to connect the two tables.
If university_name from table 1 and name from table 2 are identical, get the id from table 1 and replace it onto table 2 university_id
I am very new to sql so if you could explain that would be great. I have also tried the following with no avail!
select a.id,b.name from university as a
inner join school as b on a.university_name = b.name
UPDATE `school` SET `university_id` = a.id WHERE a.university_name = b.name
Something like
UPDATE school a
JOIN university b ON a.university_name = b.name
SET a.university_id = b.id
should work
I cannot run a test right now... Maybe it does give you a hint.
UPDATE `school` s SET `university_id` = (SELECT u.id FROM `university` u WHERE u.name=s.university_name)
You might need to JOIN the school-table within the SELECT statement.
Related
This is a different question because I have joined 2 tables. The solutions for the duplicate question indicated does not work for me.
This is the query:
SELECT a.id, a.userName, IF(o.userId = 1, 'C', IF(i.userId = 1,'I','N')) AS relation
FROM tbl_users AS a
LEFT JOIN tbl_contacts AS o ON a.id = o.contactId
LEFT JOIN tbl_invites AS i on a.id = i.invitedId
ORDER BY relation
This returns the output as follows:
id username relation
1 ray C
2 john I
1 ray N
I need to remove the third row from the select query by checking if possible that id is duplicate. I tried adding distinct(a.id) but it doesn't work. How do I do this?
I have the following 2 queries:-
Query 1:-
mysql> select mccid_c, id_c from contacts_cstm where accountnumber_c = '1601480000552527';
250762 | 475093000013882513
Query 2:-
mysql> select first_name, last_name from contacts where id = '475093000013882513';
John | Doe
id in contacts = id_c in contacts_cstm
I required a join query to get mccid_c, first_name, last_name in one query
Thanks!
This is a classic usecase of the join syntax:
SELECT mccid_c, first_name, last_name
FROM contacts_cstm cc
JOIN contacts c ON c.id = cc.id_c
WHERE c.id = '475093000013882513'
You can use multiple tables in your single SQL query. The act of joining in MySQL refers to smashing two or more tables into a single table.
You can use JOINS in SELECT, UPDATE and DELETE statements to join MySQL tables.But firstly you need a common column (attribut),suppose it is 'tutorial_author' between the two tables that you want to join it.
SELECT a.mccid_c, b.first_name , b.last_name
FROM contacts_cstm a, contacts b
WHERE a.tutorial_author = b.tutorial_author and a.accountnumber_c = '1601480000552527' and b.id = '475093000013882513';
I hope it will help you, best regards
Try this.
Select a.mccid_c, b.first_name, b.last_name from contacts_cstm a inner join contacts b where a.id_c=b.id;
I've got a problem with a query with 2 tables
Table A
id (key), name
Table B
id, a_id (foreign key), language
In the first table you can find a lot of people and in the second languages they can speak. My problem now is, that I want to find all the people that can speak English and German (not one of them but both!) for example.
Do you have any idea to solve this problem?
this should work, assuming your language table looks something like this
https://www.dropbox.com/s/bgk0rjo8g86rfz5/Screenshot%202014-08-11%2022.20.55.png
SELECT a.*
FROM b
JOIN a ON a.id = b.user_id
WHERE b.language = 'German'
OR b.language = 'English'
GROUP BY b.user_id
HAVING COUNT(*) = 2;
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
Suppose I have the following table structure:
TABLE 1
main_id | type | information
first segway excellent
second car mercedes
third bike sliceofwind
TABLE segway
id | grade
1 excellent
2 bad
3 (...)
TABLE car
id | brand
1 mercedes
2 honda
3 (...)
TABLE bike
id | tires
1 sliceofwind
2 flatasfaque
3 (...)
What I'd like to do is dinamically obtain information from different tables based on type from table1.
Here's the generic example of a query that I've tried
SELECT (CASE
WHEN table1.type = 'segway' AND segway.grade = table1.information
THEN segway.id,
WHEN table1.type = 'car' AND car.brand = table1.information
THEN car.id,
WHEN table1.type = 'bike' AND bike.tires = table1.information
THEN bike.id
END) AS information
FROM table1,segway,bike,car WHERE table1.main_id IN ("ids")
The result of this query is a cartesian product because all the data from all tables will be retrieved despite the restrictions inside the case because not all tables have restrictions.
I'd like to know if there is a way to work around this without changing the table structure, and if not plea for some hints! (I'm up to some kinky sql stuff, what I'm asking here if it is indeed possible to do this, despite it being advised or not and why!).
This might be one way to do it.
SELECT t1.*
FROM table1 t1
LEFT JOIN segway s
on T1.main_id = s.id and T1.type= 'segway'
LEFT JOIN car c
on T1.main_id = c.id and T1.type= 'car'
LEFT JOIN bike b
on T1.main_id = b.id and T1.type= 'bike'
WHERE t1.main_ID in (SomeList)
segway, car, and bike table columns will be null when the Table1's type doesn't match.
However this seems like it would give you back more data/columns than you need. I think you'd be better off writing separate queries outside the database and call them depending on the value they select. OR using a procedure within the database and conditional logic to return the desired result set. (again 3 separate queries and conditional logic in the database) but without understanding use case, I can't really say which would be better.
We could further coalese the brand, tires and grade into a "Value" field as in
Select t1.*, coalese(s.grade,c.brand,b.tires) as value but I'm not sure this offers any help either.
if we needed to only return table 1 values and set values from the other tables... you said kinky, not me.
I can't see how the Cartesian would occur this way.
This will be your expected result, try this query..
SELECT (CASE WHEN s.`id` IS NOT NULL THEN s.`id`
WHEN c.`id` IS NOT NULL THEN c.`id`
WHEN b.`id` IS NOT NULL THEN b.`id`
END) AS information FROM table1 AS t1
LEFT JOIN segway s ON t1.type= 'segway'
LEFT JOIN car c ON t1.type= 'car'
LEFT JOIN bike b ON t1.type= 'bike'
WHERE t1.main_ID IN (1, 2, 3) AND (t1.information = s.`grade` OR
t1.`information`=c.brand OR
t1.`information`=b.tires);