[英]Joining and querying two mysql tables
我是php和MySql的新手,需要一些帮助。
我有两个表1)包含三列的愿望清单 - 一个emailId,itemId和alert
2)包含多个列的ItemList - itemId,itemName,itemPrice,itemUrl等。
当用户使用特定的emailId登录时,我需要在他的愿望清单中显示项目列表(来自itemList)以及其愿望清单中的“警报”列。 由于两个表都不同,并且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
那么,给定emailId,我如何以最佳方式查询与之对应的所有项目? 例如:如果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
谢谢
编辑了这个问题。对此有所了解。
尝试这个
从ItemList i中选择*,其中i.itemId =(从WishList中选择itemID,其中emailid =“emailaddress”)
您可以使用JOIN
或RIGHT JOIN
或LEFT JOIN
方法。
尝试这个 :
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
尝试这个
select * from WishList join ItemList ON WishList.itemId = ItemList.itemId
where emailId = "emailaddress"
您可以对该类型的结果使用左连接。
Select *
FROM ItemList I
Left join WatchList W On I.ItemId=W.ItemId
where your_require_condition ;
尝试这个
select b.* from WishList as a join ItemList as b on a.itemId = b.itemId
where a.emailId = 'email address'
它将为您提供所需emailid的整个项目列表。
你可以尝试以下查询。
select *
from wishlist w,itemlist i
where w.itemid=i.itemid
and i.emailid="emailaddress"
使用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.