473,505 Members | 13,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

order by with variable

hello,

I have listings with coordinates, i would like to do ORDER BY and
display listings with coords first. If they don't have coords the value
is blank and the coords go both positive and negative values. So i
tried an "ORDER by l_xcoord HAVING l_xcoord<>0" but this is acting like
a WHERE statement removing all results with no coords. I tried GROUP BY
as well but same results...

So the question is how can i display all results with the ones that
have coords first? It seems like it should be simple but I'm just not
seeing it.

Thanks,

Chad

Jun 8 '06 #1
5 2212
>I have listings with coordinates, i would like to do ORDER BY and
display listings with coords first.
If they don't have coords the value
is blank and the coords go both positive and negative values.
If they don't have coords the values that the coords don't have
can be positive or negative? Huh? Also, there's no such value
as "blank" for a numeric field. Null, yes, blank, no.
So i
tried an "ORDER by l_xcoord HAVING l_xcoord<>0" but this is acting like
a WHERE statement removing all results with no coords. I tried GROUP BY
as well but same results...
ORDER BY if(l_xcoord<>0, 0, 1), l_xcoord

should do what I think you want.

So the question is how can i display all results with the ones that
have coords first? It seems like it should be simple but I'm just not
seeing it.


The trick here is to come up with an expression you can put earlier
in the sort order which distinguishes the cases you want.
if(l_xcoord<>0, 0, 1) has the value 0 if there are coordinates, 1
if there are not.

Gordon L. Burditt
Jun 8 '06 #2
Hey Gordon,

I didn't know you could put if() ststements into a SELECT. I will give
that a try. yes NULL not blank thanks for that, and the coords in
general are both positive and negative values, and if it is blank or
NULL obviously it wouldn't be + or - .

how does the if work exactly in a SELECT? you wrote "ORDER BY
if(l_xcoord<>0, 0, 1), l_xcoord" So we have if(item<>value, 0, 1)item
does this mean 0 is like true and 1 is false or do the numbers mean
something else like number value lengths??? And then i just put the
order by l_xcoord after it? Can you explain how it works?

I use if statements in php but don't recognize the 0, 1 in it.

Thanks,

Chad

Gordon Burditt wrote:
I have listings with coordinates, i would like to do ORDER BY and
display listings with coords first.
If they don't have coords the value
is blank and the coords go both positive and negative values.


If they don't have coords the values that the coords don't have
can be positive or negative? Huh? Also, there's no such value
as "blank" for a numeric field. Null, yes, blank, no.
So i
tried an "ORDER by l_xcoord HAVING l_xcoord<>0" but this is acting like
a WHERE statement removing all results with no coords. I tried GROUP BY
as well but same results...


ORDER BY if(l_xcoord<>0, 0, 1), l_xcoord

should do what I think you want.

So the question is how can i display all results with the ones that
have coords first? It seems like it should be simple but I'm just not
seeing it.


The trick here is to come up with an expression you can put earlier
in the sort order which distinguishes the cases you want.
if(l_xcoord<>0, 0, 1) has the value 0 if there are coordinates, 1
if there are not.

Gordon L. Burditt


Jun 8 '06 #3
i just tried it and your example works great. I would still like to
understand it more so i can use it further.

what if you wanted to do the same thing but with a work.

like you searched the listings for a keyword, and wanted to return a
weighted result.

If the table has a title, and a description which we will call l_title
and l_desc and you search looking for a keyword so, where l_title or
l_desc = 'keyword' and then want to order by if the keyword matches the
title first and then the desc. Wouldn;t this give "weight to the title"
I just dont understand the 0, 1 at the end of the if yet to modify it.

Could something like this work?

Thanks for your help.

Chad

ch*********@gmail.com wrote:
Hey Gordon,

I didn't know you could put if() ststements into a SELECT. I will give
that a try. yes NULL not blank thanks for that, and the coords in
general are both positive and negative values, and if it is blank or
NULL obviously it wouldn't be + or - .

