简体   繁体   English

我如何选择几乎不同的行,包括mysql中的nondistinct列(来自php)

[英]how can I select nearly distinct rows including the nondistinct columns in mysql (from php)

I am running the following query which works except when there are duplicate records (duplicate except for the id (primary key, int) field and the modified (timestamp) columns, there are many columns which get duplicated. The dups are a result of people hitting a submit button multiple times on a form (gotta fix that too, but thats another issue). Here is the query: 我正在运行以下查询,该查询的工作原理是:当有重复的记录(除了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";

I don't want the SUM to include the duplicates. 我不希望SUM包含重复项。 I am using mysql and php. 我正在使用mysql和php。

Thanks 谢谢

PS the table structure: 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: My query is now after Tom's suggestion: 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";

Thanks Tom. 谢谢汤姆。 This works except I lose the repeat_guest data from the original query. 除了我从原始查询中丢失了repeat_guest数据外,此方法有效。 How can I preserve it? 我该如何保存?

PPPS: wait, how about this: 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";

seems to work but is it doing what I think it is? 似乎有效,但它是否按照我的想法去做?

You could do something like this, where FIELD1, FIELD2 and FIELD3 are the fields that may be duplicated (and num_guests): 您可以执行以下操作,其中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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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