Home >Database >Mysql Tutorial >How to Retrieve the Last 7 Days of Data from SQL Server to MySQL?

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

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-30 12:29:02312browse

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>

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>

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!

Statement:
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