473,659 Members | 3,592 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 2227
>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*********@gma il.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(expressi on1, 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='key word', 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(expressi on1, 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='key word', 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
5888
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 availible. What I'm trying to get is this: 1) Variable(A) passed from HTML page to script. 2) Script checks for cookie.
4
1717
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 again and I may see the image previously displayed rather than go through the entire new inventory. Any suggestions on how to prevent that occurrence? SELECT * FROM `Catalog`
7
3654
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. And i have things to offer, and to request. And a lot of ideas, but who needs them.... here's an example (from type_struct.py):
1
3791
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 each other. So class One has a private class variable of type Two and class Two has a private class variable of type One. I #include the header file of the other class in each file. When I try to build the project it gives me three errors, all...
13
12152
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 like this: $the_row = "'A', 'B', 'B b', 'C', 'D d', 'E'";
6
1438
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 call it in a specific order. in a simple way : evaluating some expression in its original order see the following part of code:
7
2152
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 various $i's are printed in
13
2677
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
3526
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 following (with incomplete succes): I want in a given namespace Parameters a list of "initializers" (which are objects derived from a simple interface that can be implemented anywhere, and are used to define which parameters the program will take at...
3
5845
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 section describes a solution using methods returning references to static objects. But consider:
0
8337
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8531
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7359
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2754
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 we have to send another system
2
1978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.