[英]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']
. 我要完成的工作是选择所有比
$influencer_account['followed_by']
多20%或少20%的用户。
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.
我也建议您使用mysqli函数。 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: 编辑:如果要使用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.