Skip to main content
Inspiring
December 10, 2008
Answered

my query or i might be wrong?

  • December 10, 2008
  • 1 reply
  • 1807 views
I have one MYSQL query Which I think is right but not know it is right or not:

it does not produce the required output as i have records in the database:

here is the query:

<cfquery datasource="#request.dsn#" name="newclass" username="#request.user#" password="#request.pass#">
SELECT grcat.cid,grcat.title, count(listid) as totallist
FROM grcat
LEFT JOIN grlist ON grcat.cid = grlist.cid
WHERE
parentid = <cfqueryparam cfsqltype="cf_sql_numeric" value="#url.cid#">
AND
active = <cfqueryparam cfsqltype="cf_sql_tinyint" value="1">
GROUP BY
grcat.cid
</cfquery>
<cfdump var="#newclass#">
This topic has been closed for replies.
Correct answer newchickinCF
it is just a precaution against having NULL as totallist value.
but with MySQL as your db you do not really need it since COUNT(expr) in
MySQL will count only non-null values, and return 0 if there are no
matching rows (i.e. if they are all NULL)

as for your problem:

1) what is the value of #url.cid# when you run your original query?

2) are there any records in your grcat table that actually have the
field parentid with the value of url.cid variable? run this query to
check that you actually have the data in your table you think you have
in there:

SELECT grcat.cid, grcat.parentid, grcat.title, COUNT(grlist.listid) AS
totallist
FROM grcat LEFT JOIN grlist ON grcat.cid = grlist.cid
WHERE grlist.active = <cfqueryparam cfsqltype="cf_sql_tinyint" value="1">
GROUP BY
grcat.cid


3) see if this query does what you want it to do:

SELECT grcat.cid, grcat.title, COUNT(grlist.listid) AS totallist
FROM grcat LEFT JOIN grlist ON grcat.cid = grlist.cid AND grlist.active
= <cfqueryparam cfsqltype="cf_sql_tinyint" value="1">
WHERE grcat.parentid = <cfqueryparam cfsqltype="cf_sql_integer"
value="#url.cid#">
GROUP BY
grcat.cid

4) are you sure it is the parentid field you need to compare to #url.cid# ?

5) if nothing above worked, see if this query does:

SELECT grcat.cid, grcat.title, sq_grlist.totallist
FROM grcat
LEFT JOIN
(SELECT grlist.cid, COUNT(grlist.listid) AS totallist FROM grlist WHERE
grlist.active = <cfqueryparam cfsqltype="cf_sql_tinyint" value="1">
GROUP BY grlist.cid) sq_grlist ON grcat.cid = sq_grlist.cid
WHERE grcat.parentid = <cfqueryparam cfsqltype="cf_sql_integer"
value="#url.cid#">
GROUP BY
grcat.cid



Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/

quote:

Originally posted by: Newsgroup User
it is just a precaution against having NULL as totallist value.
but with MySQL as your db you do not really need it since COUNT(expr) in
MySQL will count only non-null values, and return 0 if there are no
matching rows (i.e. if they are all NULL)

as for your problem:

1) what is the value of #url.cid# when you run your original query?

2) are there any records in your grcat table that actually have the
field parentid with the value of url.cid variable? run this query to
check that you actually have the data in your table you think you have
in there:

SELECT grcat.cid, grcat.parentid, grcat.title, COUNT(grlist.listid) AS
totallist
FROM grcat LEFT JOIN grlist ON grcat.cid = grlist.cid
WHERE grlist.active = <cfqueryparam cfsqltype="cf_sql_tinyint" value="1">
GROUP BY
grcat.cid


3) see if this query does what you want it to do:

SELECT grcat.cid, grcat.title, COUNT(grlist.listid) AS totallist
FROM grcat LEFT JOIN grlist ON grcat.cid = grlist.cid AND grlist.active
= <cfqueryparam cfsqltype="cf_sql_tinyint" value="1">
WHERE grcat.parentid = <cfqueryparam cfsqltype="cf_sql_integer"
value="#url.cid#">
GROUP BY
grcat.cid

4) are you sure it is the parentid field you need to compare to #url.cid# ?

5) if nothing above worked, see if this query does:

SELECT grcat.cid, grcat.title, sq_grlist.totallist
FROM grcat
LEFT JOIN
(SELECT grlist.cid, COUNT(grlist.listid) AS totallist FROM grlist WHERE
grlist.active = <cfqueryparam cfsqltype="cf_sql_tinyint" value="1">
GROUP BY grlist.cid) sq_grlist ON grcat.cid = sq_grlist.cid
WHERE grcat.parentid = <cfqueryparam cfsqltype="cf_sql_integer"
value="#url.cid#">
GROUP BY
grcat.cid



Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/



All of the the qUERY

SELECT grcat.cid, grcat.title, COUNT(grlist.listid) AS totallist
FROM grcat LEFT JOIN grlist ON grcat.cid = grlist.cid AND grlist.active
= <cfqueryparam cfsqltype="cf_sql_tinyint" value="1">
WHERE grcat.parentid = <cfqueryparam cfsqltype="cf_sql_integer"
value="#url.cid#">
GROUP BY
grcat.cid


This Woked. Thanks U All Guys

Hats Off

1 reply

Inspiring
December 10, 2008
I assume you are getting 0 rows returned. When that happens to me, I start by commenting out as much of the query as possible and running it. I then un-comment one thing at a time until I get 0 records. Then I know which part of the query cause the problem.
Inspiring
December 10, 2008
Yes!! I am getting 0 Results..
I did according to your written query.

When i try to use the parentid it does not return result...

I hardcoded the parentid = 2 which has 18 records associated to the grcat table but i do not know what going wrong..

My table structure of grcat is like this:

Field Type Collation Attributes Null Default Extra Action
cid int(11) No auto_increment
parentid int(11) No 0
img varchar(30) latin1_swedish_ci No
title varchar(40) latin1_swedish_ci No
cdesc text latin1_swedish_ci No
hits int(11) No 0