Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to query the nearest record data in mysql

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces how to query the nearest recorded data in mysql, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Query scenario: the demand now is to query the users closest to the age of 20, and get the top 5

My current database record user age field record format is "1995-05-20", field name birthday

The solution is as follows:

1. First of all, when querying, it is converted to the user's age.

How to change the date format to age:

(1) the year in the current year-date format

Date_format (now (),'% Y')-from_unixtime (unix_timestamp (birthday),'% Y')

(2) the year in the current year-date format (different acquisition methods)

Date_format (now (),'% Y')-year (birthday)

two。 Get the closest data by sorting order by asb ()

Complete statement:

SELECT (date_format (now (),'% Y')-year (birthday)) as ageFROM userWHERE is_anchor = 1ORDER BY abs (20-age) limit 0Magne5 on how to query the closest record data in mysql is shared here. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report