If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > select query help...

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-12-09, 11:13
acctman acctman is offline
Registered User
 
Join Date: Aug 2008
Posts: 4
select query help...

Hi i'm trying to do a query where it scans through all the i_user fields and groups the results of the i_status, and give me the results if an i_user does not have a i_status of 2. example below would return 010000 since i_status 2 is not present out of all the entries. the query has to look at the results as a group and not an individual entry.

tbl: rate_pictures

i_user i_status
010000 1
010000 1

i_user i_status
130000 1
130000 1
130000 1
130000 2

i_user i_status
150000 1
150000 1
150000 1
150000 2
Reply With Quote
  #2 (permalink)  
Old 12-12-09, 11:47
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
SELECT i_user 
  FROM rate_pictures
GROUP
    BY i_user
HAVING COUNT(CASE WHEN i_status = 2
                  THEN 'hiccough'
                  ELSE NULL END) = 0
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 12-12-09, 12:06
acctman acctman is offline
Registered User
 
Join Date: Aug 2008
Posts: 4
Quote:
Originally Posted by r937 View Post
Code:
SELECT i_user 
  FROM rate_pictures
GROUP
    BY i_user
HAVING COUNT(CASE WHEN i_status = 2
                  THEN 'hiccough'
                  ELSE NULL END) = 0
what does 'hiccough' never seen that used in a sql statement before
Reply With Quote
  #4 (permalink)  
Old 12-12-09, 13:23
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by acctman View Post
what does 'hiccough' never seen that used in a sql statement before
thank you, thank you very much, i am glad that you asked me because not everyone does, when i post this solution

you could substitute 'hiccup' if that helps

actually you could substitute any non-null value, and sometimes i write it like this --
Code:
HAVING COUNT(CASE WHEN i_status = 2
                  THEN 937 END) = 0
and of course the ELSE value is NULL by default

now all you have to do is take this knowledge of NULL versus not NULL, armed with the fact that aggregate functions do not include NULLs, and consider that the aggregate function in this instance is COUNT...

let me know if that makes sense
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On