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
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

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.