0

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
busboy10
  • 41
  • 4
  • Just to clarify: do you want to change the value that is stored in the table, or simply to display the corresponding name in a query? –  Jan 23 '12 at 18:33

2 Answers2

0

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
Marco
  • 56,740
  • 14
  • 129
  • 152
  • I cannot even theoretically envision a universe in which the result of that update query would be desirable. (I suspect the second query is what's wanted.) – Dan J Jan 23 '12 at 19:27
  • @djacobson: I can't understand if OP needs to update first table or join two tables. And my doubt has been told from Mark too with his comment... – Marco Jan 23 '12 at 19:29
  • @user1165536: as you're new here, I remember that you should [accept an answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) if you think it solved (or helped to solve) your problem. – Marco Jan 23 '12 at 21:00
0

Like this?

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