Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the mysql group by problem

Shulou Source: shulou.com Published: 2022-06-03 05:40:49 10月01日 Update

This article introduces the relevant knowledge of "how to solve mysql group by problem". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

The data tables are as follows:

Demand counts who has the highest wages in each department.

sql select NAME,desp, MAX(price) from show_plan GROUP BY desp

name , desp are the first data...

Correct statement: Use temporary tables. The first time, sort by price desc.

drop TABLE tmp3 ;

CREATE TABLE tmp3 (SELECT * from show_plan ORDER BY price DESC ) ;

SELECT * from tmp3 GROUP BY desp

It is strange that the sql result below is incorrect.

SELECT * from (SELECT * from show_plan ORDER BY price DESC) b GROUP BY b.desp

The result of the implementation of Article 1 is the same...

With the table statement, the judge can test it by himself.

DROP TABLE IF EXISTS `show_plan`;

CREATE TABLE `show_plan` (

`id` bigint(20) NOT NULL AUTO_INCREMENT,

`name` char(4) NOT NULL,

`desp` int(11) NOT NULL,

`price` int(11) NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB ;

INSERT INTO `show_plan` VALUES ('1', 'a1', '1', '55');

INSERT INTO `show_plan` VALUES ('2', 'a2', '1', '20');

INSERT INTO `show_plan` VALUES ('3', 'a3', '1', '63');

INSERT INTO `show_plan` VALUES ('4', 'a4', '2', '89');

INSERT INTO `show_plan` VALUES ('5', 'a5', '2', '78');

INSERT INTO `show_plan` VALUES ('6', 'a6', '2', '90');

INSERT INTO `show_plan` VALUES ('7', 'a7', '3', '88');

INSERT INTO `show_plan` VALUES ('8', 'a8', '3', '77');

INSERT INTO `show_plan` VALUES ('9', 'a9', '3', '100');

INSERT INTO `show_plan` VALUES ('10', 'a10', '1', '77');

INSERT INTO `show_plan` VALUES ('11', 'a11', '4', '75');

"How to solve mysql group by problem" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Tags: Question content data more knowledge result sentence practical highest same successful next dilemma actual salary situation data sheet article case viewer Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn NVidia Shulou Tech Info Shulou Technology Linux