Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 12th, 2012  |  syntax: None  |  size: 1.39 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Order MySQL data by row value
  2. id | itemId
  3. ---|------
  4. 0  | 1
  5. 1  | 2
  6. 2  | 3
  7.        
  8. id | itemId | key          | values
  9. ---|--------|---------------
  10. 0  | 1      | itemreceived | 2012-06-01
  11. 1  | 1      | modelyear    | 1992
  12. 2  | 1      | model        | 2
  13. 3  | 2      | itemreceived | 2012-06-05
  14. 4  | 2      | modelyear    | 2003
  15. 5  | 2      | model        | 1
  16. 6  | 3      | itemreceived | 2012-07-05
  17. 7  | 3      | modelyear    | 2000
  18. 8  | 3      | model        | 3
  19.        
  20. SELECT items.*, item_specs.* FROM item_specs
  21. INNER JOIN item_specs ON items.itemId = item_specs.itemId
  22. WHERE itemId IN(1,2,3)
  23.        
  24. SELECT items.*, item_specs.*
  25. FROM item_specs
  26. INNER JOIN item_specs ON items.itemId = item_specs.itemId
  27. INNER JOIN item_specs aux ON (aux.key = 'model' and aux.itemID = item_specs.itemId)
  28. WHERE item_specs.itemId IN(1,2,3)
  29. ORDER BY aux.values/*this is the model*/, item_specs.id;
  30.        
  31. SELECT items.*,
  32.        item_specs.*,
  33.        (select aux.values
  34.         from item_specs aux
  35.         where aux.key = 'model' and aux.itemID = item_specs.itemId
  36.         ) as model
  37. FROM item_specs
  38. INNER JOIN item_specs ON items.itemId = item_specs.itemId
  39. WHERE item_specs.itemId IN(1,2,3)
  40. ORDER BY model, item_specs.id;
  41.        
  42. SELECT * FROM `table` WHERE `key` = 'model' ORDER BY `values` ASC
  43.        
  44. SELECT * FROM `table`
  45.    Order by (case When Key='model' then 0 else 1 end), values
  46.        
  47. SELECT * FROM `table`
  48. WHERE `key` = 'model'
  49. ORDER BY `values`;