Welcome Guest!

If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:

  • Receive Fast Advice
  • Learn Programming Languages
  • Get Professional Website Reviews
  • Quick Troubleshooting Assistance

> Trying To Avoid Splitting Into 2 Queries...

This is a discussion on Trying To Avoid Splitting Into 2 Queries..., within the MySQL section. This forum and the thread "Trying To Avoid Splitting Into 2 Queries..." are both part of the Programming Your Website category.

 
Reply to this topicStart new topic
> Trying To Avoid Splitting Into 2 Queries...
paintingtheweb
post Apr 14 2008, 12:10 PM
Post #1


Squeezing
***

Posts: 76
Joined: 14-February 08
From: Las Vegas, NV


Hey gurus. I have a query I'm trying to run that I can only figure out how to do with 2 queries, but there's this itch telling me there's a better way of doing it. I'll post what I've got going right now and then try to explain it.
CODE
SELECT thumb, store_price
FROM inventory
WHERE (brand = 'mybrand')
ORDER BY thumb DESC, store_price DESC
LIMIT 0, 20

What I'm doing here is grabbing items from the inventory table and sorting them by whether or not they have a picture associated with it (top priority) and then sorting it by the price of the item so the higher priced items are placed on top. Unfortunately, when sorting by the image (basically checking whether or not it is null), the sort by price becomes pretty much useless. The only workaround I could think of was to split it into 2 queries and set the WHERE clause to include thumb <> '' and run a second one to inlcude the ones that are blank. That way the order isn't touched by the name of the image. This will work, but like I said, I have this little itch telling me there's a better way to do this.


--------------------
http://www.infoonmike.com

"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live." Martin Golding
Go to the top of the page
 
+Quote Post
rewake
post Apr 14 2008, 01:12 PM
Post #2


Rapid Squeezer
Group Icon

Posts: 239
Joined: 14-February 08
From: NY, USA


Hi PTW,

The query you posted seems as if it should work fine, but if it's not then there might be something else that's causing a problem. What data types are these two fields?

Maybe it's just a matter of setting the `thumb` data type to bool, or int(1), and setting the default value to 0.

Rich


--------------------
QUOTE
if ($name=='will') echo '/(bb|[^b]{2})/';

Raineri Jewelers | MySpace | Facebook | deviantART
Go to the top of the page
 
+Quote Post
paintingtheweb
post Apr 14 2008, 05:32 PM
Post #3


Squeezing
***

Posts: 76
Joined: 14-February 08
From: Las Vegas, NV


QUOTE (rewake @ Apr 14 2008, 10:12 AM) *
Hi PTW,

The query you posted seems as if it should work fine, but if it's not then there might be something else that's causing a problem. What data types are these two fields?

Maybe it's just a matter of setting the `thumb` data type to bool, or int(1), and setting the default value to 0.

Rich

well technically it does work fine. The problem is that the thumb field is a path to an image, so mysql sorts by the name of the image. Does that make sense? Unfortunately, adding a column is a more daunting task than it seems to be. I know it sounds weird but I'm working on a pretty horrible system for now. Is there a way to find out if it's null or not and sort that way?


--------------------
http://www.infoonmike.com

"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live." Martin Golding
Go to the top of the page
 
+Quote Post
rewake
post Apr 14 2008, 06:17 PM
Post #4


Rapid Squeezer
Group Icon

Posts: 239
Joined: 14-February 08
From: NY, USA


Ok gotcha! Try this...

CODE
SELECT `thumb` IS NULL AS `boolThumb`, `store_price` FROM `inventory` WHERE (`brand`='mybrand') ORDER BY `boolThumb` ASC, `store_price` DESC LIMIT 0, 20


That should do the trick. Let me know if it doesn't work.

Rich


--------------------
QUOTE
if ($name=='will') echo '/(bb|[^b]{2})/';

Raineri Jewelers | MySpace | Facebook | deviantART
Go to the top of the page
 
+Quote Post
paintingtheweb
post Apr 14 2008, 06:35 PM
Post #5


Squeezing
***

Posts: 76
Joined: 14-February 08
From: Las Vegas, NV


QUOTE (rewake @ Apr 14 2008, 03:17 PM) *
Ok gotcha! Try this...

CODE
SELECT `thumb` IS NULL AS `boolThumb`, `store_price` FROM `inventory` WHERE (`brand`='mybrand') ORDER BY `boolThumb` ASC, `store_price` DESC LIMIT 0, 20


That should do the trick. Let me know if it doesn't work.

Rich


It's not working because it's only selecting what is not null (I rearranged the query just little - changed to ISNULL(`thumb`) - I actually need to grab all things, whether or not thumb is null, but stick all of the ones where thumb is null at the bottom of the list...


--------------------
http://www.infoonmike.com

"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live." Martin Golding
Go to the top of the page
 
+Quote Post
paintingtheweb
post Apr 14 2008, 06:42 PM
Post #6


Squeezing
***

Posts: 76
Joined: 14-February 08
From: Las Vegas, NV


QUOTE (paintingtheweb @ Apr 14 2008, 04:35 PM) *
It's not working because it's only selecting what is not null (I rearranged the query just little - changed to ISNULL(`thumb`) - I actually need to grab all things, whether or not thumb is null, but stick all of the ones where thumb is null at the bottom of the list...


I got it working. problem was that the fields weren't NULL - so I just changed it to `thumb=' '` and it seemed to work ok. Thanks man - got me on the right track!


--------------------
http://www.infoonmike.com

"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live." Martin Golding
Go to the top of the page
 
+Quote Post
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   9 cosmicbdog 702 1st October 2008 - 10:47 PM
Last post by: cosmicbdog
No New Posts   1 djeyewater 45 18th December 2008 - 10:25 AM
Last post by: djeyewater