Jump to content

Do I Need A Join?


joeygallo

Recommended Posts

I have a table that looks like this:

 

TABLE1:

id, username, timestamp

1, test1, 123123123

2, test2, 123123124

3, test3, 123123125

4, test4, 123123126

1, test1, 123123127

1, test1, 123123128

3, test3, 123123129

 

There are thousands of entries in that table.  I'm producing a top 10 list of entries based on row count and date range using this:

 

select id, COUNT(id) AS total_count from TABLE1 where timestamp >= '$startTime' and timestamp <= '$endTime' GROUP BY id ORDER BY total_count DESC LIMIT 10

 

startTime and endTime is defined and the above query works exactly how I want it to.  Now I want to add another table into the mix.

 

TABLE2:

id, username, timestamp

1, test1, 123123123

2, test2, 123123124

3, test3, 123123125

4, test4, 123123126

 

How can I include TABLE2 into the query so the totals come from both tables and are combined?

Link to comment
Share on other sites

select id, COUNT(id) AS total_count from ((SELECT * FROM TABLE1 where timestamp >= '$startTime' and timestamp <= '$endTime') UNION (SELECT * FROM TABLE2 where timestamp >= '$startTime' and timestamp <= '$endTime')) GROUP BY id ORDER BY total_count DESC LIMIT 10

Link to comment
Share on other sites

I replaced this:

from TABLE1 where timestamp >= '$startTime' and timestamp <= '$endTime'

 

With this as suggested:

from ((SELECT * FROM TABLE1 where timestamp >= '$startTime' and timestamp <= '$endTime') UNION (SELECT * FROM TABLE2 where timestamp >= '$startTime' and timestamp <= '$endTime'))

 

The output is this:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given

 

The IDs are just user IDs.  They are not indexed or auto-incremented.. just a numerical value.

 

Both TABLE1 and TABLE2 have the timestamp and id fields in them.  It's basically a matching table with a different name.

Link to comment
Share on other sites

The UNION is not working.  The tables don't have the same number of columns and I can't change that as one table has additional data than the other.

 

EDIT: They do both have the ID and TIMESTAMP fields as mentioned before and they are both setup the same as far as the field types.  Any suggestions on why this doesn't work?

Link to comment
Share on other sites

Trying to figure this out on my own I read this about UNIONS:

The number of columns in each SELECT statement has to be the same.

 

TABLE1 has 33 columns, TABLE2 has only 30 columns.

 

I will continue working on this.

 

Don't use SELECT * in the UNION queries. Just select the common columns that you are working with

 

from ((SELECT id, username, timestamp FROM TABLE1 where timestamp >= '$startTime' and timestamp <= '$endTime') 
      UNION (SELECT id, username, timestamp FROM TABLE2 where timestamp >= '$startTime' and timestamp <= '$endTime'))

Link to comment
Share on other sites

Don't use SELECT * in the UNION queries. Just select the common columns that you are working with

 

from ((SELECT id, username, timestamp FROM TABLE1 where timestamp >= '$startTime' and timestamp <= '$endTime') 
      UNION (SELECT id, username, timestamp FROM TABLE2 where timestamp >= '$startTime' and timestamp <= '$endTime'))

 

This works:

SELECT id, COUNT(id) AS total_count FROM TABLE1 where timestamp >= '$startTime' and timestamp <= '$endTime' GROUP BY id ORDER BY total_count DESC LIMIT 10

 

And this works:

SELECT id, COUNT(id) AS total_count FROM TABLE2 where timestamp >= '$startTime' and timestamp <= '$endTime' GROUP BY id ORDER BY total_count DESC LIMIT 10

 

SELECT id, COUNT(id) AS total_count FROM ((SELECT id FROM TABLE1 where timestamp >= '$startTime' and timestamp <= '$endTime') UNION (SELECT id FROM TABLE2 where timestamp >= '$startTime' and timestamp <= '$endTime')) GROUP BY id ORDER BY total_count DESC LIMIT 10

 

So independently they are working but together in the JOIN they are not.  I tried select id, timestamp in both queries instead of just id.  That didn't make a difference.  I'm just not seeing what the problem is here.

Link to comment
Share on other sites

SELECT id FROM ((SELECT id FROM TABLE1) UNION (SELECT id FROM TABLE2)) AS Foo

 

I tried that.  No more error but the output is not correct.  Here's the current code.

 

$get_totals = mysql_query("SELECT id, COUNT(id) AS total_count FROM ((SELECT id FROM TABLE1 where timestamp >= '$startTime' and timestamp <= '$endTime') UNION (SELECT id FROM TABLE2 where timestamp >= '$startTime' and timestamp <= '$endTime')) as x GROUP BY id ORDER BY total_count DESC LIMIT 10");
if (mysql_num_rows($get_totals)) {
while ($qry = mysql_fetch_array($get_totals)) {

// display info here.
// $qry['total_count'] is used to show number of entries.
} }

 

In the above output, the accounts are not ordered correctly and the value of the qry['total_count'] variable simply reads 1 for each entry found.

Link to comment
Share on other sites

You're only selecting the ID from the inner tables.  Perhaps you're not getting the right number.  Try this:

 

 

$get_totals = mysql_query("SELECT id, COUNT(timestamp) AS total_count FROM ((SELECT id, timestamp FROM TABLE1 where timestamp >= '$startTime' and timestamp <= '$endTime') UNION (SELECT id, timestamp FROM TABLE2 where timestamp >= '$startTime' and timestamp <= '$endTime')) as x GROUP BY id ORDER BY total_count DESC LIMIT 10");

 

If that doesn't work, do the inner query only (table X) and see if there are any duplicates.  Maybe there just aren't.

 

Link to comment
Share on other sites

I think I'm getting somewhere.  I run this direct on the database (just basic queries with no extra stuff added):

SELECT id FROM ((SELECT id FROM TABLE1) UNION (SELECT id FROM TABLE2))

 

I get this:

Every derived table must have its own alias

 

Also, why the extra select at all?

Link to comment
Share on other sites

@fenway, I was unioning the two tables in the inner select so that the counts were accurate

 

@barand:  That's exactly right, I completely forgot about union all.  That's the issue.  You can fix it either with UNION ALL or by including the timestamp in the inner query.

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.