SQL:在具有多列的一列上不同

时间:2018-06-26 15:48:46

标签: mysql sql distinct

您好,在网站上搜索了几个小时之后,我没有找到答案!

我在MySQL上创建了一个查询,我想显示包含一次ID的每一列,但是此查询反复显示相同的ID。我怀疑是因为列日期。但是我不知道该如何解决!

| groups.id |名称|日期|日期结束

| 12345678 | abc | 12/12/09 | 12/12/09

| 12345678 | abc | 13/01/23 | 13/01/23

| 23456677 | bdc | 01/01/18 | 01/01/18

我想要这个输出:

| groups.id |名称|日期|日期结束

| 12345678 | abc | 12/12/09 | 12/12/09

| 23456677 | bdc | 01/01/18 | 01/01/18

谢谢!

SELECT DISTINCT (projects_prj.uuid_group_prj),groups.country_alpha2_prj,
          groups.title_prj,projects_prj.start_prj,projects_prj.end_prj,crisis.toDate,
          crisis.fromDate,crisis.calculationType,crisis.level,crisis.reportlink,
          groups.location_prj_lat,groups.location_prj_long,crisis.long_disaster,
          crisis.lat_disaster,kml_link,
     get_distance_metres(groups.location_prj_lat,groups.location_prj_long,crisis.lat_disaster,crisis.long_disaster)/1000 as km
            FROM `projects_prj`, groups,crisis
            WHERE groups.uuid_group_prj= projects_prj.uuid_group_prj
            and projects_prj.status_prj ='FREE'
            and projects_prj.end_prj >= NOW()
            and crisis.toDate >= NOW() - INTERVAL 3 DAY
            and (groups.location_prj_lat <= crisis.lat_disaster +1 AND groups.location_prj_lat >= crisis.lat_disaster -1) AND (groups.location_prj_long <= crisis.long_disaster +1 AND groups.location_prj_long >= crisis.long_disaster - 1)

Result here

1 个答案:

答案 0 :(得分:0)

要获取名称的单个实例,可以使用group by

SELECT DISTINCT (projects_prj.uuid_group_prj),groups.country_alpha2_prj,
          groups.title_prj,projects_prj.start_prj,projects_prj.end_prj,crisis.toDate,
          crisis.fromDate,crisis.calculationType,crisis.level,crisis.reportlink,
          groups.location_prj_lat,groups.location_prj_long,crisis.long_disaster,
          crisis.lat_disaster,kml_link,
     get_distance_metres(groups.location_prj_lat,groups.location_prj_long,crisis.lat_disaster,crisis.long_disaster)/1000 as km
            FROM `projects_prj`, groups,crisis
            WHERE groups.uuid_group_prj= projects_prj.uuid_group_prj
            and projects_prj.status_prj ='FREE'
            and projects_prj.end_prj >= NOW()
            and crisis.toDate >= NOW() - INTERVAL 3 DAY
            and (groups.location_prj_lat <= crisis.lat_disaster +1 AND groups.location_prj_lat >= crisis.lat_disaster -1) AND (groups.location_prj_long <= crisis.long_disaster +1 AND groups.location_prj_long >= crisis.long_disaster - 1)
 group by projects_prj.uuid_group_prj,Free2016.projects_prj.start_pr
 -- This may be the wrong column, but group it by that 'Name' one you have in your question