0

我正在运行以下查询,除非有重复记录(除了 id(主键,int)字段和修改的(时间戳)列之外的重复,有很多列被重复。重复是人的结果在表单上多次点击提交按钮(也必须解决这个问题,但这是另一个问题)。这是查询:

$sql = "SELECT COUNT(id) AS rcount,
    SUM(num_guests) AS gcount,
    DATE_FORMAT(modifed, '%%m-%%Y') AS adate,
    SUM(1st_visit REGEXP '^no') AS repeat_guest
    FROM reservation_stats
    WHERE establishmentid = %i
    AND num_guests > 0
    GROUP BY adate
    ORDER BY added";

我不希望 SUM 包含重复项。我正在使用 mysql 和 php。

谢谢

PS表结构:

 id     int(10)          
establishmentid     int(11)         
name    varchar(50)     
email   varchar(100)    
phone   varchar(30)     
num_guests  int(11)         

reservation_time    varchar(50)     
reservation_date    date            
1st_visit   varchar(10)     
data    text    
added   date        
modifed     timestamp 

PPS:我的查询现在是在汤姆的建议之后:

 $sql = "SELECT COUNT(*) as rcount, SUM(num_guests) AS gcount, 
DATE_FORMAT(added, '%%m-%%Y') AS adate, 
1 as repeat_guest 
FROM 
( SELECT added, name, email, phone, num_guests, 
  reservation_time, reservation_date 
FROM reservation_stats where establishmentid = %i 
GROUP BY added, name, email, phone, num_guests, 
  reservation_time, reservation_date 
\) dup 
group by adate 
order by added";

谢谢汤姆。这有效,除非我从原始查询中丢失了 repeat_guest 数据。我怎样才能保存它?

PPPS:等等,这个怎么样:

        $sql = "SELECT COUNT(*) as rcount, SUM(num_guests) AS gcount, DATE_FORMAT(added, '%%m-%%Y') AS adate,    SUM(repeat_guest)                       
            FROM
            (
             SELECT  added, name, email, phone, num_guests, reservation_time, reservation_date, SUM(1st_visit REGEXP '^no') AS repeat_guest
             FROM    reservation_stats
             where establishmentid = %i
             GROUP BY added, name, email, phone, num_guests, reservation_time, reservation_date
            ) dup
            group by adate order by added";

似乎有效,但它是否在做我认为的那样?

4

1 回答 1

1

您可以这样做,其中 FIELD1、FIELD2 和 FIELD3 是可能重复的字段(和 num_guests):

SELECT COUNT(*) rcount, SUM(num_guests) AS gcount

FROM
(
 SELECT  FIELD1, FIELD2, FIELD3, num_guests
 FROM    YOUR_TABLE
 GROUP BY FIELD1, FIELD2, FIELD3, num_guests
) dup
于 2012-10-26T19:05:42.110 回答