MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 13th, 2012, 05:57 PM
JugglingReferee JugglingReferee is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2001
Posts: 52 JugglingReferee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 h 23 m 12 sec
Reputation Power: 13
Use of UNION invalidates SELECT results

Related to my previous thread: http://forums.devshed.com/mysql-help-4/having-or-group-by-or-something-else-929995.html


So I did use the UNION keyword to merge together 3 SELECT statements.

(SELECT)

UNION

(SELECT)

UNION

(SELECT)

Initially, the correct and expected output was generated.

However, just the other day, the ranked-with-ties results from the first SELECT statement is incorrect.

I'm 99.99% sure it's not a bug in the SELECT statement. Which leads me to believe that it's a bug in the engine that is returning the results.

The entire command is:

Code:
(SELECT
    IF(@LastScore9 <> Score, @CurrentRank9 := @CurrentRank9 + @NextRank9, @CurrentRank9) AS Rank, Player, Games, Points, Won, Spread, PointsFor,
  IF(@LastScore9 = Score, @NextRank9 := @NextRank9 + 1, @NextRank9 := 1) as AddToGetNextRank9,
    @LastScore9 := Score AS Score
FROM Results
JOIN (SELECT @CurrentRank9 := 1, @LastScore9 := 0, @NextRank9 := 0) r
WHERE Date='2012-09-11 19:00:00' AND Games=9
ORDER BY Score DESC)

UNION

(SELECT
    IF(@LastScore8 <> Score, @CurrentRank8 := @CurrentRank8 + @NextRank8, @CurrentRank8) AS Rank, Player, Games, Points, Won, Spread, PointsFor,
    IF(@LastScore8 = Score, @NextRank8 := @NextRank8 + 1, @NextRank8 := 1) as AddToGetNextRank8,
    @LastScore8 := Score AS Score
FROM Results
JOIN (SELECT @CurrentRank8 := 1, @LastScore8 := 0, @NextRank8 := 0) r
WHERE Date='2012-09-11 19:00:00' AND Games=8
ORDER BY Score DESC)

UNION

(SELECT
    IF(@LastScore7 <> Score, @CurrentRank7 := @CurrentRank7 + @NextRank7, @CurrentRank7) AS Rank, Player, Games, Points, Won, Spread, PointsFor,
    IF(@LastScore7 = Score, @NextRank7 := @NextRank7 + 1, @NextRank7 := 1) as AddToGetNextRank7,
    @LastScore7 := Score AS Score
FROM Results
JOIN (SELECT @CurrentRank7 := 1, @LastScore7 := 0, @NextRank7 := 0) r
WHERE Date='2012-09-11 19:00:00' AND Games=7
ORDER BY Score DESC)


The output is here: http://www.halscrib.com/index.php/web-club/results?Tournament=2012-09-11+19%3A00%3A00

You can see that for the case of the people playing 9 games, there is SHEPHERD and FRANWARD. FRANWARD has 6 points, so she should be ranked higher than SHEPHERD. This is evident by her Score being higher.

But it ranks SHEPHERD ahead of FRANWARD.

If I run the first SELECT statement in phpMyAdmin by itself, it returns the correct ranking information.

Any ideas?

Reply With Quote
  #2  
Old September 13th, 2012, 10:21 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,375 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 30 m 16 sec
Reputation Power: 4140
i gotta be honest wif you... TL;DR
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old September 14th, 2012, 04:30 AM
JugglingReferee JugglingReferee is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2001
Posts: 52 JugglingReferee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 h 23 m 12 sec
Reputation Power: 13
Quote:
Originally Posted by r937
i gotta be honest wif you... TL;DR


Cool story bro.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Use of UNION invalidates SELECT results

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap