Select field information with minimum time

Hey guys, quick question, I thought I was doing the right thing, but I still get the wrong result. I am trying to just find the record ID with the minimum time, but I am not getting this record.

$qryuserscount1="SELECT id,min(entry_time) FROM scrusersonline WHERE topic_id='$topic_id'";
$userscount1=mysql_query($qryuserscount1);
while ($row2 = mysql_fetch_assoc($userscount1)) {
    echo $onlineuser= $row2['id'];
}

This is my request and it does not work. This does however work, which does not make sense to me. SELECT id FROM scrusersonline WHERE topic_id = '$ topic_id' ORDER by entry_time LIMIT 1 , can someone quickly indicate what I'm doing wrong?

+3
source share
3 answers
SELECT id,min(entry_time) FROM scrusersonline WHERE topic_id='$topic_id'

id entry_time , topic_id, group-by, min(entry_time). SELECT , , any - , WHERE.

SELECT id FROM scrusersonline WHERE topic_id='$topic_id' ORDER BY entry_time LIMIT 1

id (SELECT id) (LIMIT 1) topic_id (WHERE topic_id='$topic_id') entry_time ( ).

, , SELECT id FROM scrusersonline WHERE topic_id='$topic_id' ORDER BY entry_time LIMIT 1 - , .

+2

, min group by. , , , , topic_id - , , entry_time ( ASC DESC, imo), .

?: -)

+1
SELECT  id, MIN(entry_time)
FROM    scrusersonline
WHERE   topic_id='$topic_id'

SQL .

id SELECT, GROUP BY, , id .

MySQL, , id ( , GROUP BY).

PRIMARY KEY GROUP BY:

SELECT  mytable.*, COUNT(*)
FROM    mytable
JOIN    othertable
ON      othertable.col1 = mytable.id
GROUP BY
        mytable.id

mytable , , , .

Your request will not return you a record with a minimum value entry_time. Instead, it will return you a minimum value entry_timeand a random one idthat satisfies a condition topic_id = $topic_idthat is not guaranteed to apply to the same record.

+1
source

Source: https://habr.com/ru/post/1743974/


All Articles