how does the if work exactly in a SELECT? you wrote "ORDER BY
if(l_xcoord<>0, 0, 1), l_xcoord" So we have if(item<>value, 0, 1)item
does this mean 0 is like true and 1 is false or do the numbers mean
something else like number value lengths??? And then i just put the
order by l_xcoord after it? Can you explain how it works?

I use if statements in php but don't recognize the 0, 1 in it.

Thanks,

Chad

Gordon Burditt wrote:
I have listings with coordinates, i would like to do ORDER BY and
display listings with coords first.
If they don't have coords the value
is blank and the coords go both positive and negative values.


If they don't have coords the values that the coords don't have
can be positive or negative? Huh? Also, there's no such value
as "blank" for a numeric field. Null, yes, blank, no.
So i
tried an "ORDER by l_xcoord HAVING l_xcoord<>0" but this is acting like
a WHERE statement removing all results with no coords. I tried GROUP BY
as well but same results...


ORDER BY if(l_xcoord<>0, 0, 1), l_xcoord

should do what I think you want.

So the question is how can i display all results with the ones that
have coords first? It seems like it should be simple but I'm just not
seeing it.


The trick here is to come up with an expression you can put earlier
in the sort order which distinguishes the cases you want.
if(l_xcoord<>0, 0, 1) has the value 0 if there are coordinates, 1
if there are not.

Gordon L. Burditt


Jun 8 '06 #4
>i just tried it and your example works great. I would still like to
understand it more so i can use it further.
if(expression, truevalue, falsevalue)
is an expression. (forget about an if *statement* here or the
syntax of one). It has the value truevalue if expression is not
equal to 0 and not null and the value falsevalue of expression is
0 or null. It can be used where an expression can be used.

Note that
if(x = 2, 1, 0)
and
if(x != 2, 0, 1)
and
x = 2
are different if x is null (and otherwise they are equal).

Another one you might want to use is
ifnull(expression1, expression2)
which returns expression1 if expression1 is not null, otherwise
it returns expression2 (so expression2 is the "default value" that
replaces a null value in expression1).
ORDER BY lets you sort the result based on an expression. For example,

ORDER BY (xcoord - 5.0)*(xcoord - 5.0) + (ycoord - 2.0)*(ycoord - 2.0)

orders by the distance (well, actually here, it's the SQUARE of the
distance, since square roots are expensive and using one won't
change the order) from (5.0, 2.0).

what if you wanted to do the same thing but with a work.
Huh? You mean homework?
like you searched the listings for a keyword, and wanted to return a
weighted result.
You can do things like:

order by case b.politics
when 'republicrat' then 1
when 'demopublican' then 2
when 'libertarian' then 3
when 'communist' then 4
when 'fascist' then 5
when 'asshole' then 6
when 'jerk' then 7
else 0
end

where all that stuff from case ... end is just another expression. It
would sort things in the order:

somethingelse
republicrat
demopublican
libertarian
communist
fascist
asshole
jerk
If the table has a title, and a description which we will call l_title
and l_desc and you search looking for a keyword so, where l_title or
l_desc = 'keyword' and then want to order by if the keyword matches the
title first and then the desc. Wouldn;t this give "weight to the title"
I just dont understand the 0, 1 at the end of the if yet to modify it.

Could something like this work?


I'm not sure I understand what you are asking for, but something
like:

order by 2*(l_title = 'keyword') + 1*(l_desc = 'keyword') desc

would put the records in order:
both title and description match
title only match
description only match
neither match
.. Assuming that l_title and l_desc are NOT NULL fields.
if() is useful for defaulting the NULL case one way or the other.

Another possiblity is:
order by if(l_title='keyword', 0, if(l_desc = 'keyword', 1, 2))
which puts things in the order:
title match (0)
description match but no title match (1)
neither match or null (2)

Gordon L. Burditt

Jun 8 '06 #5
Thanks Gordon, you have just answered huge questions for me, and opened
new doors in my limited skills. I really appreciate it.

