gpt4 book ai didi

mysql从多个语句中选择语句

转载 作者:行者123 更新时间:2023-11-29 23:46:24 25 4
gpt4 key购买 nike

SET @row_number:=0;
SET @db_names:='';
SELECT @row_number:=CASE WHEN @db_names=app_id THEN @row_number+1 ELSE 1 END AS row_number,@db_names:=app_id AS db_names
FROM master_order
ORDER BY app_id ;

当我运行此语句时,它会给出结果。

我想要这样

select *from (SET @row_number:=0;
SET @db_names:='';
SELECT @row_number:=CASE WHEN @db_names=app_id THEN @row_number+1 ELSE 1 END AS row_number,@db_names:=app_id AS db_names
FROM master_order
ORDER BY app_id ) as temp

where row_number = 1;

我想选择重复列的第一行

最佳答案

你可以这样简化:

SELECT * FROM master_order
ORDER BY app_id = '' DESC, app_id
LIMIT 1;

但为了澄清如果这种简化不可能的话它将如何工作,无论是这样:

SET @row_number:=0;
SET @db_names:='';

select *from (
SELECT @row_number:=CASE WHEN @db_names=app_id THEN @row_number+1 ELSE 1 END AS row_number,@db_names:=app_id AS db_names
FROM master_order
ORDER BY app_id
) as temp
where row_number = 1;

或者这个:

select *from (
SELECT @row_number:=CASE WHEN @db_names=app_id THEN @row_number+1 ELSE 1 END AS row_number,@db_names:=app_id AS db_names
FROM master_order
CROSS JOIN (SELECT @row_number := 0, @db_names := '') var_init
ORDER BY app_id
) as temp
where row_number = 1;

关于mysql从多个语句中选择语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25912548/

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