如何提取具有最大值的行?

发布于 2024-10-08 22:01:56 字数 862 浏览 4 评论 0原文

我有两张桌子,想要执行以下操作。我从第一个表中提取满足给定条件的所有行。对于所有提取的行,我从第一列中获取一个值。对于每个值,我从第二个表中提取第一列中包含给定值的所有原始数据。从第二个表中提取的所有原始数据都必须以特定方式排序,我只想取第一行(根据排序标准)。这是我的问题,我不知道如何取排序标准最高值的一行。

添加

第一个表:

| aaa | Bob |
| bbb | Eva |
| ccc | Bob |

第二个表:

| aaa | 111 | 1 |
| aaa | 342 | 2 |
| ccc | 576 | 1 |
| ccc | 749 | 3 |

从第一个表中,我获取包含 Bob 的所有行。这些行中的第一列包含以下值:“aaa”和“ccc”。对于第二个表中的“aaa”,我有两行,我想采用最后一列中具有最大值的行。所以,我有 |aaa|342|2|。我对“ccc”也是如此。我选择这一行|ccc|749|3|。最后我想根据最后一列的值对两行进行排序。

添加2

我刚刚意识到问题的本质如下。在给定的表中,我想用一个“代表性”行(在第三列中具有最大值)替换第一列中包含相同值的所有行。更详细地说,我想用这个替换这张表

| aaa | 111 | 1 |
| aaa | 342 | 2 |
| ccc | 576 | 1 |
| ccc | 749 | 3 |

| aaa | 342 | 2 |
| ccc | 749 | 3 |

I have two tables and want to do the following. From the first table I extract all rows that fulfill a given condition. For all extracted rows I take a value from the first column. For every value I extract all raws from the second table that contain the given value in the first column. All raws extracted from the second table have to be ordered in a specific way and I want to take only one row that is the first (according to the ordering criteria). This is my problem, I do not know how to take one row with the highest value of the ordering criteria.

ADDED

The first table:

| aaa | Bob |
| bbb | Eva |
| ccc | Bob |

The second table:

| aaa | 111 | 1 |
| aaa | 342 | 2 |
| ccc | 576 | 1 |
| ccc | 749 | 3 |

From the first table I take all rows containing Bob. The first column in these rows contain the following values: "aaa" and "ccc". For "aaa" in the second table I have two rows and I want to take those that has the maximal value in the last column. So, I have |aaa|342|2|. The same I do with the "ccc". I take this row |ccc|749|3|. And finally I want to have the two rows ordered according to the value of the last column.

ADDED 2

I have just realized that the essence of the problem is as following. In a given table I want to replace all rows containing the same value in the first columns by one "representative" row (that has the maximal value in the third column). In more details, I want to replace this table:

| aaa | 111 | 1 |
| aaa | 342 | 2 |
| ccc | 576 | 1 |
| ccc | 749 | 3 |

By this one:

| aaa | 342 | 2 |
| ccc | 749 | 3 |

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

屌丝范 2024-10-15 22:01:56
select t2.* 
  from t1
  join t2 on t2.id = (select t2c.id 
                        from t2 as t2c 
                       where t2c.t1_id = t1.id 
                       order by t2c.val desc
                       limit 1)
 where t1.name = 'Bob'
 order by t2.val

您没有指定列名称,所以我编了它们。

t2.id 必须是唯一的。

select t2.* 
  from t1
  join t2 on t2.id = (select t2c.id 
                        from t2 as t2c 
                       where t2c.t1_id = t1.id 
                       order by t2c.val desc
                       limit 1)
 where t1.name = 'Bob'
 order by t2.val

You did not specify column names, so i made them up.

t2.id has to be unique.

梦情居士 2024-10-15 22:01:56

这可以使用相关子查询来实现,但性能会很糟糕。

mysql> select * from user;
+--------+-----------+
| userid | user_name |
+--------+-----------+
| aaa    | Bob       |
| bbb    | Eva       |
| ccc    | Bob       |
+--------+-----------+
3 rows in set (0.00 sec)

mysql> select * from user_data;
+--------+-------+------+
| userid | value | num  |
+--------+-------+------+
| aaa    |   111 |    1 |
| aaa    |   342 |    2 |
| ccc    |   576 |    1 |
| ccc    |   749 |    3 |
| bbb    |  1000 |    1 |
| bbb    |   800 |    2 |
+--------+-------+------+
6 rows in set (0.00 sec)

mysql> SELECT u1.userid,u1.user_name,ud1.value from user_data ud1 join user u1 ON ud1.userid=u1.userid where ud1.value=(SELECT value FROM user_data ud2 WHERE ud2.userid=ud1.userid ORDER BY value desc LIMIT 1) AND u1.user_name='Bob';
+--------+-----------+-------+
| userid | user_name | value |
+--------+-----------+-------+
| aaa    | Bob       |   342 |
| ccc    | Bob       |   749 |
+--------+-----------+-------+
2 rows in set (0.00 sec)

This can be achieved using correlated subqueries but the performance will be abysmal.

mysql> select * from user;
+--------+-----------+
| userid | user_name |
+--------+-----------+
| aaa    | Bob       |
| bbb    | Eva       |
| ccc    | Bob       |
+--------+-----------+
3 rows in set (0.00 sec)

mysql> select * from user_data;
+--------+-------+------+
| userid | value | num  |
+--------+-------+------+
| aaa    |   111 |    1 |
| aaa    |   342 |    2 |
| ccc    |   576 |    1 |
| ccc    |   749 |    3 |
| bbb    |  1000 |    1 |
| bbb    |   800 |    2 |
+--------+-------+------+
6 rows in set (0.00 sec)

mysql> SELECT u1.userid,u1.user_name,ud1.value from user_data ud1 join user u1 ON ud1.userid=u1.userid where ud1.value=(SELECT value FROM user_data ud2 WHERE ud2.userid=ud1.userid ORDER BY value desc LIMIT 1) AND u1.user_name='Bob';
+--------+-----------+-------+
| userid | user_name | value |
+--------+-----------+-------+
| aaa    | Bob       |   342 |
| ccc    | Bob       |   749 |
+--------+-----------+-------+
2 rows in set (0.00 sec)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文