Fiat Coupe Forum
- Founded by Kayjey & James Northam
- Funded by the Club for the benefit of all owners
Fiat Coupe Club UK
join the club
Fiat Coupe Forum
 
» Announced
    Posting images


» Related sites
    Main club site
    fiatcoupe.net


» External data
    owners listed
 
Who's Online Now
1 registered members (Rustydog), 286 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Forum Statistics
Forums69
Topics113,635
Posts1,341,406
Members1,814
Most Online731
Jan 14th, 2020
Top Posters(All Time)
barnacle 33,568
stan 32,122
Theresa 23,304
PeteP 21,522
bockers 21,071
JimO 17,917
Nigel 17,367
Edinburgh 16,845
RSS Feeds
Club Events
Club Information
Track Events
Rolling Road/RWYB
Social Events
Non-UK Events
Coupé Related Chat
Coupé Spotting
Coupé News/Press
Buying/Selling Advice
Insuring a Coupé
Basic FAQ's
How to Guides
Forum Issues
Technical Problems
General Maintenance
Styling
Tuning
Handling
ICE and Alarm
Coupés for Sale
Coupés Wanted
Parts for Sale
Parts Wanted
Group Buys
Business Forum
Other Vehicles for Sale/Wanted
Other Items for Sale/Wanted
Haggling/Offers
Ebay links
Other Cars
Other Websites
General Chat
Previous Thread
Next Thread
Print Thread
SQL question #1547962
28/08/2015 14:11
28/08/2015 14:11

E
elder81
Unregistered
elder81
Unregistered
E



Hello all,

I am doing some work on a customer database and need to delete specific date from a back up table. The data needs to be the top 10 based or date order. (Don't ask why :))

The following script does not work, but what could i use?

delete (select top 10 * from back_notifmsgqueue
order by timetosend desc)
from back_notifmsgqueue

Re: SQL question [Re: ] #1547965
28/08/2015 14:28
28/08/2015 14:28
Joined: May 2011
Posts: 2,388
Caston, Norfolk
Blueboyracer Offline
Competition Level
Blueboyracer  Offline
Competition Level

Joined: May 2011
Posts: 2,388
Caston, Norfolk
If you mean delete the previous 10 days data from today in a table then the following would be do it...

delete from
back_notifmsgqueue
where
timetosend >= date_sub(curdate(), interval 10 day)

However, if you mean that you want to delete the most recent 10 days in a table and it has not been updated for a period of time then you will have to perform a subquery based upon your max(timetosend) to achieve the same

Last edited by Blueboyracer; 28/08/2015 14:29.
Re: SQL question [Re: ] #1547967
28/08/2015 14:40
28/08/2015 14:40
Joined: Dec 2005
Posts: 812
Sussex
StuP Offline
Enjoying the ride
StuP  Offline
Enjoying the ride

Joined: Dec 2005
Posts: 812
Sussex
Use a sub-query as above, something like this:

DELETE FROM back_notifmsgqueue
WHERE <ID> IN (SELECT TOP 10 <ID> FROM back_notifmsgqueue
ORDER BY timetosend DESC)

Change the <ID> to whatever your primary key is called

Re: SQL question [Re: ] #1547970
28/08/2015 15:24
28/08/2015 15:24

E
elder81
Unregistered
elder81
Unregistered
E



Thats what I was trying to acheive in other examples, but without the IN.

Thanks StuP

Re: SQL question [Re: ] #1547971
28/08/2015 15:25
28/08/2015 15:25
Joined: May 2011
Posts: 2,388
Caston, Norfolk
Blueboyracer Offline
Competition Level
Blueboyracer  Offline
Competition Level

Joined: May 2011
Posts: 2,388
Caston, Norfolk
Think I over complicated it ... DOH

Re: SQL question [Re: ] #1547975
28/08/2015 15:52
28/08/2015 15:52
Joined: Dec 2005
Posts: 12,295
Sandhurst
Begbie Offline
Ex El Presidente
Begbie  Offline
Ex El Presidente
I AM a Coop

Joined: Dec 2005
Posts: 12,295
Sandhurst
Please use 'Begin Tran' before you do the delete, as I've just had to restore a 160GB DB back for a client because they did they a delete statement before checking what it would actually delete.


Originally Posted by Jonny - After being taken out at Spa
Your car is Usain Bolt with wellies
Re: SQL question [Re: Begbie] #1549797
17/09/2015 22:20
17/09/2015 22:20
Joined: Dec 2014
Posts: 420
uk
OnlyItalian Offline
Enjoying the ride
OnlyItalian  Offline
Enjoying the ride

Joined: Dec 2014
Posts: 420
uk
Originally Posted By: Begbie
Please use 'Begin Tran' before you do the delete, as I've just had to restore a 160GB DB back for a client because they did they a delete statement before checking what it would actually delete.


That is properly good advice. Don't commit the transaction until you're sure the query does what you want. Same goes for insert and update statements really. And for added safety and if the db is offline then take a backup before doing anything.

Last edited by OnlyItalian; 17/09/2015 22:20.

"Proud owner of LE141 and a 99 VIS broomie "

Powered by UBB.threads™ PHP Forum Software 7.7.1
(Release build 20190129)
PHP: 7.3.33 Page Time: 0.010s Queries: 14 (0.004s) Memory: 0.7664 MB (Peak: 0.8311 MB) Data Comp: Off Server Time: 2024-05-16 00:52:29 UTC