1

我正在尝试获取城市表中最长和最短城市名称的长度这是我尝试使用的代码

SELECT Name, char_length(Name) from city WHERE char_length(Name)=(SELECT MAX(char_length(name)), MIN(char_length(name))from city);

但我得到错误我只能有一个操作数......当我摆脱 MAX 或 MIN 时它确实有效。我需要它们都在一个查询中运行

4

3 回答 3

2

您不能使用=将一个值与返回两列的子查询进行比较。

尝试这个:

SELECT c.Name, char_length(c.Name) FROM city AS c
JOIN (SELECT MAX(char_length(name)) AS max, MIN(char_length(name)) AS min FROM city) AS t
  ON char_length(c.Name) IN (t.max, t.min);
于 2013-09-17T01:08:14.977 回答
1

怎么样

select min(char_length(Name)), max(char_length(Name)) from city;
于 2013-09-17T01:10:05.940 回答
0

一种选择:

(select name, char_length(name) len from city
order by len asc
limit 1)
union
(select name, char_length(name) len from city
order by len desc
limit 1)

另外的选择:

select name, char_length(name) len from city
where char_length(name) in (
  select max(char_length(name)) from city
  union
  select min(char_length(name)) from city
)
于 2013-09-17T01:21:24.073 回答