Mysql groups by name and has the largest value. I don't understand. Please help.

WBOY
Release: 2016-06-27 13:18:07
Original
992 people have browsed it

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,,,,,


Reply to the discussion (solution)

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


Thanks to the two moderators for their answers!
I understand somewhat, but I’m still a little confused. When I select a.* from test a where 2 > 0, I get 2 items. I want to know how this data is obtained. It is compared with itself. Of? Because I directly write select a.* from test a where 1>0, this condition can take out all the data.

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)


That’s what I think, it’s just a bit Not sure, haha, thank you two moderators.
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template