我想要完成的是选择所有比$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");
我认为你在代码中没有正确地进行数学运算。我还建议您使用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);
我建议你先计算一下。
$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");
您使用了!=表示not =,而应该使用<>。还有,我想,这里放错了大括号....
$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;");
如果可以的话试试
$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");