doufei2194 2017-10-06 10:36
浏览 21
已采纳

MySQL查询基于最高值,但列是VARCHAR

Running into a bit of a problem here. We have a system where we track our website hostings and I'm developing a frontend portal on this already pre-existing system.

In the database, the table looks something like:

id | company_id | renewal | domain_name

and an example couple entries in a MySQL database:

 1 | 5 | June 2014 | www.example.com
 2 | 5 | June 2015 | www.example.com
 3 | 5 | June 2016 | www.example.com
 4 | 5 | June 2017 | www.example.com
 5 | 5 | June 2018 | www.example.com
 6 | 5 | June 2014 | www.stackoverflow.com
 7 | 5 | June 2015 | www.stackoverflow.com
 8 | 5 | June 2016 | www.stackoverflow.com
 9 | 5 | June 2017 | www.stackoverflow.com
10 | 5 | June 2018 | www.stackoverflow.com

I am wanting to SELECT * where the company_id = 5 - that's no problem. I then want to show all domains where the renewal is the highest - basically I want to return:

 5 | 5 | June 2018 | www.example.com
10 | 5 | June 2018 | www.stackoverflow.com

Getting unique domains is simple, I've achieved that by GROUP BY(domain_name), but am clueless on sorting by renewal, especially because it isn't a simple int, rather a combination.

  • 写回答

1条回答 默认 最新

  • dongshi4773 2017-10-06 10:48
    关注

    I would definitely recommend to convert this table to something with a real date in it, so you can sort it. For the time being, you could use MySQL's STR_TO_DATE function, which tries to parse the date according to a given format.

    Here that would be:

    SELECT domain_name, MAX(STR_TO_DATE(renewal_date, '%M %Y')) AS max_renewal_date
    FROM table
    WHERE company_id = 5
    GROUP BY domain_name
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度