Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在mySQL和Laravel中按组选择每个组中的第一行?_Php_Mysql_Sql_Database_Laravel - Fatal编程技术网

Php 在mySQL和Laravel中按组选择每个组中的第一行?

Php 在mySQL和Laravel中按组选择每个组中的第一行?,php,mysql,sql,database,laravel,Php,Mysql,Sql,Database,Laravel,正如标题所示,我想选择每组行中的第一行,这些行以分组方式分组 具体来说,如果我有一个user\u analytics表,如下所示: SELECT latitude, longitude, country_name, code, COUNT(*) as total_visitors FROM referral_analytics WHERE created_at BETWEEN '2020-01-03'AND '2021-01-03' GROUP BY code,country_name,lati

正如标题所示,我想选择每组行中的第一行,这些行以分组方式分组

具体来说,如果我有一个
user\u analytics
表,如下所示:

SELECT latitude, longitude, country_name, code, COUNT(*) as total_visitors FROM referral_analytics
WHERE created_at BETWEEN '2020-01-03'AND '2021-01-03'
GROUP BY code,country_name,latitude,longitude
我的输出:

latitude  | longitude  | country_name              | code| total_visitors
----------+------------+---------------------------+-----+---------------
32.548328 | -92.045235  | United States of America | US  | 1
33.389011 | -111.844017 | United States of America | US  | 1
34.060734 | -118.239738 | United States of America | US  | 1
38.935699 | -77.3508    | United States of America | US  | 1
39.613087 | -104.883926 | United States of America | US  | 1
39.751099 | -104.997101 | United States of America | US  | 1
39.772247 | -86.156517  | United States of America | US  | 1
39.966381 | -83.012772  | United States of America | US  | 4
latitude  | longitude  | country_name              | code| total_visitors
----------+------------+---------------------------+-----+---------------
32.548328 | -92.045235  | United States of America | US  | 11
我想在MySQL/Laravel查询中查询每个组的第一个或最小纬度和经度。 大概是这样的:

SELECT latitude, longitude, country_name, code, COUNT(*) as total_visitors FROM referral_analytics
WHERE created_at BETWEEN '2020-01-03'AND '2021-01-03'
GROUP BY code,country_name,latitude,longitude
预期输出:

latitude  | longitude  | country_name              | code| total_visitors
----------+------------+---------------------------+-----+---------------
32.548328 | -92.045235  | United States of America | US  | 1
33.389011 | -111.844017 | United States of America | US  | 1
34.060734 | -118.239738 | United States of America | US  | 1
38.935699 | -77.3508    | United States of America | US  | 1
39.613087 | -104.883926 | United States of America | US  | 1
39.751099 | -104.997101 | United States of America | US  | 1
39.772247 | -86.156517  | United States of America | US  | 1
39.966381 | -83.012772  | United States of America | US  | 4
latitude  | longitude  | country_name              | code| total_visitors
----------+------------+---------------------------+-----+---------------
32.548328 | -92.045235  | United States of America | US  | 11
明白了吗-

SELECT MIN(user_latitude) as latitude, MIN(user_longitude) as longitude, user_country_name, user_country_code, COUNT(*) as total_visitors
        FROM referral_analytics
        WHERE created_at BETWEEN '2020-01-03'AND '2021-01-03'
        GROUP BY user_country_code,user_country_name

这是一个查询,而不是一个表。请注意,按浮动纬度和经度值分组可能无法按预期工作,因为这些值不准确。我可以只获取每个组的第一个值吗?您是说只需按国家名称分组,并获取总访客以及属于该组的任意行吗?在这种情况下,然后看一看类似于选择最接近0的坐标的东西,0似乎是一个奇怪的目标。注意,这将选择最小的纬度和经度,但不会选择最小的经度对。因此,如果您有一个条目为
0,10
,另一个条目为
10,0
这将最终选择数据库中不同时存在的坐标