[英]Problem in Union Join For MySQL Query
i managed to select from a table that saves my latest posts 我设法从保存我最新职位的表格中选择
but i need to have double condition in selection 但我需要在选择时有双重条件
here is my code : 这是我的代码:
$sql_query = "SELECT b.*,u.username AS MY_Sender
FROM TABLE_users u,TABLE_blogs b
Where b.reciever = '0' AND u.user_id = b.sender
UNION
SELECT b.*,u.username AS MY_reciever
FROM TABLE_users u,TABLE_blogs b
Where b.reciever != '0' AND u.user_id = b.reciever
ORDER BY bid DESC
LIMIT 0,7 ";
but MY_reciever is Null and empty 但是MY_reciever为空
Am i wrong in using UNION for this need ?! 我是否有必要为此使用UNION?
SELECT b.*,u.username AS MY_Sender, NULL AS MY_reciever
FROM TABLE_users u,TABLE_blogs b
Where b.reciever = '0' AND u.user_id = b.sender
UNION
SELECT b.*, NULL AS MY_Sender, u.username AS MY_reciever
FROM TABLE_users u,TABLE_blogs b
Where b.reciever != '0' AND u.user_id = b.reciever
Columns across the parts of a union must have the same name. 并集各部分之间的列必须具有相同的名称。 Try:
尝试:
SELECT b.*,
u.username AS MY_User
FROM TABLE_users u,
TABLE_blogs b
WHERE b.reciever = '0'
AND u.user_id = b.sender
UNION
SELECT b.*,
u.username AS MY_User
FROM TABLE_users u,
TABLE_blogs b
WHERE b.reciever != '0'
AND u.user_id = b.reciever
ORDER BY bid DESC
LIMIT 0,7
If you need to distinguish which part of the UNION a record has been returned by, return an extra column: 如果您需要区分记录返回的UNION的哪一部分,请返回一个额外的列:
SELECT b.*,
u.username AS MY_User.
'Sender' AS MyType
FROM TABLE_users u,
TABLE_blogs b
WHERE b.reciever = '0'
AND u.user_id = b.sender
UNION
SELECT b.*,
u.username AS MY_User.
'Recipient' AS MyType
FROM TABLE_users u,
TABLE_blogs b
WHERE b.reciever != '0'
AND u.user_id = b.reciever
ORDER BY bid DESC
LIMIT 0,7
@Mac Taylor
@麦克·泰勒
hey man , sure you do understand me , told you that i need one more condition added to this statement you wrote , AS My_Real_Sender .. as you may know this statement you wrote works fine to find the Reciver ( MY_Type ) and not the sender of the post
嘿男人,确保您确实理解我,告诉您我还需要在您编写的该语句中添加一个条件,如My_Real_Sender ..您可能知道您编写的该语句可以很好地找到Reciver(MY_Type)而不是发送者该职位
You mean something like this? 你的意思是这样的吗?
SELECT b.*,
u.username AS MY_Sender.
NULL as MY_Recipient,
'Sender' AS MyType
FROM TABLE_users u,
TABLE_blogs b
WHERE b.reciever = '0'
AND u.user_id = b.sender
UNION
SELECT b.*,
u1.username AS MY_Sender.
u2.username AS MY_Recipient.
'Recipient' AS MyType
FROM TABLE_users u1,
TABLE_users u2,
TABLE_blogs b
WHERE b.reciever != '0'
AND u1.user_id = b.reciever
AND u2.user_id = b.sender
ORDER BY bid DESC
LIMIT 0,7
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.