:)
这是我的表“聊天”:ID - FROM - TO - MSG - DATE - RECU
我想列出与$ username聊天的所有用户。 其中$ username在“FROM”中:(为了便于阅读而添加换行符)
$sql = "SELECT *
FROM chat
WHERE chat.from = '$username' AND chat.to != '$username'
GROUP BY chat.to
ORDER BY chat.to ASC";
并且$ username位于“TO”中:
$sql = "SELECT *
FROM chat
WHERE chat.from != '$username' AND chat.to = '$username'
GROUP BY chat.from
ORDER BY chat.from ASC";
我现在可以列出这两个表,但是如何只生成一个请求或只列出一个表? DISTINCT与UNION ???
谢谢! :)
答案 0 :(得分:0)
如何简化查询;这将列出与您聊天或与您聊天的所有人的用户名;
SELECT chat_from buddy FROM chat WHERE chat_to=$username
UNION
SELECT chat_to buddy FROM chat WHERE chat_from=$username