Solved

Count not working

Posted on 2008-06-16
8
387 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 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
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

615 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