Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6880003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:55:08+00:00 2026-05-27T04:55:08+00:00

I have 2 table (pcgroup and client pc). Let say the date today is

  • 0

I have 2 table (pcgroup and client pc). Let say the date today is 24/11, I need to find which PC is online and which pc is not online for the last 2 days using only mySQL.

INSERT INTO pcgroup(id, groupName) 
  VALUES(1, 'defaultGroup');
INSERT INTO clientpc(id, pcGroupId, clientPcName, lastOnlineTime) 
  VALUES(1, 1, 'pc1', '2011-11-24');
INSERT INTO clientpc(id, pcGroupId, clientPcName, lastOnlineTime) 
  VALUES(2, 1, 'pc2', '2011-11-24');
INSERT INTO clientpc(id, pcGroupId, clientPcName, lastOnlineTime) 
  VALUES(3, 1, 'pc3', '2011-11-20');
INSERT INTO clientpc(id, pcGroupId, clientPcName, lastOnlineTime) 
  VALUES(4, 1, 'pc4', '2011-11-20');
INSERT INTO clientpc(id, pcGroupId, clientPcName, lastOnlineTime) 
  VALUES(5, 1, 'pc5', '2011-11-20');

Here is my query for now

SELECT DISTINCT
  pcgroup.id AS pcGroupID, pcgroup.groupName,
  online.onlinePC, offline.offlinePC
FROM
  (
    SELECT   
      pcgroup.Id, pcGroup.groupName
    FROM
      pcgroup
    WHERE
      (pcgroup.Id = 1)
  ) pcgroup
LEFT  JOIN
  (
    SELECT
      clientpc.Id, clientpc.pcGroupId, clientpc.clientPcName AS onlinePC
    FROM
      clientpc
    WHERE
      DateDiff(CURDATE(),clientpc.lastOnlineTime) <= 2
    AND
      DateDiff(CURDATE(),clientpc.lastOnlineTime) IS NOT NULL
  ) online  
 ON 
  pcgroup.Id = online.pcGroupId
 LEFT JOIN
   (
     SELECT
       clientpc.Id, clientpc.pcGroupId, clientpc.clientPcName AS offlinePC
     FROM
       clientpc
     WHERE
       (DateDiff(CURDATE(),clientpc.lastOnlineTime) > 2
     OR
        DateDiff(CURDATE(),clientpc.lastOnlineTime) IS NULL)
   ) offline 
  ON pcgroup.Id = offline.pcGroupId

This is the result I get

*pcGroupID    groupName        onlinePC       offlinePC*
  1           defaultGroup      pc1             pc3
  1           defaultGroup      pc1             pc4
  1           defaultGroup      pc1             pc5
  1           defaultGroup      pc2             pc3
  1           defaultGroup      pc2             pc4
  1           defaultGroup      pc2             pc5

However, what I need is something like this

*pcGroupID    groupName        onlinePC       offlinePC*
  1           defaultGroup      pc1             pc3
  1           defaultGroup      pc2             pc4
  1           defaultGroup                      pc5

So my question is, is that achievable? and if yes, how. Have been working on this query for 2 days. So i really appreciate if you guys can help me out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:55:08+00:00Added an answer on May 27, 2026 at 4:55 am

    Why not use 2 simple queries and do the presentation in your application?:

     --- Online ---
     SELECT
         c.pcGroupID, p.groupName, c.clientPcName, 'Online' AS pcStatus
     FROM
         pcgroup AS g
       JOIN
         clientpc AS c
           ON g.Id = c.pcGroupId
     WHERE
         g.Id = @pcGroupIdToBeChecked
       AND
         c.lastOnlineTime >= CURDATE() - INTERVAL 2 DAY
    
     --- Offline ---
     SELECT
         c.pcGroupID, p.groupName, c.clientPcName, 'Offline' AS pcStatus
     FROM
         pcgroup AS g
       JOIN
         clientpc AS c
           ON g.Id = c.pcGroupId
     WHERE
         g.Id = @pcGroupIdToBeChecked
       AND
         ( c.lastOnlineTime < CURDATE() - INTERVAL 2 DAY
          OR
           c.lastOnlineTime IS NULL
         )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table A in Oracle that has a primary key (id). I need
I have table which has a class data and it has table rows inside
I have table which will only contain information about files, so, as the file
I have table with two SelectOneMenu in row. I need populate data in second
I have table named Table X which contains some names and their corresponding age,
I have table that has date field. When I run a query, I see
I have table A(id). I need to create table B(id) add a foreign key
I have table view which is showing list of files to download. When I
I have table ,and i need access them from shell script and do the
I have table named A with column B defined int not null Primary Key

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.