tags:

views:

374

answers:

4
+2  Q: 

MySQL Help

I need to run a query, and Im not sure if it's possible. I could do 2 queries but one is better right :|

I have two tables. First is user, this consists of username and userid. The second is search which consists of userid and query. When I select the search table I want user ID to be replaced by username by taking the data from the user table. Is this making sence?

+-------+----------+
|userid | username |
+-------+----------+
|    1  |   foo1   |
+-------+----------+
|    2  |   foo2   |
+-------+----------+
|    3  |   foo3   |
+-------+----------+
|    4  |   foo4   |
+-------+----------+

+-------+----------+
|userid |   query  |
+-------+----------+
|    1  |   blah1  |
+-------+----------+
|    2  |   blah2  |
+-------+----------+
|    3  |   blah2  |
+-------+----------+
|    4  |   blah2  |
+-------+----------+
+27  A: 

You are looking for an inner join. This would do it:

SELECT s.query, u.username
FROM search s
INNER JOIN users u
ON s.userid = u.userid
Paolo Bergantino
Well that was a weird downvote...
Paolo Bergantino
upvote for including a link.
seth
Good answer .
George Stocker
Why would linking something easily searchable persuade you to upvote?
orlandu63
A: 
SELECT U.username, S.query 
FROM User U 
INNER JOIN Search S ON (U.userid = S.userid)
Binoj Antony
+1  A: 
SELECT u.`username`, s.`query` 
    FROM `search` s
        INNER JOIN `users` u ON s.`userid` = u.`userid`
Željko Živković
+2  A: 
SELECT username, query FROM Users, Queries WHERE Users.userid=Queries.userid;
Michael Aaron Safyan