Chad
Gordon Burditt wrote:
i just tried it and your example works great. I would still like to
understand it more so i can use it further.


if(expression, truevalue, falsevalue)
is an expression. (forget about an if *statement* here or the
syntax of one). It has the value truevalue if expression is not
equal to 0 and not null and the value falsevalue of expression is
0 or null. It can be used where an expression can be used.

Note that
if(x = 2, 1, 0)
and
if(x != 2, 0, 1)
and
x = 2
are different if x is null (and otherwise they are equal).

Another one you might want to use is
ifnull(expression1, expression2)
which returns expression1 if expression1 is not null, otherwise
it returns expression2 (so expression2 is the "default value" that
replaces a null value in expression1).
ORDER BY lets you sort the result based on an expression. For example,

ORDER BY (xcoord - 5.0)*(xcoord - 5.0) + (ycoord - 2.0)*(ycoord - 2.0)

orders by the distance (well, actually here, it's the SQUARE of the
distance, since square roots are expensive and using one won't
change the order) from (5.0, 2.0).

what if you wanted to do the same thing but with a work.


Huh? You mean homework?
like you searched the listings for a keyword, and wanted to return a
weighted result.


You can do things like:

order by case b.politics
when 'republicrat' then 1
when 'demopublican' then 2
when 'libertarian' then 3
when 'communist' then 4
when 'fascist' then 5
when 'asshole' then 6
when 'jerk' then 7
else 0
end

where all that stuff from case ... end is just another expression. It
would sort things in the order:

somethingelse
republicrat
demopublican
libertarian
communist
fascist
asshole
jerk
If the table has a title, and a description which we will call l_title
and l_desc and you search looking for a keyword so, where l_title or
l_desc = 'keyword' and then want to order by if the keyword matches the
title first and then the desc. Wouldn;t this give "weight to the title"
I just dont understand the 0, 1 at the end of the if yet to modify it.

Could something like this work?


I'm not sure I understand what you are asking for, but something
like:

order by 2*(l_title = 'keyword') + 1*(l_desc = 'keyword') desc

would put the records in order:
both title and description match
title only match
description only match
neither match
. Assuming that l_title and l_desc are NOT NULL fields.
if() is useful for defaulting the NULL case one way or the other.

Another possiblity is:
order by if(l_title='keyword', 0, if(l_desc = 'keyword', 1, 2))
which puts things in the order:
title match (0)
description match but no title match (1)
neither match or null (2)

Gordon L. Burditt


Jun 8 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5878
by: Daniel Ruscoe | last post by:
Hi chaps, I'm relatively new to the language, but I want to create a simple order form using PHP and cookies. Please let me know if there's a better way in this situation, database isn't...
4
1710
by: Jefferis NoSpamme | last post by:
I have a query that is showing new items in a store based upon their inventory date. The problem with my Random order sort is that when the "next" link is pressed, the random function is called...
7
3638
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
1
3783
by: Holden | last post by:
Hello, I am using Visual C++ 6.0 with Service Pack 5 installed and I have run into a problem that I haven't been able to figure out. I have written two classes and I want them to have pointers to...
13
12123
by: Dark Rayden | last post by:
Hi! I recently got a strange problem and I have no idea on the solution. I try to do a ORDER BY statement with a fixed order of values, because my client want's it this way. My approach is...
6
1432
by: lig | last post by:
hi , i am trying to do something that is not working (cuz i tried that) so i hope for some ideas about it. ok, so i have this recursive function, lets call it recFun and i wanted to be able to...
7
2146
by: pauld | last post by:
Ive got a series of 2 dimensional associative arrays while(list($key, $val) = each ($results)) { {foreach($val as $i=>$v) print "$i = $v"; } } but i want to define the order that the...
13
2658
by: Thomas Mlynarczyk | last post by:
Hi, I have this code: class Test { public $status = 'dead'; function __construct() { $this->status = 'alive'; } function __destruct() { echo '<br>__destruct()'; } }
1
3509
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
3
5830
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
0
7298
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4698
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1526
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.