Home > Database > Mysql Tutorial > body text

How to Retrieve the Last 7 Days of Data from SQL Server to MySQL?

Mary-Kate Olsen
Release: 2024-10-30 12:29:02
Original
232 people have browsed it

How to Retrieve the Last 7 Days of Data from SQL Server to MySQL?

Loading Last 7 Days of Data from SQL Server to MySQL

When transferring data from a SQL Server table (Table A) to a MySQL table, it's often necessary to select a specific time range. In this case, the user needs to retrieve the last 7 days of data from Table A.

The user initially attempted the following query:

<code class="sql">select id,
NewsHeadline as news_headline,
NewsText as news_text,
state,
CreatedDate as created_on
from News
WHERE CreatedDate BETWEEN GETDATE()-7 AND GETDATE()
order by createddate DESC</code>
Copy after login

However, this query was only retrieving 5 days of data. To resolve this issue, a slightly different approach is required.

Solution

The solution involves using the DATEADD function to calculate the start date of the 7-day period:

<code class="sql">SELECT id, NewsHeadline as news_headline, NewsText as news_text, state CreatedDate as created_on
FROM News 
WHERE CreatedDate >= DATEADD(day,-7, GETDATE())</code>
Copy after login

By subtracting 7 days from the current date using DATEADD, we ensure that the query retrieves the data from the last 7 days, accurately satisfying the user's requirement.

The above is the detailed content of How to Retrieve the Last 7 Days of Data from SQL Server to MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!