php mysql大于问题

时间:2011-09-02 16:48:23

标签: php mysql

我有两张桌子:users, images。用户有一列paid,这是他们支付多少张图片进入比赛的数字。 图像有一列entered,未输入值为0,输入值为1。我需要每个用户的总和。

我正在尝试查询支付了比他们输入的更多图像的所有用户。但它没有正常工作..继承人打印的是什么:

bob@example.com        8  2  
jcannon@example.com    3  3  
jrcih@example.com      7  3  
aerwqeerll@example.com 5  1  
ray@example.com        2  3  

你可以看到jcannon @和ray @不应该在列表中,因为付费不大于输入

这是代码:

$SQL = mysql_query("SELECT  *, SUM(images.entered) 
                        FROM users, images 
                        WHERE users.id=images.owner 
                        AND users.paid>'SUM(images.entered)' 
                        GROUP BY users.id  ");

while($sendto=mysql_fetch_array($SQL)){
    print $sendto['email']." ".$sendto['paid']. 
          " ".$sendto['SUM(images.entered)']. "<br>";
}

我做错了什么?

2 个答案:

答案 0 :(得分:6)

首先除去引号,它们用于文字字符串。其次,您只能将聚合值与HAVING子句进行比较:

… WHERE … GROUP BY … HAVING users.paid >= SUM(images.entered) …

我发现使用显式连接语法通常更清楚,而不是WHERE

SELECT  *, SUM(images.entered)
FROM users
INNER JOIN images
ON users.id = images.owner
GROUP BY users.id
HAVING users.paid > SUM(images.entered)

答案 1 :(得分:1)

尝试Mysql Having

*如果两个表都有一些列标识

,您可以使用join
$SQL = mysql_query("
SELECT  
*, 
SUM(images.entered) as totalImage,
FROM users
INNER JOIN images on (users.id = images.owner) 
HAVING users.paid >  totalImage
GROUP BY users.id  

");
相关问题