Forum Moderators: open

Message Too Old, No Replies

Selecting Data < 3 Days Ago

Not sure how to go about this

         

Jeigh

12:53 pm on Aug 6, 2007 (gmt 0)

10+ Year Member



Hello,

I'll explain my situation a little, I've made a section where users can upload certain information for display, which is ordered from latest to oldest using ORDER BY id DESC.

They also have the ability to delete and re add new information (maximum of 2), but the problem is, people could just delete somthing and add the same information back again so they'd be at the top of the list.

To combat this once the user deletes somthing, it will be added to another table temporarily which will just store their username and the date in: Y-m-d H:i:s format. So say if they have one active, and one deleted I'll use PHP and if it adds up to 2 (which obviously in this case it does) then they will not be allowed to submit another one.

What I need to know is how can I only select records that were added less than 3 days ago and what is the most appropriate way to delete ones that are over 3 days ago as they are no longer needed (eg. run the delete script everytime someone tries to upload somthing, or using Cron (which I'm not sure of how to use)).

Thanks,
Jeigh.

UFfan

3:21 pm on Aug 6, 2007 (gmt 0)

10+ Year Member



What DB are you using? What data type are using for the date field? Why dont you have a variable that you write the date into and modify it by 3 days...

DELETE FROM tblSomething WHERE (Adate >= '" & theVariable & "')

Achernar

10:44 am on Aug 7, 2007 (gmt 0)

10+ Year Member Top Contributors Of The Month



SELECT x,y FROM table WHERE dateF>=(date'now'-interval'3d')::dateTypeUsedBy_dateF_column AND other conditions
DELETE FROM table WHERE dateF<('now'-interval'3d')::dateTypeUsedBy_dateF_column

If "date'now'" (or "'now'::date") doesn't work on you system, try "now()", but then your query might not use your index.

Jeigh

10:56 am on Aug 7, 2007 (gmt 0)

10+ Year Member



Thanks for the replies, So say my date column is just called date and I'm using the table 'example', I would use:

SELECT * FROM example WHERE date>=(date'now'-interval'3d')::0000-00-00 00:00:00 AND username='$username'

DELETE FROM example WHERE date<('now'-interval'3d')::0000-00-00 00:00:00

Thanks,
Jeigh

Achernar

12:48 am on Aug 8, 2007 (gmt 0)

10+ Year Member Top Contributors Of The Month



If your date column format looks like this "000-00-00 00:00:00", it's probably of type "timestamp"

SELECT * FROM example WHERE date>=(date'now'-interval'3d')::timestamp AND username='$username';
DELETE FROM example WHERE date<(date'now'-interval'3d')::timestamp;

Don't forget the type attached to 'now' (like I did earlier on the DELETE example).

To verify that it work as expected, first try the SELECT query. See if it returns "young" records. Also, check the correct type of the 'date' column.

Jeigh

9:02 am on Aug 9, 2007 (gmt 0)

10+ Year Member



I attempted trying what you said (or what I understood anyway) which ended in me using this:

SELECT * FROM deleted_items WHERE date>=(date('Y-m-d H:i:s', time())-interval'3d')::datetime AND username='$username';

It came up with:

ERROR: Unknown Punctuation String @ 83
STR: ::

Not sure if I followed what you said correctly though.

Thanks.

Habtom

9:15 am on Aug 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



SELECT * FROM deleted_items WHERE date>=(date('Y-m-d H:i:s', time())-interval'3d')::datetime AND username='$username';

Why would you need this

)::datetime

Try without it, and somebody pls explain me on what that is.

Jeigh

9:31 am on Aug 9, 2007 (gmt 0)

10+ Year Member



That gives me:

SQL query:

SELECT *
FROM deleted_items
WHERE date >= ( date( 'Y-m-d H:i:s', time( ) ) - INTERVAL '3d' )
AND username = '$username'
LIMIT 0 , 30

MySQL said:

#1064 - You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '( 'Y-m-d H:i:s' , time( ) ) - interval '3d' ) AND username = '$

I'm not sure what that line did, but I was just following the format he used.

Thanks.

Habtom

9:48 am on Aug 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Date is a reserved word, start by renaming your field name date to something different.

or

change it to
`date`

Jeigh

12:50 pm on Aug 9, 2007 (gmt 0)

10+ Year Member



I tried:

SELECT *
FROM deleted_items
WHERE time >= ( date( 'Y-m-d H:i:s', time( ) ) - INTERVAL '3d' )
AND username = 'Jeigh'
LIMIT 0 , 30

But it gave me:

#1064 - You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '( 'Y-m-d H:i:s' , time( ) ) - interval '3d' ) AND username = 'J

Thanks for all the help guys.

Habtom

12:55 pm on Aug 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem could be something else, but unfortunately time is also a reserved word See the list of Reserved Mysql/SQL Terms [hockinson.com]

Change that to `time` or time_new or something similar.

Habtom

[edited by: Habtom at 12:55 pm (utc) on Aug. 9, 2007]

Jeigh

1:12 pm on Aug 9, 2007 (gmt 0)

10+ Year Member



I was experimenting a little and tried:

SELECT *
FROM deleted_items
WHERE time_added >= ( CURRENT_DATE( ) - INTERVAL 3
DAY )

Which I'm pretty sure is working, so problem solved I guess.

Thanks for all the help again :)