user1967599
user1967599

Reputation:

MySQL data sort

I have a table that has four columns: id, item_number, feature, value.

The table looks like this and has about 5 million entries.

╔════╦═════════════╦═════════╦═══════╗
║ id ║ item_number ║ feature ║ value ║
╠════╬═════════════╬═════════╬═══════╣
║ 1  ║ 234         ║ 5       ║ 15    ║
║ 2  ║ 234         ║ 3       ║ 256   ║
║ 3  ║ 453         ║ 5       ║ 14    ║
║ 4  ║ 453         ║ 4       ║ 12    ║
║ 5  ║ 453         ║ 7       ║ 332   ║
║ 6  ║ 17          ║ 5       ║ 88    ║
║ 7  ║ 17          ║ 9       ║ 13.86 ║
╚════╩═════════════╩═════════╩═══════╝

How can I sort the table so that I can get the item_numbers in descending order based on the feature value?

I am also selecting other feature numbers with their values but I only want to sort by feature number 5.

Upvotes: 2

Views: 118

Answers (3)

Brian Gerhards
Brian Gerhards

Reputation: 849

In your query, add

order by item_number desc

If you are trying to query based on a specific feature, so only receive one set of data for a feature at at time, add

where feature = 'feature'

where "feature" is the feature value you want to search for. If you are looking to provide all features but sort them, you can add

order by feature, item_number desc

and you will be give all features in ascending order and together (grouped) then the items_number(s) in descending order

EDIT:: Sounds like from your latest comment, this may be your solution:

SELECT item_number FROM table WHERE feature = '5' ORDER BY value DESC

Upvotes: 0

potashin
potashin

Reputation: 44581

Using order by with desc and where clauses:

select `item_numbers`
from `tbl`
where `feature` = 5
order by `value` desc

Upvotes: 3

Vikas Umrao
Vikas Umrao

Reputation: 2615

You need to do order by first with feature and then with item_numbers

select * from `table` order by `feature`, `item_numbers` desc

Upvotes: 2

Related Questions