I have a field in my table which contains categories data eg
Computer > Mouse
Computer > Keyboard
Archived Game > Strategy
PS3 > Game > sports
when I pull them out of the table I strip all but the last word using:
$category = trim(substr($databack20[main_category], strrpos($databack20[main_category], '>') + 1));
Which works perfectly.
I need to pull these out of the table, in name order
So currently I get:
Archived Game > Strategy
Computer > Keyboard
Computer > Mouse
PS3 > Game > Sports
But the order I need is:
Keyboard
Mouse
Sports
Strategy
Is there a way to do this within the query? If so how, if not is there another way?
While I do agree with the answers, you should split out the string into separate fields, you can sort it as you'd like, although performance will not be very good. Try this
ORDER BY LCASE(RIGHT(main_category, LOCATE('>', REVERSE(main_category) ) ))
SELECT
*
FROM
(
<your current query>
)
AS data
ORDER BY
field_name_1,
field_name_2
I have a field in my table which contains categories data eg
this is what you are doing wrong.
the catalog hierarchy should be implemented using more intelligent design.
The very basic rule of the database design is that data in the fields should be atomic , ie represent only one matter.
Mixing matters will lead you to the site malfunction pretty soon.
You have to store each catalog node in a separate field. Thus you'll be able to order by whatever level nodes. You may also wish to add a "parent" field, to link a node with a parent one.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.