简体   繁体   中英

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

What im trying to accomplish is to select all the users who has 20% more or 20% less than $influencer_account['followed_by'] .

I tried using this code below but it doesnt work.

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

Try this if it works

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

I suggest you to do the calculation previously.

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

I think you were missing a parenthesis.

You were using != for not equals whereas it should be <>. Also, there was, I think, a misplaced brace in there....

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

I think that you are not doing the maths correctly in your code. I also recomend to you to use mysqli functions. See this code, I didn't test it but I think It's going to work:

<?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);
      }

?>

EDIT: If you want to use mysql_query, try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM