Jump to content

MySQL Select Statement to pull Cetain Rows


Go to solution Solved by Barand,

Recommended Posts

Hi,

I'm trying to define a query that will only pull results for rows where only a specific action is defined for said column (Process) where the 'Action' is 'idle'. Note: if any process as other actions logged, other than 'idle' for any rows of data, then that process should NOT be included in the results at all - only those that do NOT have additional rows logged as 'started' or 'complete'.

For example: I need to define the MySQL query that will ONLY parse results that include id 5, 8, 12 being those only show 'Action' of 'idle'.
Being all the rest have multple 'Actions' of 'started' or 'complete' - ids: 1,2,3,4,6,7,9,10,11 - then they should be excluded in the 'SELECT' results.

==============================
Id              Process                   Action

1                Filter                         idle

2                Drain                         idle

3                Swell                         idle

4                Filter                       started

5                Stretch                      idle

6                Filter                     complete

7                Drain                       started

8                Spread                      idle

9                Swell                       started

10              Swell                     complete

11              Drain                     complete

12              Plant                         idle

==============================

 

I played around with the 'SELECT' statement (defining where 'action != started', etc...) but it pulls the row matching 'idle' - even for those having other actions logged.

 

Again: that's my problem - not sure how to define the logic to exclude those (I even tried subquery to pull all matching 'started' or 'complete' then insert in later query to 'NOT' match on those results but it takes forever to finish the query so know that's not correct. Note: each of the logs have a timestamp - I excluded that column but is apart of each record (just fyi...)

I would appreciate some guidance on figuring this out as I'm not sure if 'Case' the correct way to define this logic nor how to defined the query using such... - ensuring any 'process' linked to any other 'Action' other then 'idle' is excluded in the results. Thanks in advance!

  • Solution


SELECT n.id, n.process, n.action
FROM n1concepts n
LEFT JOIN
    (
    SELECT process
    FROM n1concepts
    WHERE `action` <> 'idle'
    ) as notidle USING (process)
WHERE n.action = 'idle' AND notidle.process IS NULL

 

Edited by Barand
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.