select a.* from test a where 1 > (select count(*) from test where name = a.name and val > a.val )
SQL statement, group by name to display the largest val value Field
Question: select count(*) from test where name = a.name and val > a.val Isn’t the subquery a numeric value? Wouldn’t the SQL statement become
select a.* from test a where 1 >1? But this logic is obviously wrong, so what is the correct logic? How do the outer and inner layers compare? I feel like I don’t have enough IQ to understand,,,,,
When getting the maximum value of val, count(*) is 0, 1>0 isn’t it just enough?
When a.val is the maximum value
val > a.val is not true, (select count(*) from test where name = a.name and val > a.val ) returns 0 (no matching records)
1 > (select count(*) from test where name = a.name and val > a.val )
is established, this clause The record is selected
In fact, you already know it, but you just didn’t go around the corner
select a.* from test a where 1 > count
count is the number of all records greater than a.val , only when it does not exist, conut is equal to 0. The expression 1 > 0 holds
When a.val is the maximum value,
val > a.val does not hold, ( select count(*) from test where name = a.name and val > a.val ) returns 0 (no matching records)
1 > (select count(*) from test where name = a.name and val > a.val )
is established and the record is selected
In fact, you already know it, but you just didn’t go around the detour to
select a.* from test a where 1 > ; count
count is the number of all records greater than a.val. Conut is equal to 0 only when it does not exist. The expression 1 > 0 holds
This means traversing every record in the test table, and then comparing it with itself. Each time a record is traversed, it is compared with all its own records.
Equivalent to two foreach in php
foreach($ar as $v)
foreach($ar as $vl)
It means to traverse every record in the test table, and then compare it with itself. Each time a record is traversed, it is compared with all its own records.
Equivalent to two foreach in php
foreach($ar as $v)
foreach($ar as $vl)