user319940
user319940

Reputation: 3317

Sorting by position in MySQL, multiple products assigned to one position

I'm using an ecommerce system and I'm trying to modify how products are sorted.

My question is, how does mysql deal with sorting when two items have the same value?

e.g.

Products are sorted by a position that is assigned to them (so position 1 would display at the top).

However, if I have 3 products assigned to position 1 and 2 products assigned to position 2, how are the 3 products assigned to position 1 sorted by default? Would this be specific to the ecommerce system or is there a mysql default, such as ID?

I hope this is clear enough, I've not been able to really find any good answers by Googling.

Upvotes: 0

Views: 118

Answers (3)

Gene
Gene

Reputation: 46960

You can give multiple column names (say A,B,C) in the ORDER BY clause. When values are equal in A, then B is used to make sorting decisions. If also equal in B, then C is used. This called a lexicographic ordering. See any SQL documentationn of the SELECT command and ORDER BY clause.

Upvotes: 0

juergen d
juergen d

Reputation: 204766

The result is random since no other "default" order will be applied. If you need to sort by more than one column you can do it like this

order by position, id, date

Upvotes: 0

eggyal
eggyal

Reputation: 125865

You can specify additional fields on which to order by, the earliest ones take precedence but latter ones decide how to break ties. For example:

ORDER BY foo ASC, bar DESC

Upvotes: 2

Related Questions