双MYSQL请求

时间:2013-07-28 12:47:08

标签: mysql request double

:)

这是我的表“聊天”: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 ???

谢谢! :)

1 个答案:

答案 0 :(得分:0)

如何简化查询;这将列出与您聊天或与您聊天的所有人的用户名;

SELECT chat_from buddy FROM chat WHERE chat_to=$username
UNION
SELECT chat_to buddy FROM chat WHERE chat_from=$username