Solved

Count not working

Posted on 2008-06-16
8
371 Views
Last Modified: 2013-12-13
Dear Experts,

select count(*) as numrows
from datatable dt
inner join categorytable ct on ct.mainid = dt.mainid
where ct.categoryid = 10
order by dt.date desc

This gives me 214 rows when I should be getting 142 rows

So I tried adding a group

select count(*) as numrows
from datatable dt
inner join categorytable ct on ct.mainid = dt.mainid
where ct.categoryid = 10
group by dt.mainid
order by dt.date desc

but then it goes completely wrong.

Can someone please help as I need to get the count I get from this section

select count(*) as numrows
from datatable dt
order by dt.date desc

But I need to specify the categoryid which only exists in the categorytable.

please help.

I am using mysql 3.25 I think.
0
Comment
Question by:narmi2
8 Comments
 
LVL 29

Assisted Solution

by:fibo
fibo earned 100 total points
ID: 21793829
select unique count(dt.*) as numrows
from datatable dt
inner join categorytable ct on ct.mainid = dt.mainid
where ct.categoryid = 10
order by dt.date desc
?? why this "order"?

select unique count( dt.mainid) as numrows
from datatable dt
inner join categorytable ct on ct.mainid = dt.mainid
where ct.categoryid = 10
order by dt.date desc

0
 
LVL 3

Expert Comment

by:pzurowski
ID: 21793905
how does look rows which they shoudln't be? Is there any NULLs in ct.mainid or dt.mainid ?
select * as numrows

from datatable dt

inner join categorytable ct on ct.mainid = dt.mainid

where ct.categoryid = 10

order by dt.date desc

Open in new window

0
 
LVL 1

Author Comment

by:narmi2
ID: 21793907
You're right I don't need the order.  However the above script is not working.  I am getting a query error.  How do I check what the error is?
0
 
LVL 29

Expert Comment

by:fibo
ID: 21794009
select unique count( dt.mainid) as numrows
from datatable dt, categorytable ct
where (ct.categoryid = 10) AND (ct.mainid = dt.mainid)
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Author Comment

by:narmi2
ID: 21794010
pzurowski,

No nulls, but there are repeating rows because each main id in datatable has many categoryids in categorytable
0
 
LVL 1

Author Comment

by:narmi2
ID: 21794026
That query does not work fibo,  I think mysql 3.23 does not support the "unique".
0
 
LVL 31

Accepted Solution

by:
awking00 earned 400 total points
ID: 21794320
Use distinct keyword instead of unique.
0
 
LVL 2

Expert Comment

by:raheel_lips
ID: 21795125
Hey narmi2,

Try following query. I think this will get rid of your problem. I think your query need left join instead of inner join

Regards,
Raheel

select count(*) as numrows

from datatable dt

left join categorytable ct on ct.mainid = dt.mainid

where ct.categoryid = 10

group by dt.mainid

Open in new window

0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now