我正在尝试获取城市表中最长和最短城市名称的长度 这是我正在尝试使用的代码
SELECT Name, char_length(Name) from city WHERE char_length(Name)=(SELECT MAX(char_length(name)), MIN(char_length(name))from city);
但是我得到错误我只能有一个操作数...当我摆脱MAX或MIN时它确实有效。我需要它们在一个查询中运行
答案 0 :(得分: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);
答案 1 :(得分:1)
怎么样
select min(char_length(Name)), max(char_length(Name)) from city;
答案 2 :(得分: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
)