添加条件到mysql查询并再次获取

时间:2016-07-29 12:47:57

标签: php mysql mysqli

我运行的3个查询几乎相同,后两个查询条件AND

主要查询:

$mess = $mysqli->prepare("SELECT * from ( SELECT cm.id ,cm.userid,cm.message,cm.voteup,cm.votedown,cm.date
                        FROM chat_messages cm 
                        INNER JOIN members m  ON m.id =cm.userid
                        INNER JOIN chat_settings cs ON cs.id = cm.room_id 
                        WHERE cm.setting_id = ?          
                        ORDER BY cm.date DESC LIMIT 30 ) ddd
                        ORDER BY date ASC ");
$mess->bind_param("i", $room);                              
$mess->execute();
$mess->store_result();

$mess->bind_result($chatid,$chat_userid,$message,$voteup,$votedown,$date);
while($row = $mess->fetch()){
    //im fetching here in my <div class='div1' >
}

然后,在第二个div中,我必须添加AND条件:

$mess2 = $mysqli->prepare("SELECT * from ( SELECT cm.id ,cm.userid,cm.message,cm.voteup,cm.votedown,cm.date
                        FROM chat_messages cm 
                        INNER JOIN members m  ON m.id =cm.userid
                        INNER JOIN chat_settings cs ON cs.id = cm.room_id
                        WHERE cm.setting_id = ?    AND voteup - votedown >= 5      
                        ORDER BY cm.date DESC LIMIT 30 ) ddd
                            ORDER BY date ASC ");
$mess2->bind_param("i", $room);                              
$mess2->execute();
$mess2->store_result();

$mess2->bind_result($chatid,$chat_userid,$message,$voteup,$votedown,$date);
while($row2 = $mess2->fetch()){
    //im fetching here in my <div class='div2' >
}

最后,在第三个div中,我的AND条件略有不同:

$mess3 = $mysqli->prepare("SELECT * from ( SELECT cm.id ,cm.userid,cm.message,cm.voteup,cm.votedown,cm.date
                        FROM chat_messages cm 
                        INNER JOIN members m  ON m.id =cm.userid
                        INNER JOIN chat_settings cs ON cs.id = cm.room_id 
                        WHERE cm.setting_id = ?    AND votedown - voteup >= 5      
                        ORDER BY cm.date DESC LIMIT 30 ) ddd
                            ORDER BY date ASC ");
$mess3->bind_param("i", $room);                              
$mess3->execute();
$mess3->store_result();

$mess3->bind_result($chatid,$chat_userid,$message,$voteup,$votedown,$date);
while($row3 = $mess3->fetch()){
    //im fetching here in my <div class='div3' >
}

一切正常但是做这个近乎相同的查询似乎很笨拙。是否有可能只用一个查询构造相同的东西?我使用了$mess->data_seek(0);,但它没有帮助,因为我没有在查询中添加条件。

2 个答案:

答案 0 :(得分:1)

只需要PhP来过滤您的数据,而不是对您的数据库进行三重查询。在这种情况下,您可以找到解决方案,因为您使用相同的参数调用了3次查询:

    $mess3  =   $mysqli->prepare("  SELECT    * 
                                    FROM      ( SELECT  cm.id ,
                                                        cm.userid,
                                                        cm.message,
                                                        cm.voteup,
                                                        cm.votedown,
                                                        cm.date
                                                FROM        chat_messages cm 
                                                INNER JOIN  members m  ON m.id =cm.userid
                                                INNER JOIN  chat_settings cs ON cs.id = cm.room_id 
                                                WHERE       cm.setting_id = ?    
                                                AND         votedown - voteup >= 5      
                                                ORDER BY    cm.date DESC LIMIT 30 ) ddd
                                    ORDER BY   date ASC ");
$mess3->bind_param("i", $room);                              
$mess3->execute();
$mess3->store_result();
$mess3->bind_result($chatid,$chat_userid ,$message,$voteup,$votedown ,$date);

while($row = $mess3->fetch()){
  $voteup     =   $row['voteup'];
  $votedown   =   $row['votedown'];

  addToDiv1($row);

  if( $voteup - $votedown >= 5 ) {
    addToDiv2($row);
  }

  if( $votedown - $voteup >= 5 ) {
    addToDiv3($row);
  } 
}

答案 1 :(得分:1)

我将专门根据清理代码给出答案。从技术上讲,你仍然会在这种情况下进行3次调用,但它会更清晰,因为你只包含一个函数,你看不到它背后的脚本。

正如我所提到的,我不是一个SQL爱好者所以我不能在那里给出一个好的解决方案(也许你可以使用GROUP BYOR条款......我真的不知道...)。如果我这样做,我会做一个可以返回所有选项的函数:

<强> /core/functions/getChatMessages.php

function getChatMessages($settings,$mysqli)
    {
        $id   = (!empty($settings['id']))? $settings['id'] : false;
        $type = (!empty($settings['type']))? $settings['type'] : false;
        $max  = (!empty($settings['max']))? $settings['max'] : 30;
        $mod  = '';
        // No id, just stop
        if(!is_numeric($id))
            return false;
        // equation one
        if($type == 'up')
            $mod = ' AND voteup - votedown >= 5';
        // equation two
        elseif($type == 'down')
            $mod = ' AND votedown - voteup >= 5';

        $mess = $mysqli->prepare("SELECT * from ( SELECT cm.id ,cm.userid,cm.message,cm.voteup,cm.votedown,cm.date
                            FROM chat_messages cm 
                            INNER JOIN members m  ON m.id =cm.userid
                            INNER JOIN chat_settings cs ON cs.id = cm.room_id 
                            WHERE cm.setting_id = ? {$mod}     
                            ORDER BY cm.date DESC LIMIT {$max} ) ddd
                            ORDER BY date ASC");
        $mess->bind_param("i", $id);
        $mess->execute();
        $mess->store_result();
        $mess->bind_result($chatid, $chat_userid, $message, $voteup, $votedown, $date);
        while($mess->fetch()){
            $result[] = array(
                            'chatid'=>$chatid,
                            'chat_userid'=>$chat_userid,
                            'message'=>$message,
                            'voteup'=>$voteup,
                            'votedown'=>$votedown
                        );
        }
        // Send back the data
        return (!empty($result))? $result : array();
    }

使用:

// Include our handy function
require_once('/core/functions/getChatMessages.php');
// Store our id for use
$settings['id'] = 100;
// Should get 30 from first select
$voteGen = getChatMessages($settings,$mysqli);
// Should get 30 from second select
$settings['type'] = 'up';
$voteUp = getChatMessages($settings,$mysqli);
// Should get 15 from third select
// Just for the heck of it, I added in a limit settings
$settings['max'] = 15;
$settings['type'] = 'down';
$voteDown = getChatMessages($settings,$mysqli);

现在您已经存储了这些内容,只需使用foreach循环将它们放入您的视图中即可。好的一面是,您可以随时随地调用此函数,因为该函数仅返回数据。它允许您在视图或非视图情况下处理数据。 旁注,我使用PDO,所以如果mysqli获取的方式无效,那就是原因。最好提取一个assoc数组来返回...