具有多个表的MySQL查询

时间:2012-03-09 05:58:08

标签: mysql

Table: UserInfoTbl
=======================
| username  | type    |
=======================
| user0001  | premium |
| user0002  | premium |
| user0003  | normal  |
| user0004  | premium |
=======================

Table: UserPvTbl
========================
| username | fUsername |
========================
| user0003 | user0002  |
| user0002 | user0001  |
| user0003 | user0001  |
========================

如何从UserInfoTbl中选择UserPvTbl的fUsername具有user0003用户名的所有信息?

编辑:这意味着我需要从UserInfoTbl中检索user0002和user0001的信息

Edit2:两个表之间的关系:UserInfoTbl.username = UserPvTbl.username

3 个答案:

答案 0 :(得分:7)

DroidMatt可以澄清2表关系是什么

UserInfoTbl.username = UserPvTbl.fusername

UserInfoTbl.username = UserPvTbl.username

维克拉姆是第一个正确的假设。否则你想要这个。

SELECT *
FROM UserInfoTbl, UserPvTbl
WHERE UserPvTbl.username = UserInfoTbl.username
AND UserPvTbl.username = 'user0003'

答案 1 :(得分:1)

使用类似:

select * from UserInfoTbl inner join UserPvTbl on UserInfoTbl.username=UserPvTbl.fusename
where UserPvTbl.usename='user0003'

答案 2 :(得分:0)

select U.*
from UserInfoTbl U
inner join UserPvTbl UP
on U.username  = UP.fusername  
where UP.username = 'user0003'