FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


order by difference





Peterssidan
Is it possible to take some entries from a table and order them so the in the how much a number is difference from another number. I can give an example:

I have this table

id | name | number
------------------------
0 | .... | 5
1 | .... | 3
2 | .... | 1
3 | .... | 8

so say I want to select the entries with name .... and order them by |3-number| (the absolute value)
So I get numbers in this order:

id | name | number
------------------------
1 | .... | 3
0 | .... | 5
2 | .... | 1
3 | .... | 8

I want to use php to call this statement. Will compare some timestamps this way. So enyone know if it's possible?
AftershockVibe
Yeah you can do this. However, I have to point out that your timestamps should always be positive anyway.

Anyhoo...
Code:
SELECT name, ABS(3 - number) AS orderVal
FROM <tableName>
ORDER BY orderVal



Edit: Nevermind! You get your negative timestamps from the subtraction. Duh!
Peterssidan
I got it to work =) Thanks! I have never used that you wrote with AS orderVal. It looks a bit strange to me.. looks like it selects both name and ABS(3 - number) in some way when I look at it.. lol I can read more about it late.

Quote:
I have to point out that your timestamps should always be positive anyway.

My timestamps in my database is positive. both the 3 and the number will be timestamps and this will sort them with the difference. It's works that's the most importent.
jabapyth
wow. i never knew you could do 'AS oderVal'
Peterssidan
jabapyth wrote:
wow. i never knew you could do 'AS oderVal'

But I can' see the reason why. The same thing can be done by simply write ORDER BY ABS(3 - number) instead. Maybe AftershockVibe know if there is a advantage to do like you have done?
AftershockVibe
No there's no advantage to it. I just thought you wanted to read the value as well Wink
Peterssidan
AftershockVibe wrote:
No there's no advantage to it. I just thought you wanted to read the value as well Wink

ah. then I understand more. Very Happy No I don't need to take the number out it was interesting. Applause
Related topics
Windows Tips&tricks!
20 Frih$
Difference between young girl and young married woman!
FOR WAR OR NOT
mIRC for begginers.
If Hardly Davidson made Computers instead of Motorcycles...
Whats the difference of FRIH$ and Points?
Tutorial: PHP Installed Modules Dynamic Reference Tool
ebsite Promotion Secret: How To Explode Your Website Sales
Dems: these are merely the facts
How much do fans cost in a computer store?
What is the difference between a Domain & Sub Domain Nam
The difference between science and engineering?
CSS1 vs CSS2: any difference?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.