Update last 10 records?

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,
brihol44Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
Angelp1ayCommented:
So something like:

    UPDATE Set field = blah ORDER BY id_field DESC LIMIT 10
0
brihol44Author Commented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ee_rleeCommented:
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
ursangelCommented:
write the update for teh table where the records will be selected either TOP 10 when ordered by DESC.
0
Angelp1ayCommented:
brihol: Did you get this to work?
0
brihol44Author Commented:
Yes, thanks! I was on vacation for some time and I'm back getting into this part of the project.

Thanks,

B
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.