具有多个表的 MySQL 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9629592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 12:26:50  来源:igfitidea点击:

MySQL Query with Multiple Tables

mysql

提问by DroidMatt

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

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

How can I select all the information from UserInfoTbl where fUsername of UserPvTbl has username of user0003?

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

Edit: Meaning that I need to retrieve information of user0002 and user0001 from the UserInfoTbl

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

Edit2: Relationship between both tables : UserInfoTbl.username = UserPvTbl.username

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

回答by jammin

DroidMatt can you clarify what the 2 tables relationship are

DroidMatt 你能澄清一下 2 个表的关系是什么吗

UserInfoTbl.username = UserPvTbl.fusername
or
UserInfoTbl.username = UserPvTbl.username

UserInfoTbl.username = UserPvTbl.fusername

UserInfoTbl.username = UserPvTbl.username

Vikram is right assuming the first. otherwise you want this.

Vikram 假设第一个是正确的。否则你想要这个。

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

回答by khushbu

Use like :

像这样使用:

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

回答by Vikram

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