2

I have a table similar to this

id    message    state        timestamp
1     abcd       Heartbeat    1970-01-01 01:00
2     efgh       Start        1970-01-01 02:00
3     sdcvsd     Stop         1970-01-01 02:30
4     efgh       Start        1970-01-01 03:00
5     sdcvsd     Stop         1970-01-01 03:30
6     dsdfsd     Heartbeat    1970-01-01 04:10
7     sdcsc      Heartbeat    1970-01-01 04:20
8     sewwdf     Heartbeat    1970-01-01 04:30

What I would like to do is query this table in PHP and show the rows, but only show the latest row of Heartbeat, because it is very repetitive and I only need to know when the last one happened. All other states will echo even if they are repetitive. So the output would be (newest at the top):

8 sewwdf     Heartbeat    1970-01-01 04:30
5 sdcvsd     Stop         1970-01-01 03:30
4 efgh       Start        1970-01-01 03:00
3 sdcvsd     Stop         1970-01-01 02:30
2 efgh       Start        1970-01-01 02:00

I have only been able to leave it out with

mysqli_query($con, " SELECT * FROM msg WHERE state <> 'Heartbeat' ORDER BY id DESC LIMIT 16 " );

I don't think I can use GROUP BY or DISTINCT for more than one state.

How can I query only the latest Heartbeat state without effecting other states or remove the older ones in the PHP echo?

4
  • mysqli_query($con, " SELECT * FROM msg GROUP BY id ORDER BY id DESC LIMIT 16" ); try this Commented Aug 5, 2017 at 4:36
  • Also 1970-01-01 01:00 ? I think some problem is there? Commented Aug 5, 2017 at 4:42
  • I made the date/time up for the example. Commented Aug 5, 2017 at 4:43
  • SELECT * FROM msg GROUP BY(state) HAVING timestamp = MAX(timestamp) ORDER BY id DESC LIMIT 16 Commented Aug 5, 2017 at 5:13

5 Answers 5

2

This also works:

select * from
`msg`
where
`state` = 'Heartbeat'
and
`id` =  
(
select
`id`
from
`msg`
where
`state` = 'Heartbeat'
order by `timestamp` desc
limit 1
)

union

select * from
`msg`
where
`state` <> 'Heartbeat'

order by `timestamp` desc
Sign up to request clarification or add additional context in comments.

2 Comments

These id's are real, do u want to enumerate result rows ?
SELECT * FROM msg WHERE message = '$foo' AND state <> 'Heartbeat' UNION SELECT * FROM msg WHERE state = 'Heartbeat' AND id = (SELECT id FROM msg WHERE state = 'Heartbeat' ORDER BY id DESC LIMIT 1) ORDER BY id DESC LIMIT 16
0

Try this:

mysqli_query($con, "SELECT * FROM msg GROUP BY(state) ORDER BY id DESC LIMIT 16" );

2 Comments

This shows one row of every state. I need all states ungrouped, except 'Heartbeat' state, which I need the latest to be shown.
No, this groups each state. I do not want that. Please read the question carefully and see example.
0

enter image description here

Your query is:

mysqli_query($con, " SELECT * FROM msg WHERE state <> 'Heartbeat' ORDER BY id DESC LIMIT 16 " );

I think It should be :

mysqli_query($con, " SELECT * FROM msg WHERE state="Heartbeat" ORDER BY id DESC LIMIT 16 " );

You can see my attached picture, What happen in their? Thank You.

1 Comment

No, this only outputs the one state.
0

Try this Query :

SELECT * FROM `msg` GROUP BY `state` ORDER BY `id` DESC,`Timestamp` DESC LIMIT 16 

it will work

2 Comments

No, this groups each state. I do not want that.
so you just want state with heartbeat ?
0

you can try this also

select * from `test2` where `state` != 'Heartbeat' OR `id` = (select `id` from `test2` where `state` = 'Heartbeat' order by `date` desc limit 1 )

if need order with date

select * from `test2` where `state` != 'Heartbeat' OR `id` = (select `id` from `test2` where `state` = 'Heartbeat' order by `date` desc limit 1 ) ORDER BY date DESC

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.