gpt4 book ai didi

MySQL 从头开始​​选择直到 row = string

转载 作者:太空宇宙 更新时间:2023-11-03 12:08:07 25 4
gpt4 key购买 nike

有这张表:

+----+-------+---------+-----+
| id | group | name | age |
+----+-------+---------+-----+
| 1 | a | John | 11 |
+----+-------+---------+-----+
| 2 | a | Rachel | 12 |
+----+-------+---------+-----+
| 3 | a | Sarah | 11 |
+----+-------+---------+-----+
| 4 | a | Joe | 14 |
+----+-------+---------+-----+
| 5 | b | Richard | 13 |
+----+-------+---------+-----+
| 6 | b | Zoe | 12 |
+----+-------+---------+-----+

我想编写一个查询,以便获取从表的开头 ( id = 1 ) 到 group = 'b' 的所有值, 所以我得到一个这样的表:

+----+-------+--------+-----+
| id | group | name | age |
+----+-------+--------+-----+
| 1 | a | John | 11 |
+----+-------+--------+-----+
| 2 | a | Rachel | 12 |
+----+-------+--------+-----+
| 3 | a | Sarah | 11 |
+----+-------+--------+-----+
| 4 | a | Joe | 14 |
+----+-------+--------+-----+

谁能帮帮我?非常感谢!

最佳答案

你会做这样的查询:

select tt.*
from thistable tt
where tt.id < (select min(tt2.id) from thistable tt2 where tt2.`group` = 'b');

请注意,group 是列名的糟糕名称,因为它是保留字。

关于MySQL 从头开始​​选择直到 row = string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25536856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com