?
Solved

Update last 10 records?

Posted on 2008-02-01
8
Medium Priority
?
1,081 Views
Last Modified: 2008-03-26
Searched the web to find out how to update the last 10 rows of my table (MySQL) but I can't seem to find anything. Is there such a way? I need to be able to update the last 10 rows dynamically so I won't know any specific ID's  to specify the update.

Thanks,
0
Comment
Question by:brihol44
8 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20797718
last 10 in a given ORDER BY ... ASC <=> first 10 using ORDER BY ... DESC

so, just use the proper ORDER BY and add the LIMIT 0,10, so it should be fine.
0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 20797820
So something like:

    UPDATE Set field = blah ORDER BY id_field DESC LIMIT 10
0
 

Author Comment

by:brihol44
ID: 20797931
hmm.....I'm getting an error with my update statement.

 Incorrect usage of UPDATE and ORDER BY
    UPDATE stats_daily s
    INNER JOIN stats_daily_temp st
    ON s.date = st.date
    SET s.page_views = st.page_views,
	    s.visits = st.visits,
		s.hits = st.hits,
		s.bandwidth = st.bandwidth
	WHERE s.date = st.date
	ORDER BY daily_id DESC LIMIT 10

Open in new window

0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
LVL 11

Accepted Solution

by:
Angelp1ay earned 1300 total points
ID: 20800993
I think you need the SET before the JOIN:
    UPDATE stats_daily s
    SET s.page_views = st.page_views,
        s.visits = st.visits,
        s.hits = st.hits,
        s.bandwidth = st.bandwidth
    INNER JOIN stats_daily_temp st
    ON s.date = st.date
    WHERE s.date = st.date
    ORDER BY daily_id DESC LIMIT 10

Open in new window

0
 
LVL 26

Assisted Solution

by:ee_rlee
ee_rlee earned 500 total points
ID: 20807711
try this
UPDATE stats_daily s INNER JOIN stats_daily_temp st ON s.date = st.date
    SET s.page_views = st.page_views,
        s.visits = st.visits,
        s.hits = st.hits,
        s.bandwidth = st.bandwidth
    WHERE s.daily_id IN
    (SELECT s2.daily_id FROM stats_daily s2
    ORDER BY s2.daily_id DESC LIMIT 10)

Open in new window

0
 
LVL 5

Assisted Solution

by:ursangel
ursangel earned 200 total points
ID: 20812297
write the update for teh table where the records will be selected either TOP 10 when ordered by DESC.
0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 20983943
brihol: Did you get this to work?
0
 

Author Comment

by:brihol44
ID: 21216205
Yes, thanks! I was on vacation for some time and I'm back getting into this part of the project.

Thanks,

B
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question