如何只显示以前与[php]聊天的用户

juud5qan  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(430)

我有一个表用户


**| username | password |**

     username    1234
     username1   1234
     username2   1234

和信息


**| from_user_name | to_user_name | message |**

     username        username1      Hi      
     username        username2      Hi
     username1       username      reply

这是我的sql代码

$query = "SELECT * FROM " .table_name. " WHERE to_user_name = '".$_SESSION['username']."' OR from_user_name = '".$_SESSION['username']."' GROUP BY from_user_name,to_user_name";

我用(username)登录如果有(username1)的回复,它会显示

username 1
username 1
username 2

我要表现得像

username 1
username 2

你能告诉我吗,谢谢大家

nwwlzxa7

nwwlzxa71#

用途:

SELECT DISTINCT CASE WHEN to_user_name = $_SESSION['username']
                     THEN from_user_name
                     ELSE to_user_name
                     END AS responder
FROM message
WHERE $_SESSION['username'] IN (from_user_name, to_user_name);

SELECT to_user_name AS responder
FROM message
WHERE from_user_name = $_SESSION['username']
UNION 
SELECT from_user_name 
FROM message
WHERE to_user_name = $_SESSION['username']

相关问题