在JOIN语句中使用MAX

时间:2016-08-30 19:44:22

标签: mysql

我试图返回不同域名的最大人员数量但是已经撞墙了。我用man_hits回复了不同的域名,但它只抓住了第一个域名,所以它不是最大值。

我尝试将MAX添加到语句并进行试验,但只是设法挂起数据库。

这是我到目前为止所拥有的......

Array
(
    [paging] => Array
        (
            [total_items] => 33
            [current_page] => 1
            [total_pages] => 3
        )

    [data] => Array
        (
            [0] => Array
                (
                    [id] => 776583
                    [title] => NAME
                    [url] =>URL
                    [status] => open
                    [current_status] => open
                    [location] => a, a
                    [programmes] => Array
                        (
                            [id] => 2
                            [short_name] => SHORT NAME
                        )

                    [applications_count] => 5
                    [is_favourited] => 
                    [branch] => Array
                        (
                            [id] => 319532
                            [name] => International SOS - 1
                            [organisation_id] => 318911
                            [profile_photo_url] => URL
                            [url] => URL
                        )

                    [views] => 248
                    [duration_min] => 22
                    [duration_max] => 24
                    [applications_close_date] => 2016-09-06T00:00:00.000Z
                    [earliest_start_date] => 2016-10-01T00:00:00.000Z
                    [latest_end_date] => 2017-04-01T00:00:00.000Z
                    [profile_photo_urls] => Array
                        (
                            [original] => PIC URL
                            [medium] => PIC URL
                            [thumb] => PIC URL
                        )

                    [cover_photo_urls] => PNG IMG
                    [created_at] => 2016-08-30T03:24:41Z
                    [updated_at] => 2016-08-30T15:36:02Z
                )

            [1] => Array
                (
                    [id] => 774984
                    [title] => NAME
                    [url] => URL
                    [status] => open
                    [current_status] => open
                    [location] => Bonn, Germany
                    [programmes] => Array
                        (
                            [id] => 2
                            [short_name] => NAME
                        )

                    [applications_count] => 128
                    [is_favourited] => 
                    [branch] => Array
                        (
                            [id] => 287321
                            [name] => Deutsche Post DHL Group
                            [organisation_id] => 286836
                            [profile_photo_url] => PHOTO
                            [url] => URL
                        )

                    [views] => 1331
                    [duration_min] => 48
                    [duration_max] => 48
                    [applications_close_date] => 2016-09-04T00:00:00.000Z
                    [earliest_start_date] => 2016-10-01T00:00:00.000Z
                    [latest_end_date] => 2017-10-01T00:00:00.000Z
                    [profile_photo_urls] => Array
                        (
                            [original] => PIC
                            [medium] => PIC
                            [thumb] => PIC
                        )

                    [cover_photo_urls] => PIC
                    [created_at] => 2016-08-23T19:47:04Z
                    [updated_at] => 2016-08-24T06:35:58Z
                )

有人可以帮忙吗?

由于

史蒂夫

2 个答案:

答案 0 :(得分:1)

这个怎么样?

    SELECT ad.domain, MAX(ah.human_hits)
    FROM `a_hits_hourly` ah
    INNER JOIN a_saved_domains ad 
    ON ah.domain_id = ad.domain_id  
    WHERE ah.datestamp > 2016070000 AND ah.human_hits > 0
    GROUP BY ad.domain

它应该为每个human_hits获得最大domain。或许我不确定你想要什么。

答案 1 :(得分:0)

如果您需要知道与您感兴趣的记录相对应的其他值,例如datestamp,您可以使用如下查询:

SELECT domain, datestamp, human_hits 
FROM ( 
    SELECT     ah.datestamp,
               ah.human_hits,
               @rn := if (ad.domain = @domain, @rn + 1, 1) rn,
               @domain := ad.domain domain
    FROM       a_hits_hourly ah
    INNER JOIN a_saved_domains ad ON ah.domain_id = ad.domain_id
    WHERE      ah.datestamp > 2016070000
    ORDER BY   ad.domain,
               ah.human_hits DESC
) q 
WHERE rn = 1

内部查询按每个域的降序对命中进行排序,为其添加一个行计数,该行计数在下一个域的开头重置。外部查询仅采用编号为1的记录。