簡體   English   中英

MySQL查詢,從中選擇值小於X%或大於X%的值

[英]Mysql query to select from where value is X% less or X% more than a value

我要完成的工作是選擇所有比$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");

如果可行,請嘗試此

   $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");

我建議您事先進行計算。

$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");

我認為您缺少括號。

您使用!=表示不等於,但應為<>。 而且,我認為那里的支撐架放錯了位置。

$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;");

我認為您在代碼中沒有正確進行數學運算。 我也建議您使用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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM