同时获取列的最大值和特定记录的同一列 [英] get max value for column and the same column for specific record at the same time

查看:53
本文介绍了同时获取列的最大值和特定记录的同一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的表:

_id integer
column_1 text
column_2 integer

如果 _id = 1 ,我想获取 column_2 的值,如果不存在,请获取 column_2

I want to get value of column_2 if the _id = 1 and if not exist get the max value for column_2

SELECT max(column_2), column_2
where _id = 2

这肯定会失败,有没有办法在同一个查询中获得两个值?

this will fail sure, is there a way to get the both values in the same query?

推荐答案

尝试一下:

SELECT IFNULL(column_2, (SELECT MAX(column_2) FROM test)) column2  
FROM test WHERE id = 1;

检查 SQL FIDDLE DEMO

这篇关于同时获取列的最大值和特定记录的同一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