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

189 views Asked by At

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.

2

There are 2 answers

3
Marco On BEST ANSWER

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
0
AudioBubble On

Like this?

select u.username, l.logintime, l.logouttime
from loginouttable l
join usertable u on l.userid = u.userid