473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Double count problem. Who can help?



Here's a little question to MySQL Gurus.

Two tables. One called 'document' one called 'page'. Needles to say, a
document consist out of multiple pages (1:n). The primary (P) and
foreign (F) keys are:

*** document
document_id (P)
*** page
page_id (P)
document_id (F)
Now the task:

How many documents have more than ten pages?
And no subselects please.
I can do the following to filter out all documents with ten or less
pages:

SELECT document.document_id, COUNT(page_id) as pages FROM document INNER
JOIN page ON page.document_id=document.document_id WHERE pages>10 GROUP
BY page_id
But how can I now run a count over the whole thing?

--
TGOS
Jul 20 '05 #1
2 2225
"TGOS" <tg**@invalid.invalid> wrote in message
SELECT document.document_id, COUNT(page_id) as pages FROM document INNER
JOIN page ON page.document_id=document.document_id WHERE pages>10 GROUP
BY page_id

But how can I now run a count over the whole thing?


In MySql 4.1 it is just

select count(*) from
(select ...) as whatever_name
In most versions of MySql you can also do

select @var:=0;

select document.id, count(page.id) as pages, @var:=@var+1
from
document
inner join page on document.id = page.document_id
group by document.id
having pages > 1

and the 3rd column @var:= holds the count.

In any version of MySql, you can also always create a new table to store the
results of the sub-query, and populate it with an insert ... select
statement. Internally, this is how MySql would handle some sub-queries. To
create a temporary table visible to only one connection, do the following:

create temporary table_name ...
Jul 20 '05 #2
TGOS <tg**@invalid.invalid> wrote in message
*** page
page_id (P)
document_id (F)
Is there a column for page number? If so, then you can just join
document to page where the page number is 11, and this means the
document has more than 10 pages.
Now the task:

How many documents have more than ten pages?
And no subselects please.


select count(*) from
document D
inner join page P1 on D.id = P1.document_id
inner join page P2 on D.id = P2.document_id and (P1.id <> P2.id)
inner join page P3 on D.id = P3.document_id and (P1.id <> P2.id and
P1.id <> P3.id)
....
Maybe the older versions of MySql support IN statements for fixed
constants (rather than a select statement) so you could try

select count(*) from
document D
inner join page P1 on D.id = P1.document_id
inner join page P2 on D.id = P2.document_id and P2.id not in (P1.id)
inner join page P3 on D.id = P3.document_id and P3.id not in (P1.id,
P2.id)
....

To get the documents having fewer than 5 pages you can left outer join
and a query on P5.id is NULL.
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ai lian | last post by:
When I use printf to print a large double number, the result is not the same as the original input number. For example: double num=899999999999.894400; printf("%lf\n",num); The output is:...
6
by: ebc | last post by:
Hi, I have written a function that removes double entries from a sorted array. See the structures typedef struct tagRECR { char name;
2
by: Alpha | last post by:
I have a window application. In one of the form, a datagrid has a dataview as its datasource. Initial filtering result would give the datavew 3 items. When I double click on the datagrid to edit...
0
by: Malte | last post by:
Hello there, We got a problem atm with our 20k database. Everytime we search for sth. with double quotes it takes much more longer than without. Here is what we did: mysql> SELECT COUNT(*)...
6
by: oskar | last post by:
Hello everyone. I have a problem with my program... and i kinda dunno what to do.. everything seems to work ok, but i'm getting corrupted double-linked list error =\. *** glibc detected ***...
6
by: Jay | last post by:
I need to convert from a string a double that is followed by a scaling character (k means *1e3, M=*1e6, etc) then apply the scaling character. Example: "-1.345k #comment" I know roughly how...
2
by: cendter | last post by:
Hi there, I am utterly confused - I have a form that starts an instance from excel and let's the user select a range. I then want to take this range as an array of doubles. I ran into trouble...
2
dlite922
by: dlite922 | last post by:
Hey guys, this is so weird, i don't know what else to do. Hopefully one of you is a Smarty guru as this is difficult to search (google and smarty forum) and if you aren't i'll post answer here, i'm...
3
by: kingparthi | last post by:
This is a program to add, display & delete an item from the double linked list ... Here the add & display works correctly... where my delete is having some problem, when ever I delete an item, it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.