Welcome to Dream.In.Code
Become an Expert!

Join 149,494 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,358 people online right now. Registration is fast and FREE... Join Now!




MySQL: Force SELECT to return a default value

 
Reply to this topicStart new topic

MySQL: Force SELECT to return a default value

girasquid
26 Mar, 2007 - 12:50 PM
Post #1

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,294



Thanked: 18 times
Dream Kudos: 725
My Contributions
Hello, all.

I have been expanding on some of my MySQL code, which generates user rankings based on the size of their armies. This is the code:
CODE

SELECT COUNT(*)+1 AS COUNT FROM user_armies GROUP BY commander HAVING (SUM(amount) > (SELECT SUM(amount) FROM user_armies WHERE commander = ?)) LIMIT 1

And it works fine and retrieves a user's rank, but if a user is at the very top of the rankings it doesn't return anything. Does anyone know of a way that I can force the query to return a 1 instead of no rows?

Thanks,
Girasquid
User is offlineProfile CardPM
+Quote Post

Trogdor
RE: MySQL: Force SELECT To Return A Default Value
27 Mar, 2007 - 03:59 AM
Post #2

D.I.C Addict
Group Icon

Joined: 6 Oct, 2006
Posts: 549



Thanked: 4 times
Dream Kudos: 125
My Contributions
For that you can use UNION.

<your query>
UNION
<alternative>

the alternative would be something like
SELECT 1 AS count
User is offlineProfile CardPM
+Quote Post

girasquid
RE: MySQL: Force SELECT To Return A Default Value
27 Mar, 2007 - 07:17 AM
Post #3

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,294



Thanked: 18 times
Dream Kudos: 725
My Contributions
That did the trick, thanks!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 05:54PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month