MySQL selecting rows where datetime is X days ago [duplicate] - php

This question already has answers here:
Add 2 hours to current time in MySQL?
(5 answers)
Closed 9 years ago.
I have a table in database for meeting schedules. It has 2 columns named start and end and since I dont have access to the php script which fills this table with new data, I am not sure in which format it is.
But PHPMyAdmin shows taht the columns for start and end are varchar(15) So I guess it should be datetime compatible.
Example in DB: 1378033200
Which shows as 01 September 2013
My question is, I want to pull the meetings and show them in a html page, but I do not want meetings which are older than 2 days ago (server time) to show up. What will be the query?
SELECT * FROM schedules ORDER BY start
Something like
SELECT * FROM schedules ORDER BY start WHERE start > 2 days ago
I tried this but it seems it does nothing!
SELECT *
FROM schedules
WHERE COALESCE(start, 0) < CURDATE() - INTERVAL 2 DAY
ORDER BY start

But PHPMyAdmin shows taht the columns for start and end are
varchar(15) So I guess it should be datetime compatible.
You've guessed wrong. Strings are only sortable as strings. Which means, unless you're using a sortable date formats (YYYY/MM/DD being one: I'm not aware of others) you'll have to parse all the results and do the calculation by yourself in PHP (otherwise, 13/11/2000 will come before 14/01/2000). Alternatively, you might wanna use the proper type for your column: datetime, date or timestamp. Once you'll do that, you'll be able to query your db and compare dates with < and > operators.
For the 2 days ago part, you'd like to know that MySql has a built in NOW variable to which you can sum/subtract days. If you'll design your db correctly, you won't even have to touch PHP (which a desiderable thing).

Try this:
SELECT *
FROM schedules
WHERE COALESCE(start, 0) < DATE_SUB(NOW(), INTERVAL 2 DAYS)
ORDER BY start

Related

show records from sql created during specific week

I have tons of records that are in my database table leadactivity basically I need to display all the records that were created in the first week of the current month, then also another query to display records in the second week, same for third week and then finally the fourth week of the current month.
I have a column called created_date which onupdate puts in the current timestamp
What is the best way to achieve this?
You can use date functions for this:
select la.*
from leadactivity la
where day(la.created_date) between 1 and 7 and
created_date >= curdate() + (1 - day(curdate())) day;
The above assumes MySQL syntax. Most databases have similar functionality, although the specific functions may differ.

Proper MYSQL syntax for timestamp comparison and distinct results with certain exclusions

Sorry for asking, but I've never had to do such a complex MYSQL query before and I don't actually know what to google search in order to get the answer.
I have a poorly crafted database with a table of appointments of pregnant women that includes the day they came and the number of weeks pregnant they were at that time. I'm trying to select each one that should be 30 weeks right now but that doesn't already have a separate entry after 25 weeks pregnancy. I use the phone number to uniquely identify each person.
Since I really don't know how to formulate this query, this is the best I've come up with.
SELECT * FROM patientlist WHERE
UNIX_TIMESTAMP() - (UNIX_TIMESTAMP(`date`) - `weekspreg`*604800) > 29*604800
AND
UNIX_TIMESTAMP() - (UNIX_TIMESTAMP(`date`)- `weekspreg`*604800) <= 30*604800
AND
/* a subquery that keeps out results where the phone number would show up elsewhere in the table for a woman with more than 25 weeks of pregnancy. */
There has to be a better solution than separately querying each of the results from the date range by phone number to see if the weekspreg is more than 25.
Thank you in advance for any help or direction.
Your entire WHERE is incorrect. A query can only have ONE where clause. You join multiple conditions with and and or, not and where:
WHERE foo AND bar // correct
WHERE foo AND WHERE bar // syntax error
Check out the MySQL Date and Time Functions. For example, I'm not entirely certain what that last WHERE clause is trying to do, but I believe the first portion could be rewritten as something like:
SELECT *
FROM patientlist
WHERE `date` - interval `weekspreg` week
between now() - interval 29 week
and now() - interval 30 week

PHP and MySQL - display contnet of column between certain numerical values

I'm new to MySQL and PHP but was wondering if someone could help me with a little project I'm doing for my boss.
I have a SQL database (MyDB) and a table in there (mytable) with two columns - the first column (index) is an auto-incrementing integer from 1-10, the second column (date) has different dates and timestamps in the format of Year-month-day time 2013-04-12 1326
I'm trying to create a simple PHP page that first gets the current date (easy enough) then looks at the table and shows the number of rows that fall within yesterday's date. For example, if I have 3 rows with 2013-04-11 XXXX and 2 rows with 2013-04-12 XXXX (and today is the 12th April 2013) the page will display 3. (The time is not important but we can't remove it from the table as it's auto created by one of the other staff's programs and he refuses to change it).
So far I've got my php page, done a connection to the DB and defined two variables:
$startdate = date('Y'."-".'n'."-".'d'." "."0000");
$enddate = date('Y'."-".'n'."-".'d'." "."2359");
As the timestamp doesn't matter I've gone for the min/max possible on the variables. I realise this will only give the current date, trying to work out how to get it to display the previous day as the date in the variable.
Now I'm trying to create a sql query that will count the number of rows where the date field falls within the startdate and enddate variables (-1 day) but not too sure where to start or how this would look. I then need to output this as a variable in PHP so I can echo it later in the page.
Anyone able to point me in the right direction? Hope any of this makes sense.
You could write a query with no params to do this (if its always just yesterday).
SELECT * FROM <table>
WHERE DATE_FORMAT(<date column>,'%j-%Y') = DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 DAY), '%j-%Y');
Date functions in the where clause might not be super awesome performance wise

How to get data from MySQL DB between certain dates

Im adding points to a db for when a user does somit on site.
Alls I would like to do is get the points for the last 7 days and total them.
In my DB I have it saved like: PointsID, PointsUserID, PointsTotal, PointsDate
I guess I just need to figure out the latest date in the db, then minus 7 days, then get the values from between them. Once I have returned the values they will need to be summed so I can output one number.
Thanks, Bonxy
SELECT SUM(PointsTotal) FROM TableName WHERE PointsUserID = 'IdInQuestion' AND PointsDate >= Date_Sub(Now(), Interval 7 Day)
In the event that you are storing the date as a unix timestamp value (AKA a PHP date value) you would want to convert the one of the two values in the appropriate direction.
for instance you could convert the field to a MySQL datetime value by replacing PointsDate with FROM_UNIXTIME(PointsDate). You could also go the other way and convert the results of DATE_SUB() by wrapping it all in a UNIX_TIMESTAMP(). Both should have equal results.
Something like
Select * From MyTable Where MyDate Between Date_Sub(Now(), Interval 7 Day) and Now()

query mysql table and fetch rows posted in 3 days

how can i query my mysql database and fetch rows which are posted in earlier 3 days
i know how to fetch todays's rows but not 3 days ago
time will save in my table like this :
2010-01-20 19:17:49
and this is what i know :
SELECT id FROM pages WHERE date=now()
but i need to show posts in 3days
and im looking for a simple and straight solution ,because i know how to do so in long php codes
To get records three days in the past up to current time:
SELECT t.id
FROM PAGES t
WHERE t.date BETWEEN DATE_SUB(NOW(), INTERVAL 3 DAY) AND NOW()
This should select all entries with a date value after and including 3 days ago:
Select id From pages Where NOW() >= Interval date day + 3
Note that if there are dates in the future this will select them too.
Would be helpful to know what version of MySQL you're using and what column type we're talking about but here's the reference of Date and Time Functions for 5.1. You're going to probably want DATE_SUB().

Categories