dbTalk Databases Forums  

Can a query return both a count and ensure that a particular valuedoes not exist within the counted records?

comp.databases.mysql comp.databases.mysql


Discuss Can a query return both a count and ensure that a particular valuedoes not exist within the counted records? in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
keytostars
 
Posts: n/a

Default Can a query return both a count and ensure that a particular valuedoes not exist within the counted records? - 12-31-2010 , 01:31 AM






I have a table with fields id, parentid, userid, and rowtype. I have
joined it with itself where one parent row has a one to many
relationship with children rows. I need to create a query which will
return the number of children rows, but only if a particular userid is
not contained in the counted rows and the count is less than 6. I've
got it returning a result that will give me the count... but how do I
make sure that a particular value is not in the userid in any of those
counted fields (without doing another query)?

SELECT COUNT (table1.parentid) AS count_children LEFT OUTER JOIN table
table1 ON (table.id=table1.parentid) WHERE table.rowtype = 1 AND
table1.rowtype = 2 HAVING COUNT(table1.parentid) <=6

Reply With Quote
  #2  
Old   
Luuk
 
Posts: n/a

Default Re: Can a query return both a count and ensure that a particularvalue does not exist within the counted records? - 12-31-2010 , 03:22 AM






On 31-12-10 08:31, keytostars wrote:
Quote:
I have a table with fields id, parentid, userid, and rowtype. I have
joined it with itself where one parent row has a one to many
relationship with children rows. I need to create a query which will
return the number of children rows, but only if a particular userid is
not contained in the counted rows and the count is less than 6. I've
got it returning a result that will give me the count... but how do I
make sure that a particular value is not in the userid in any of those
counted fields (without doing another query)?
add something to the WHERE-clause

Quote:
SELECT COUNT (table1.parentid) AS count_children LEFT OUTER JOIN table
table1 ON (table.id=table1.parentid) WHERE table.rowtype = 1 AND
table1.rowtype = 2 HAVING COUNT(table1.parentid)<=6
AND userid<>666


--
Luuk

Reply With Quote
  #3  
Old   
Axel Schwenke
 
Posts: n/a

Default Re: Can a query return both a count and ensure that a particular value does not exist within the counted records? - 12-31-2010 , 05:14 AM



keytostars <starkeypictures (AT) gmail (DOT) com> wrote:

Quote:
I have a table with fields id, parentid, userid, and rowtype. I have
joined it with itself where one parent row has a one to many
relationship with children rows. I need to create a query which will
return the number of children rows, but only if a particular userid is
not contained in the counted rows and the count is less than 6. I've
got it returning a result that will give me the count... but how do I
make sure that a particular value is not in the userid in any of those
counted fields (without doing another query)?

SELECT COUNT (table1.parentid) AS count_children LEFT OUTER JOIN table
table1 ON (table.id=table1.parentid) WHERE table.rowtype = 1 AND
table1.rowtype = 2 HAVING COUNT(table1.parentid) <=6
SELECT COUNT(*) AS count_children,
GROUP_CONCAT(
IF(user_id = $forbidden, '1', '0')
SEPARATOR ''
) AS bitmask
FROM $table
WHERE parent_id = $parent
GROUP BY parent_id
HAVING count_children <= 6
AND bitmask NOT LIKE '%1%'


XL

Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.