我在下面使用此查询与用户进行对话:
SELECT DISTINCT id, receivern, receiver
FROM arany_conversations
WHERE sender = '$_SESSION[success]'
UNION
SELECT DISTINCT id, sendern, sender
FROM arany_conversations
WHERE receiver = '$_SESSION[success]'
ORDER BY id DESC
以下是“ arany_conversations”表的内容: https://i.stack.imgur.com/3DZhr.png
我使用以下PHP代码:
foreach ($conversations as $conversation) {
echo $conversation['receiver']."<br/>".$conversation['sender'];
}
但是结果将是:https://i.stack.imgur.com/g7Mk9.png
说明:结果显示“发送者”和“接收者”,但仅使用以下代码:
echo $conversation['receiver'];
使用以下代码将不会显示“发件人”:
echo $conversation['sender'];
我试图使提到的查询像sender AS A1
和receiver AS A2
一样,但这是相同的问题。
那么如何使用$conversation['sender'];
而不是$conversation['receiver'];
显示“发送者”?
答案 0 :(得分:1)
UNION始终在第一个查询上显示该字段。这就是为什么发件人不存在的原因。 要获取两个字段名称,可以执行以下操作:
SELECT DISTINCT id, receivern, receiver, 0 as sender
FROM arany_conversations
WHERE sender = '$_SESSION[success]'
UNION
SELECT DISTINCT id, sendern, 0,sender
FROM arany_conversations
WHERE receiver = '$_SESSION[success]'
ORDER BY id DESC
对不起,我的英语不好。