Jump to content

Merging more than one table


Yesideez

Recommended Posts

I've got a couple tables: users; onlinetime

 

The users table contains loads of info including userid, username, activitycount and the onlinetime contains id, userid, date, active, idle. The field userid is what ties both tables together and active and idle are both integers (seconds).

 

Where the users table has one entry per user (as is normal) the onlinetime table can have multiple entries per user.

 

What I need to do is make a query to list all the users who have activity count as 0 and the sum(active) in onlinetime below a certain number, let's say 100.

 

If the onlinetime had only one entry per user I'd be able to write the query but it's the multiple entries in the onlinetime table that's throwing me.

Link to comment
https://forums.phpfreaks.com/topic/220462-merging-more-than-one-table/
Share on other sites

I did try this:

SELECT u.userid,u.username,z.active,z.idle FROM users AS u
  LEFT JOIN onlinetime AS t ON u.userid=t.userid
  LEFT JOIN (SELECT userid,SUM(active) AS active,SUM(idle) AS idle FROM onlinetime) AS z ON u.userid=z.userid

Doesn't work :(

 

Tried this as well to no avail:

SELECT u.userid,u.username,z.active,z.idle FROM users AS u
  LEFT JOIN (SELECT userid,SUM(active) AS active,SUM(idle) AS idle FROM onlinetime) AS z ON u.userid=z.userid ORDER BY z.active DESC LIMIT 20

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.