我有一个消息mysql error 1241(21000)操作数应该包含1列

2guxujil  于 2021-07-27  发布在  Java
关注(0)|答案(2)|浏览(333)

我有一个'nama\u pelanggan'列,来自'ms\u pelanggan'表。

+---------------------+
| nama_pelanggan      |
+---------------------+
| Eva Novianti, S.H.  |
| Heidi Goh           |
| Unang Handoko       |
| Jokolono Sukarman   |
| Tommy Sinaga        |
| Irwan Setianto      |
| Agus Cahyono        |
| Maria Sirait        |
| Ir. Ita Nugraha     |
| Djoko Wardoyo, Drs. |
+---------------------+

问题是,如何显示姓名最长的人的姓名(在第一行),以及姓名最短的人的姓名(在下一行)。我试过使用:

SELECT nama_pelanggan 
FROM ms_pelanggan 
WHERE LENGTH(nama_pelanggan) in (
  SELECT MAX(LENGTH(nama_pelanggan)), MIN(LENGTH(nama_pelanggan)) 
  FROM ms_pelanggan
);

但显示:
第2行出现错误1241(21000):操作数应包含1列。
你能帮我找到解决办法吗?

axkjgtzd

axkjgtzd1#

很棘手。你可以做:

with
l as ( -- longest name
  select nama_pelanggan, 1 as o from t order by length(nama_pelanggan) desc limit 1
),
s as ( -- shortest name
  select nama_pelanggan, 2 as o from t order by length(nama_pelanggan) limit 1
),
r as ( -- rest of names
  select nama_pelanggan, 3 as o
  from t
  where nama_pelanggan not in (select nama_pelanggan from l)
    and nama_pelanggan not in (select nama_pelanggan from s)
)
select nama_pelanggan -- together now, sorted as requested
from (
  select * from l union all select * from s union all select * from r
)
order by o
yvgpqqbh

yvgpqqbh2#

第一个select查询将为您提供最大长度的查询,第二个select查询将为您提供最小长度的查询,您可以在查询中添加union,这将添加两个查询的结果
试试这个:

Select column 
from table 
order by length(column) desc 
limit 1
      Union   
Select column 
from table 
order by length(column) 
limit 1;

相关问题