busboy10 busboy10 - 5 years ago 101
SQL Question

Using one cell to reference another database and combine the two in a query

I am trying to figure out if there is a way to take the value in a cell and replace it with the text name of a user that that number refers to in another table.

I have looked through subqueries but I don't think that is what I want. So for example my query comes back now as userID, state, country. And there is another table that has userID, name. I want to query the first database but to have userID replaced with its corresponding name from the other table.

Is that doable? I am using mySQL Workbench to make my queries.

Answer Source

If I understand what you need you could try this (after a backup!!)

UPDATE table1 SET userID = 
    (SELECT name FROM table2
     WHERE table2.userID = table1.userID)

If you just need to get results from two tables you could use

SELECT t2.name, t1.state, t1.country
FROM table1 t1 INNER JOIN table2 t2
ON t1.userID = t2.userID
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download