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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路