473,787 Members | 2,989 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.docume nt_id, COUNT(page_id) as pages FROM document INNER
JOIN page ON page.document_i d=document.docu ment_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 2244
"TGOS" <tg**@invalid.i nvalid> wrote in message
SELECT document.docume nt_id, COUNT(page_id) as pages FROM document INNER
JOIN page ON page.document_i d=document.docu ment_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_i d
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.i nvalid> 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
21701
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: 899999999999.894409 Could anyone tell me what the problem is? How can I get the right output?
6
2962
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
3311
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 the selected lie item at which case I would pop up a separate dialog box to do so, in the debugging code, the dataview.count would return 0. I get a error message because I tried to get values out of a dataview that holds 0 items. Does anyone...
0
1540
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(*) FROM `Pages` WHERE MATCH (`Text`) AGAINST ('"term"' IN BOOLEAN MODE); +----------+ | COUNT(*) | +----------+
6
38226
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 *** corrupted double-linked list: 0x080aff40 *** Program received signal SIGABRT, Aborted. 0xaf7037b2 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2 (gdb) bt #0 0xaf7037b2 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2
6
5975
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 to do this in C (using the %n in the sscanf format specifier to find the number of characters converted) , but how do I do it in C#? My problem is how to count the number of characters of the double (chpos in the C example) so that the scaling...
2
5194
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 because the 1-based indexing that Excel passes (I think) so after many tries I got: Excel.Range aRange = xlApp.get_Range(textBox2.Text.Split(':'),textBox2.Text.Split(':') );
2
4493
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 sure one of you will run into it. I have an array of object, one of those objects has an attribute that is a string (description), This description does not appear in an INPUT text field ONLY, but appears to print inside div tags just fine. ...
3
3233
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 also deletes the tails.. kindly can someone help out... Here is my data types used in my program typedef struct {
0
9655
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.