Author Topic: MySQL "ordering" (show some value first)  (Read 825 times)

Offline saljutin

  • Level 22
  • *
  • Posts: 266
  • Reputation: +6/-0
    • View Profile
MySQL "ordering" (show some value first)
« on: November 11, 2010, 09:11:33 AM »
lets say we have table
id/a/b/c

we want to show "a"

SELECT a FROM table WHERE b=1

it provides what I want ... BUT not exactly
lets say c can be 1,2,3,4 and I want to "show" c=1 on top ... so some sort of order by BUT if c=3 it does not work so lets say like this:
c=3
c=3
c=2
c=4
c=2
c=1

it only matters that that particular c is on top :)
I was thinking by solving this problem by making 2 queries, but I guess there is some other solution?

Offline lolninja

  • Level 19
  • *
  • Posts: 194
  • Reputation: +5/-0
  • BSc powered Programmer
    • View Profile
    • HTTPmmo
Re: MySQL "ordering" (show some value first)
« Reply #1 on: November 11, 2010, 10:06:10 AM »
SQL has a built in sort function called ORDER BY, dead simple to use, just list the variables you want to sort your data by and it'll happen.

For example
Code: [Select]
SELECT a FROM table WHERE b=1 ORDER BY c ASC
Has fun :)

Offline saljutin

  • Level 22
  • *
  • Posts: 266
  • Reputation: +6/-0
    • View Profile
Re: MySQL "ordering" (show some value first)
« Reply #2 on: November 11, 2010, 10:16:21 AM »
I think you don't understand me

this is not ORDER BY...what if C is 1,2,3,4,5
and I want this to be DEAD simple but its not :)

and I want ONE particular value of C to be ON top....
let say you have
b/c
t1/3
t2/1
t3/2
t4/2
t5/4

I want this to arrange that t3/2 and t4/2 is ON TOP

so if I use that "simple" solution of you...which if ofc known :P
either it will start with c=4 or c=1 :D

Offline CygnusX

  • Level 24
  • *
  • Posts: 303
  • Reputation: +3/-2
    • View Profile
    • Lords of Midnight
Re: MySQL "ordering" (show some value first)
« Reply #3 on: November 11, 2010, 10:31:38 AM »
I don't believe there is a way to custom sort in mysql.  I think you're going to need to load this data into an array, and do a custom sort function that moves these values to the top of your list.  You can then walk the array if need be.

That, or you could run 2 queries.  One that excludes the value of C you're looking for, and one that only has the value of C you want.  This would effectively provide the data you're after.

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: MySQL "ordering" (show some value first)
« Reply #4 on: November 11, 2010, 10:38:05 AM »
modulo (%)

Check one topic started by me (it should have "SQL" in title, probably in coding or general board), I had similar problem and people gave some interesting solutions.

Offline saljutin

  • Level 22
  • *
  • Posts: 266
  • Reputation: +6/-0
    • View Profile
Re: MySQL "ordering" (show some value first)
« Reply #5 on: November 11, 2010, 12:57:14 PM »
CygnusX - yeah that 2 queries solution would be the easiest to implement...but I was curious if SQL have some sort of command

so Chris I found that post:
SELECT * FROM table ORDER BY c, b == $c

$c is the value that need to be on top...so this will work? :)

Offline Harkins

  • Level 28
  • **
  • Posts: 424
  • Reputation: +11/-2
  • Coder, blogger, entrepreneur.
    • View Profile
    • Push CX - Blog
Re: MySQL "ordering" (show some value first)
« Reply #6 on: November 11, 2010, 03:02:43 PM »
Code: [Select]
select a from table order by c != 3, c;

Visit #bbg on irc.freenode.net to talk browser games anytime.


 


SimplePortal 2.3.3 © 2008-2010, SimplePortal