• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 368
  • Last Modified:

mysql swap 2 rows

I have a table of images which contains 3500 records.
The site is already live.
Now, in the frontend i want to change the display order of images.
Right now it displays in the order it is inserted.
I wrote code so that the ids can be swapped from backend but even then it won't help.
Its now not possible for me to change queries in frontend in each and every location to write  " order by id"
Is there any other way out ??
Right now when i say :
SELECT id, imageh, imagew, filename FROM picture WHERE pictures_siteid =212 LIMIT 13
it displays
1042,1043,1041,1039,1147
0
Desire2change
Asked:
Desire2change
  • 3
  • 2
1 Solution
 
Ioannis AnifantakisSoftware EngineerCommented:
SELECT id, imageh, imagew, filename FROM picture WHERE pictures_siteid =212 order by id DESC LIMIT 13;

if you make "DESC" at the order by then it orders in "descending" motion.
if you don't put "DESC" it assumes "ASC" which is the "ascending motion"

so

>order by id
is similar to
>order by id ASC
and will give you ordering from lower to higher numbers

>order by id DESC
will give you descending ordering, that is from the higher to the lower value
0
 
Ioannis AnifantakisSoftware EngineerCommented:
Other than that, you can put a timestamp in your table to ensure that no matter what the id, if you make "order by mytimestamp desc" you will for sure get descending ordering.

Unfortunatelly there is no other way arround it
0
 
Desire2changeAuthor Commented:
i don't want to change the queries in the frontend.
right now in frontend the order is set as they are inserted
is there a way that without disturbing the frontend , i can change the order of data in database
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
Ioannis AnifantakisSoftware EngineerCommented:
I am sorry, but I don't follow you.
You wish to change the way your data comes, without changing your query???

What do you mean by "frontend"?

Explain me and I might be able to understand so I can help you
0
 
Chris StanyonCommented:
You can alter the order of the data in the table by ALTERing it with an ORDER BY clause:

ALTER TABLE foo ORDER BY key;

Once you've run this query, then the 'default order of the table will be changed, so if you call the query SELECT * FROM foo, it will be ordered by 'key'.




0
 
Desire2changeAuthor Commented:
Thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now