Query from two tables with one statement?
I have 2 tables
User -userid -group //other unrelated columns Logs -userid -ip
What I want to do is using an IP query the Logs table for all userids and then get the groups of all the userids. Is this possible with a single query? If not is there anyway to speed it up oth开发者_如何学Goer than querying every User row with the userid?
Something like this?
SELECT ip, userid, group
FROM Logs INNER JOIN User ON Logs.userid = User.userid
WHERE ip = '1.2.3.4'
Surely this is a simple join?
select logs.userid,logs.ip,user.group
from logs
left join user on logs.userid=user.userid
select distinct group
from user u
join logs l on u.userid=l.userid
where l.ip='yourIP'
You should be able to do it with a join ( http://dev.mysql.com/doc/refman/5.0/en/join.html )
select User.group
from Logs
inner join User on Logs.userid = User.userid
where Logs.ip = 'IP';
At least back tick some of the field names that are keywords (group) or could be special (user)
SELECT L.`group`
FROM Logs L
INNER JOIN `User` U ON U.userid = L.userid
WHERE L.ip = '1.2.3.4'
GROUP BY L.`group`
精彩评论