我有两张桌子首先是用户;这由用户名和用户名组成.第二个是由userid和query组成的搜索.
当我选择搜索表时,我希望通过从用户表中取出用户名替换用户名.这有道理吗
+-------+----------+ |userid | username | +-------+----------+ | 1 | foo1 | +-------+----------+ | 2 | foo2 | +-------+----------+ | 3 | foo3 | +-------+----------+ | 4 | foo4 | +-------+----------+ +-------+----------+ |userid | query | +-------+----------+ | 1 | blah1 | +-------+----------+ | 2 | blah2 | +-------+----------+ | 3 | blah2 | +-------+----------+ | 4 | blah2 | +-------+----------+
有没有办法用单个查询来做到这一点?
解决方法
你正在寻找一个
inner join.这样做:
SELECT s.query,u.username FROM search s INNER JOIN users u ON s.userid = u.userid