Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Count not working

Posted on 2008-06-16
8
Medium Priority
?
390 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 29

Assisted Solution

by:fibo
fibo earned 400 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
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
 
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 32

Accepted Solution

by:
awking00 earned 1600 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
The viewer will learn how to count occurrences of each item in an array.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

718 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