Mysql查询从哪里选择值小于X%或X%多于一个值

时间:2015-08-17 00:09:49

标签: php mysql

我想要完成的是选择比$influencer_account['followed_by']多20%或20%的所有用户。

我尝试使用下面的代码,但它不起作用。

$matchquery = mysql_query("SELECT * FROM `publishers_instagram_accounts` WHERE `pid` != '$publisher_id' AND ((".$influencer_account['followed_by']."+(".$influencer_account['followed_by']."*0.20)) <= followed_by) OR ((".$influencer_account['followed_by']."+(".$influencer_account['followed_by']."*0.20)) > followed_by) ORDER BY `id` DESC");

4 个答案:

答案 0 :(得分:0)

尝试此操作

   $matchquery = mysql_query(
        "SELECT * FROM `publishers_instagram_accounts` WHERE `pid` != ".$publisher_id."
    AND (
         ".$influencer_account['followed_by']."+".$influencer_account['followed_by']."*0.20 <= followed_by OR ".$influencer_account['followed_by']."+".$influencer_account['followed_by']."*0.20 > followed_by
        )
    ORDER BY `id` DESC");

答案 1 :(得分:0)

我建议你先做计算。

$reference_value = $influencer_account['followed_by'] * 1.2;
$matchquery = mysql_query(
    "SELECT * FROM `publishers_instagram_accounts` 
     WHERE `pid` != '$publisher_id' AND
         (($reference_value <= followed_by) OR ($reference_value > followed_by)) 
     ORDER BY `id` DESC");

我认为你错过了一个括号。

答案 2 :(得分:0)

你正在使用!= for not equals,而它应该是&lt;&gt;。此外,我认为,那里有一个错位的支撑......

$followedby=$influencer_account['followed_by'];

$matchquery = mysql_query("
    SELECT * FROM `publishers_instagram_accounts` 
    WHERE 
        `pid` <> '$publisher_id' AND (
            ( ".$followedby." + ( ".$followedby." * 0.20 ) ) <= followed_by ) 
            OR
            ( ".$followedby." + (".$followedby." * 0.20 ) ) > followed_by )
        )
    ORDER BY `id` DESC;");

答案 3 :(得分:0)

我认为你没有在你的代码中正确地进行数学计算。 我还建议你使用mysqli函数。看到这段代码,我没有测试它,但我认为它会起作用:

<?php

        $mysqli = new mysqli("localhost", "my_user", "my_password", "my_db");

        $query = "SELECT * FROM `publishers_instagram_accounts` WHERE `pid` != :pub_id
            AND (followed_by >= :start ) AND (followed_by <= :end) ORDER BY `id` DESC";

        //You need users with followed_by in the range:
        //      [start,end]
        // where:
        //      start = $influencer_account['followed_by'] - $influencer_account['followed_by'] * 0.2
        //      end = $influencer_account['followed_by'] + $influencer_account['followed_by'] * 0.2
        $percent = 0.2;
        $start = $influencer_account['followed_by'] * (1 - $percent); //20% less
        $end = $influencer_account['followed_by'] * (1 + $percent);//20% more       
        $stmt = $mysqli->prepare($query);
        $stmt->bind_param('idd',$publisher_id,$start,$end);
        $stmt->execute();

        $result = $stmt->get_result();
      while ($row = $result->fetch_array(MYSQLI_ASSOC))
      {
            var_dump($row);
      }

?>

修改 如果您想使用mysql_query,请尝试:

$percent = 0.2;
$start = $influencer_account['followed_by'] * (1 - $percent); //20% less
$end = $influencer_account['followed_by'] * (1 + $percent);//20% more       
$query = "SELECT * FROM `publishers_instagram_accounts` WHERE `pid` !=  $publisher_id AND (followed_by >= $start ) AND (followed_by <= $end ) ORDER BY `id` DESC";
mysql_query($query);