我有4个表(下面列出)并且需要:
表1“聊天”
ID(autoinc) fromuserid roomid text
1 23 3 bla
2 14 1 bla
3 11 3 bal
表2“用户”/短线/
ID(autoinc) nickname banned
1 chris 0
2 paul 1 // 1 = banned
表3“隐藏”
ID(autoinc) orguser hideuser
1 12 3
2 33 12
现在我用PHP例程解决了这个问题,但是我必须经历每个结果并总是新查询,这需要太长时间;
$userid = 1; // actual user
// List all chats and show userid as nickname
$sql_com = "SELECT user.id, user.nickname, chats.text, chats.id ".
" FROM chats, user".
" WHERE ".
" chats.fromuserid = user.id ".
" AND chats.roomid = 3 ".
" AND user.banned != 1 ".
" ORDER BY chats.id DESC";
$result = mysql_query ($sql_com);
$count = 0;
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
$dontshow = false;
// Filter : dont show users $userid dont like to see (table "hide")
$sql_com2 = "SELECT id from hide WHERE ( (orguser = ".$userid.") AND (hideuser = ".$row[0].") ) ";
if ($result2 = mysql_query ($sql_com2))
{
if (mysql_num_rows($result2) > 0) $dontshow = true;
}
// Output
if ($dontshow == false)
{
$count++;
echo "Nickname: ".$row[1]." Text: ".$row[2];
}
if ($count > 10) break;
}
顺便说一下。我已经做了一些改进,所以实际问题可能不适合所有答案(感谢你的帮助,直到现在)
最终它现在只是整合过滤器“不要显示在表中列出的人”隐藏“为我的实际用户”。
答案 0 :(得分:1)
未经测试,但它会是这样的:
$sql_com = "SELECT us.id, us.nickname, ch.text, ch.id ".
" FROM chats ch, ".
" user us, ".
" hide hi, ".
" banned ba, ".
" WHERE ".
" us.id != hi.hideuser ".
" us.id != ba.user ".
" us.id = ch.fromuserid ".
" AND ch.roomid = 3 ".
" ORDER BY ch.id DESC LIMIT 0,10";
答案 1 :(得分:1)
答案 2 :(得分:-1)
虽然我不能立即找到一个简单的方法来回答你的问题,但我可以指出你正确的方向:
http://dev.mysql.com/doc/refman/5.0/en/subqueries.html
使用子查询应该可以从阻止和隐藏表中进行选择,并使用原始查询中的那些。