Forum Moderators: open

Message Too Old, No Replies

MySQL union, join or order by?

         

immatt

3:36 pm on Nov 3, 2009 (gmt 0)

10+ Year Member



I have a database with a lot of information that I would like to be ordered in the way that I want. For example, if I had a database with these brands:
Sony
RCA
Vizio
Samsung
Panasonic

And I want to come out in this order:
Panasonic
Vizio
Samsung
Sony
RCA

How would I go about doing this? I have thought about assigning a number to each brand and have it come out in that order, but is there a better way for this?

LifeinAsia

4:24 pm on Nov 3, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



That's what I would do.

Also make sure that you allow gaps in your current numbering to allow for additions in the future. (In other words, if you have
Panasonic, 1
Vizio, 2
Samsung, 3
Sony, 4
RCA, 5
You'll most likely have to do a lot of renumbering in the future if you want to add a new brand at the top spot.

topr8

4:41 pm on Nov 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



or have a table which has brands and their position, eg column_brand_name column_sort_order

this way if you insert a new brand you only have to change the positions in one table, also you can easily change the positions for whatevr reason at any time but updating only one table

immatt

5:06 pm on Nov 3, 2009 (gmt 0)

10+ Year Member



I always think that there is an easier way then the one I think of. Thank you for the help!

whoisgregg

8:17 pm on Nov 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can also use ORDER BY FIELD to accomplish this:

SELECT * FROM `products` ORDER BY FIELD( brand, 'Panasonic', 'Vizio', 'Samsung', [...]) ASC

topr8

8:38 pm on Nov 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



well i never knew that, thanks whoisgregg

whoisgregg

9:02 pm on Nov 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It ends up being really handy when the ranking of rows is based on something other than the data in the table itself. For example, if you want to rank products by a combination of factors in multiple tables (pageviews, recent sales, previously bought from current customer, etc.) then you can more easily do that in the scripting and just push an ordered list of ids. Pseudocode:

 SELECT * FROM `table` WHERE `id` IN ( $list_of_ids ) ORDER BY FIELD( id, $list_of_ids) ASC LIMIT 0,10

Demaestro

10:44 pm on Nov 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I just want to throw my vote out there for a new column called "sort_order"

I find when you want to change the order they appear in later that it is easier to change the value of the "sort_order" field than it is to find all the queries that use the "ORDER BY FIELD" logic and then change those queries.

Both are fine solutions, in my eyes one is easier to maintain.

topr8

11:28 pm on Nov 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



absolutely whoisgregg i immediately saw some great things i could do with ORDER BY FIELD

especially multi table sorts where the data is not naturally (correctly) sorted in some kind of alphanemeric by default.

i've come from sqlserver and can't believe i've never used this method before, thanks!