Database Management
 
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 ForumsDatabasesDatabase Management

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 December 3rd, 2011, 06:34 AM
YoungL YoungL is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2011
Posts: 11 YoungL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 15 m 42 sec
Reputation Power: 0
Stuck on an SQL Query

Code:
SELECT ibf_arcade_cats.cat_name, (

SELECT count( * )
FROM ibf_arcade_games
WHERE ibf_arcade_cats.c_id = ibf_arcade_games.gcat
) AS cat_total
FROM ibf_arcade_cats


The above query works, it returns the total number of games which have the category ID in the cats table for each Cat ID.

Now I want to compare "cat_total" with the already set total in the table ibf_arcade_cats... this is what I have tried:

Code:
SELECT ibf_arcade_cats.cat_name, (

SELECT count( * )
FROM ibf_arcade_games
WHERE ibf_arcade_cats.c_id = ibf_arcade_games.gcat
) AS cat_total
FROM ibf_arcade_cats WHERE  num_of_games <> cat_total


but it tells me that cat_total is an unknown column... is this query possible to do, I just want to get a report of those rows where the total is not currently up to date.

Any help greatly appreciated.

Reply With Quote
  #2  
Old December 3rd, 2011, 07:35 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 43rd Plane (26000 - 26499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,439 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 13 h 57 m 19 sec
Reputation Power: 4141
cat_total is a column alias, and you cannot use a column alias in the WHERE clause of the same query

however, what you can do is push your entire query (marked in red below) down one level into a subquery, and then you can use the alias name in the outer query
Code:
SELECT t.*
  FROM ( SELECT ibf_arcade_cats.cat_name
              , ( SELECT COUNT(*)
                    FROM ibf_arcade_games
                   WHERE ibf_arcade_games.gcat = ibf_arcade_cats.c_id
                ) AS cat_total
           FROM ibf_arcade_cats  ) AS t
 WHERE t.num_of_games <> t.cat_total
when you try this, you will discover yet another problem
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old December 3rd, 2011, 10:04 AM
YoungL YoungL is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2011
Posts: 11 YoungL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 15 m 42 sec
Reputation Power: 0
Excellent,

Got it working, that had been bugging me for a good hour!

Final query was:

Code:
SELECT t . *
FROM (

	SELECT ibf_arcade_cats.cat_name, ibf_arcade_cats.num_of_games, (

		SELECT COUNT( * )
		FROM ibf_arcade_games
		WHERE ibf_arcade_games.gcat = ibf_arcade_cats.c_id
	) AS cat_total
	FROM ibf_arcade_cats
) AS t
WHERE t.cat_total <> t.num_of_games


And it worked a treat!

Reply With Quote
  #4  
Old December 3rd, 2011, 12:31 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 43rd Plane (26000 - 26499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,439 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 13 h 57 m 19 sec
Reputation Power: 4141
excellent, and congrats for fixing that other error as well


Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Stuck on an SQL Query

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