dongou4052 2015-10-05 14:46
浏览 59
已采纳

mysql组并删除重复项

I have got this mySQL table:

 id | url | origin | categroy | date 

 1 | url.com/1 | US | news | 2015-10-01 12:39:19
 2 | url.com/1 |    |      | 2015-10-01 13:59:34
 3 | url.com/2 | CN | news | 2015-09-01 12:45:26
 4 | url.com/3 |    |      | 2015-08-12 09:10:10
 5 | url.com/3 | US | news | 2015-09-14 04:56:36
 6 | url.com/4 | US |      | 2015-09-09 12:12:09

is there a way to group the entries by the url and remove the duplicates in one mySQL call without creating a new table?

desired table:

 1 | url.com/1 | US | news | 2015-10-01 12:39:19
 3 | url.com/2 | CN | news | 2015-09-01 12:45:26
 5 | url.com/3 | US | news | 2015-09-14 04:56:36
 6 | url.com/4 | US |      | 2015-09-09 12:12:09

I am a beginner and I tried to solve this with a php script which failed. I guess there is a fairly easy SQL-answer to that, but I couldn't find an easy answer on Stackoverflow.

Thank you very much!

  • 写回答

1条回答 默认 最新

  • douzi115522 2015-10-05 14:54
    关注

    Try this query

    SELECT id, url, 
    SUBSTRING_INDEX(GROUP_CONCAT(origin ORDER BY date DESC),',',1) as origin_value,  
     SUBSTRING_INDEX(GROUP_CONCAT(category ORDER BY date DESC),',',1) as category_value, 
     SUBSTRING_INDEX(GROUP_CONCAT(date ORDER BY date DESC),',',1) as date_value FROM tablename 
    GROUP BY url
    ORDER BY url,date DESC
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