Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Query with Multiple Tables

Tags:

mysql

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?

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

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

like image 903
DroidMatt Avatar asked Sep 16 '25 04:09

DroidMatt


2 Answers

DroidMatt can you clarify what the 2 tables relationship are

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

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

SELECT *
FROM UserInfoTbl, UserPvTbl
WHERE UserPvTbl.username = UserInfoTbl.username
AND UserPvTbl.username = 'user0003'
like image 52
jammin Avatar answered Sep 19 '25 02:09

jammin


Use like :

select * from UserInfoTbl inner join UserPvTbl on UserInfoTbl.username=UserPvTbl.fusename
where UserPvTbl.usename='user0003'
like image 25
khushbu Avatar answered Sep 19 '25 03:09

khushbu