[英]MySQL selecting all records and for each select separate record - in one query
I believe what I am trying to achieve here is a nested query - however I am unsure how to continue. 我相信我要在这里实现的是嵌套查询-但是我不确定如何继续。
I have 2 tables users and notifications what I am doing is querying notifications table to get the details, however I am also LEFT OUTER JOIN on users table to get my user information. 我有2个表的用户 , 通知我在做的是查询通知表以获取详细信息,但是我也对用户表进行LEFT OUTER JOIN来获取我的用户信息。 What I have is like this : 我所拥有的是这样的:
USERS TABLE
| user id | username | user image |
NOTIFICATIONS TABLE
| receiver id | sender id |
Just getting the receivers information is not an issue as I user the following query : 当我使用以下查询时,仅获取接收者的信息就不是问题了:
SELECT
notifications.senderID, notifications.receiverID, users.username, users.image
FROM
notifications
LEFT OUTER JOIN users ON users.id = notifications.receiverID
WHERE notifications.receiverID = ' xxx '
What I need to add is a way to foreach record found also grab the users.username and users.image for THAT record ( which will correspond on users.id for notifications.senderID ) as well as for the main record( notifications.recieverID ) 我需要添加的一种方法是,对找到的每个记录也抓取THAT记录的users.username和users.image( 将与notifications.senderID的users.id相对应 )以及主记录( notifications.recieverID )
IF POSSIBLE I wanted to keep this in one query and outside of a PHP foreach loop. 如果可能,我希望将其保留在一个查询中,并保留在PHP foreach循环之外。
Join the users
table twice with differant alias names 使用不同的别名将users
表连接两次
SELECT notifications.senderID, notifications.receiverID,
receiver.username as receiver_name, receiver.image as receiver_image,
sender.username as sender_name, sender.image as sender_image
FROM notifications
LEFT OUTER JOIN users as receiver ON receiver.id = notifications.receiverID
LEFT OUTER JOIN users as sender ON sender.id = notifications.senderID
WHERE notifications.receiverID = ' xxx '
This is the query for example 例如,这是查询
SELECT User
. SELECT User
。 id
, User
. id
, User
。 username
, User
. username
User
。 password
, User
. password
, User
。 email
, User
. email
, User
。 role
, User
. role
, User
。 created
, User
. created
, User
。 modified
, Userdetail
. modified
, Userdetail
。 user_id
, Userdetail
. user_id
, Userdetail
。 first_name
, last name
, Userdetail
. first_name
, last name
, Userdetail
。 address
, Userdetail
. address
, Userdetail
。 telephone
FROM cakephp_test
. telephone
来自cakephp_test
。 users
AS User
LEFT JOIN cakephp_test
. users
作为User
LEFT cakephp_test
。 userdetails
AS Userdetail
ON ( Userdetail
. user_id
= User
. id
) WHERE User
. userdetails
AS Userdetail
ON( Userdetail
。 user_id
= User
。 id
)WHERE User
。 id
= 51 LIMIT 1 id
= 51 LIMIT 1
in your case 在你的情况下
SELECT notifications.senderID, notifications.receiverID, users.username, users.image FROM database_name.users AS User LEFT JOIN database_name.notification AS notification ON (users.id = notifications.receiverID) WHERE User.id = 'xxx' LIMIT 1 SELECT notifications.senderID,notifications.receiverID,users.username,users.image FROM database_name.users AS用户LEFT JOIN database_name.notification AS通知ON(users.id = notifications.receiverID)WHERE User.id ='xxx'LIMIT 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.