Jump to content

[SOLVED] MySQL join help


rucia

Recommended Posts

Hi There,

 

I'm helping a friend who has two databases, each DB has a table of the same name.  So in DB1 there is a table T1 and in DB2 there is also a T1 table.  There is a RecID in each table.  Both tables record different information.  So a RecID might exist in both tables or exist in one but not the other.  With me so far?

 

Table 1 in Database 1 is recording Clicks and Table 1 in Database 2 is recording Reads.

 

I know its odd  :shrug: - but we need to get some stats for the time being until i can rearrange his tables into an actual sane order.

 

What i want to do is count the total Clicks and toatal reads in one query.  So i want to exclude nulls from the count on both columns and i don't seem to be able to do this - here is the query so far:

 

SELECT count(db1.click) AS TotClicks, count(db2.read) As TotReads
FROM all.fstr01 AS db1 INNER JOIN stats.fstr01 AS db2 ON db1.recid=db2.recid 
WHERE (db1.click>0 OR db2.read>0)

 

Any help much appreciated  :D

 

This will get the totals, but we'd also like to get unique counts as well - so have four columns with counts of total and unique clicks and total and unique reads.

 

Can this be done with one query?

 

Link to comment
Share on other sites

Thanks.  But that produces the same result it shows 86 total clicks and 3175 total reads.

 

Whereas i know the total clicks are only 35 and the total reads are 1583.

 

I understand that rows that do not have a match in the ON clause will have a null value in the result set.  but i want to count everything except for nulls on both columns.

 

Link to comment
Share on other sites

To preserve sanity i started out with two queries and thats where i got the figures from:

35 total clicks using

select count(recid) from all.fstr01 where click>0

 

and then 1583 total reads using

select count(recid) from stats.fstr01 where read>0

Link to comment
Share on other sites

Hi

 

Do you have a full list of recids on a seperate table? Do you want the count split down by recids?

 

This should give you the basic data, but would not make a good basis if you want the counts to be further split down (ie, with no field specified to join the 2 subselects together, if either bring bck multiple records then a massive number of records will be returned):-

 

SELECT a.RecIdCnt AS clickCnt, b.RecIdCnt as readCnt

FROM (SELECT COUNT(recid) AS RecIdCnt FROM all.fstr01 WHERE click>0 AND click IS NOT NULL) a,

(SELECT COUNT(recid) AS RecIdCnt FROM stats.fstr01 WHERE read>0 AND read IS NOT NULL) b

 

All the best

 

Keith

Link to comment
Share on other sites

Hi Keith, the query you provided was exactly what i was aiming for!  Thanks so much  :D.  I had a look at sub selects, but obvisouly didn't get as far as being able to do something like the query you provided.  Ta

 

thanks also to fenway and DEVILofDARKNESS for you time and assistance  :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.