简体   繁体   English

加入并查询两个mysql表

[英]Joining and querying two mysql tables

I am pretty new to php and MySql and required some help. 我是php和MySql的新手,需要一些帮助。

I have two tables 1) a Wishlist containing three columns - an emailId, itemId and alert 我有两个表1)包含三列的愿望清单 - 一个emailId,itemId和alert

2) an ItemList containing several columns - itemId, itemName, itemPrice, itemUrl etc. 2)包含多个列的ItemList - itemId,itemName,itemPrice,itemUrl等。

When a user is logged in with a particular emailId, i need to display the list of items in his WishList (from the itemList) along with the "alert" column from his wishlist. 当用户使用特定的emailId登录时,我需要在他的愿望清单中显示项目列表(来自itemList)以及其愿望清单中的“警报”列。 Since both the tables are different, and the emailId is the only input I will have, I need to find an optimal method to query this. 由于两个表都不同,并且emailId是我将拥有的唯一输入,我需要找到一个最佳方法来查询它。

WishList

----------------------------------
emailId      |  itemId  | alert
----------------------------------
aj@gmail.com     01        true
bb@gmail.com     02        false
aj@gmail.com     03        false
dj@gmail.com     03        false
cj@gmail.com     03        false
aj@gmail.com     04        false

ItemList
-------------------------------------------------
itemId | itemName | itemPrice | itemUrl | ..
-------------------------------------------------
 01       abc         129         
 02       cde         99
 03       def         981
 04       efg         29
 05       fgh         200

So, given the emailId, how can I optimally query all the items corresponding to that? 那么,给定emailId,我如何以最佳方式查询与之对应的所有项目? example : if emailId is aj@gmail.com , then required output: 例如:如果emailId是aj@gmail.com,则需要输出:

Output - aj@gmail.com
----------------------------------------------------------
 alert |  itemId | itemName | itemPrice | itemUrl | ..
---------------------------------------------------------
 true       01       abc         129         
 false      03       def         981
 false      04       efg         29

Thankyou 谢谢

edited the question..sorry about that. 编辑了这个问题。对此有所了解。

Try this 尝试这个

select * from ItemList i where i.itemId=(select itemID from WishList where emailid="emailaddress") 从ItemList i中选择*,其中i.itemId =(从WishList中选择itemID,其中emailid =“emailaddress”)

You can use JOIN or RIGHT JOIN or LEFT JOIN method. 您可以使用JOINRIGHT JOINLEFT JOIN方法。
try this : 尝试这个 :

SELECT WL.emailId, WL.itemId, IL.itemName, IL.itemPrice 
FROM WishList WL
LEFT JOIN ItemList IL
on WL.ItemId = IL.itemId
//filter here, you can add WHERE CLAUSE

Try This 尝试这个

select * from WishList join ItemList ON WishList.itemId = ItemList.itemId 
where emailId = "emailaddress"  

You can use left join for that types of result. 您可以对该类型的结果使用左连接。

Select * 
FROM ItemList I 
Left join WatchList W On I.ItemId=W.ItemId 
where your_require_condition ;

Try This 尝试这个

select b.* from WishList as a join ItemList as b on a.itemId = b.itemId 
where a.emailId = 'email address' 

It will give you the whole item list for the desired emailid. 它将为您提供所需emailid的整个项目列表。

u can try the following query . 你可以尝试以下查询。

select *
from wishlist w,itemlist i
where w.itemid=i.itemid
and i.emailid="emailaddress"

Use LeftJoin 使用LeftJoin

Select I.* from ItemList I Left join WishList W 
    on I.itemId = W.itemId where W.emailId = "<email_address>"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM