我有一个名为“客户”的表:
id | name | age
1 | john | 35
2 | paul | 22
3 | ana | 26
4 | mark | 19
5 | jack | 29
我想选择姓名和最大年龄,姓名和最小年龄...类似
john 35 mark 19
有可能吗?
答案 0 :(得分:1)
以下查询将根据您的要求在一行中提供最大和最小。如果最小/最大有多个匹配项,您将获得多行。取决于所使用的SQL引擎,限制为一行的语法是不同的。
SELECT cMax.Name, cMax.Age, cMin.Name, cMin.Age
FROM customers cMin
JOIN customers cMax ON
cMax.Age = (SELECT MAX(Age) FROM customers)
WHERE cMin.Age = (SELECT MIN(Age) FROM customers)
有不同类型的联接(例如INNER,OUTER,CROSS);但是,对于您的问题,使用哪个都没关系。
答案 1 :(得分:0)
尝试
select name, age from customers where age=(select max(age) from customers) union
select name, age from customers where age=(select min(age) from customers)
答案 2 :(得分:0)
是的,你可以做到
select name, age from customers
where age in (select max(age)
from customers union select min (age)from customers)
答案 3 :(得分:0)
如果您希望它们位于同一行:
private PostCallback callback;
private Exception exception;
public setPostCallback(PostCallback callback){
this.callback = callback;
}
@Override
protected JSONObject doInBackground(String... args){
// keep everything as before but when an Exception occurs,
// assign it to *exception* in the catch block
}
@Override
public void onPostExecute(JSONObject jsonObject){
// Note: this will be executed on the main thread
if(exception == null){
callback.onSuccess(jsonObject);
} else {
callback.onError(exception);
}
}
select cmin.*, cmax.*
from (select name, age as minage
from customers
order by age asc
fetch first 1 row only
) cmin cross join
(select name, age as maxage
from customers
order by age desc
fetch first 1 row only
) cmax;
是仅返回结果集第一行的标准语法。某些数据库具有定制语法,例如fetch first 1 row only
或limit
。
答案 4 :(得分:0)
尝试使用此查询显示MAX年龄:-
select * from customers where age=(select max(age) from customers);
要显示MIN年龄,请使用以下查询:-
select * from customers where age=(select min(age) from customers);
答案 5 :(得分:-1)
您可以使用cross join,它将两个查询输出并排放置。建立Rodrigo的查询:
df %>%
group_by(as.Date(Datetime)) %>%
mutate(LightIntensity = Irradiance/max(Irradiance))
它可能不是性能最高的,但形状正确。当表的行不完全为1时,请小心交叉联接,因为它会创建要联接的表中各行的笛卡尔积。