The mysql group has the largest value by name. I don't understand it. please try again.

Source: Internet
Author: User
The mysql group has the largest value by name. I don't understand it. please try again. Select a. * from test a where 1> (select count (*) from test where name = a. name and val> a. val)
SQL statement. The maximum val value field is displayed by name grouping.
Q: Does the select count (*) from test where name = a. name and val> a. val subquery find that it is not a value? The SQL statement is not
Select a. * from test a where 1> 1? But this logic is obviously incorrect. what is the correct logic? How is the outer and inner layers compared? I feel that I cannot understand my IQ ,,,,,


Reply to discussion (solution)

When the maximum val value is obtained, the count (*) is 0, and 1> 0 is not exactly true.

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 record)
1> (select count (*) from test where name = a. name and val> a. val)
Yes, this record is selected

You already know, but you didn't bypass the bend.
Select a. * from test a where 1> count
Count is the number of all records greater than a. val. it is valid only when conut is equal to 0. expression 1> 0 does not exist.

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 record)
1> (select count (*) from test where name = a. name and val> a. val)
Yes, this record is selected

You already know, but you didn't bypass the bend.
Select a. * from test a where 1> count
Count is the number of all records greater than a. val. it is valid only when conut is equal to 0. expression 1> 0 does not exist.


Thank you for your answers!
Some of them are clear, but there is still some confusion. When select. * from test a where 2> 0 gets two records. I want to know how the data is obtained. is it compared with myself? Because I directly write select a. * from test a where 1> 0, this condition can retrieve all the data.

This means that each record in the test table is traversed and compared with itself. each record is compared with all its own records.

Equivalent to two foreach commands in php
Foreach ($ ar as $ v)
Foreach ($ ar as $ vl)

This means that each record in the test table is traversed and compared with itself. each record is compared with all its own records.

Equivalent to two foreach commands in php
Foreach ($ ar as $ v)
Foreach ($ ar as $ vl)


That's what I think. it's just a bit uncertain. haha, thank you to the two moderators.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.