corsasport.co.uk
 

Corsa Sport » Message Board » Off Day » Geek Day » (My)SQL Gurus - Column Alias and Where Clause issue


New Topic

New Poll
  Subscribe | Add to Favourites

You are not logged in and may not post or reply to messages. Please log in or create a new account or mail us about fixing an existing one - register@corsasport.co.uk

There are also many more features available when you are logged in such as private messages, buddy list, location services, post search and more.


Author (My)SQL Gurus - Column Alias and Where Clause issue
Dom
Member

Registered: 13th Sep 03
User status: Offline
29th Mar 11 at 22:14   View User's Profile U2U Member Reply With Quote

Right, i have the below query -
code:

SELECT
`teacher_ref_cv`.`Teacher_ID`,
COUNT(`teacher_ref_cv`.`Teacher_ID`) AS `Num_of_Ref`
FROM `teacher_ref_cv`
WHERE `teacher_ref_cv`.`Type` = 'Reference'
GROUP BY `teacher_ref_cv`.`Teacher_ID`



which throws out (etc) -
code:

Teacher_ID----Num_of_Ref
___12____----____2____
___17____----____1____
___543___----____5____
___298___----____3____



Problem is i only want to pull out Teacher_ID's where Num_of_Ref is bigger equal-to 2 and you can't use column aliases in the WHERE clauses and you can't have HAVING and WHERE clauses together.

Has anyone got any ideas how i can achieve this?
I'm guessing subquery the COUNT? Only issue i have with this is that this query is already a subquery (it's getting extremely messy very quickly ) and there could be performance issues.

[Edited on 29-03-2011 by Dom]
gooner_47
Member

Registered: 20th Jul 04
Location: Bexhill/Croydon
User status: Offline
29th Mar 11 at 23:25   View User's Profile U2U Member Reply With Quote

Don't know if this will be any different to SQL Server, I presume not - but the following works for me:


code:
SELECT 'teacher_ref_cv'.'Teacher_ID'
, COUNT('teacher_ref_cv'.'Teacher_ID') AS 'Num_of_Ref'
FROM 'teacher_ref_cv'
WHERE 'teacher_ref_cv'.type = 'Reference'
GROUP BY 'teacher_ref_cv'.'Teacher_ID'
HAVING COUNT('teacher_ref_cv'.'Teacher_ID') > = 2


[Edited on 29-03-2011 by gooner_47]
Dom
Member

Registered: 13th Sep 03
User status: Offline
30th Mar 11 at 20:52   View User's Profile U2U Member Reply With Quote

Missed that one Cheers dude

 
New Topic

New Poll

  Related Threads Author Forum Replies Views Last Post
excel gurus paul.mitchell1984 Geek Day 2 392
13th Jun 07 at 12:52
by paul.mitchell1984
 
System Center Configuration Manager 2007 Pablo Geek Day 13 361
6th Feb 08 at 14:39
by Pablo
 
Php help? (mysql_num_rows) Whittie Geek Day 17 1520
15th Jan 10 at 14:38
by Dom
 
A website specifically for people to post projects? Gazdaman General Chat 81 1580
15th Feb 10 at 17:56
by Gazdaman
 
Database Gurus Bart Geek Day 8 558
15th Mar 11 at 18:35
by Bart
 

Corsa Sport » Message Board » Off Day » Geek Day » (My)SQL Gurus - Column Alias and Where Clause issue 28 database queries in 0.0141032 seconds