我有一个这样的表
mysql> select * from test;
+----+------+----------+----------+------+
| id | name | content2 | content3 | time |
+----+------+----------+----------+------+
| 38 | aa | aaa | aaaaaa | 222 |
| 39 | b | bbbb | bbbbb | NULL |
| 40 | ccc | c | cc | NULL |
+----+------+----------+----------+------+
3 rows in set (0.00 sec)
我想分别选择每行最长的字段。所以我需要这样的结果
+----+------+----------+----------+------+
| id | name | content2 | content3 | time |
+----+------+----------+----------+------+
| 41 | ccc | bbbb | aaaaaa | 222 |
+----+------+----------+----------+------+
1 row in set (0.00 sec)
尝试使用max()
,但只能选择time
列。
我找到了一些方法并尝试了,但所有这些都不太好。
例如:inner join
。
是的,它可以工作,但我的真实表格中有太多列。通过这种方式,如果我有10列,则需要inner join
10次。
那么,还有其他更好的方法吗?
答案 0 :(得分:0)
您可以使用具有相应顺序的选择列并限制1。
select
(select id from test order by id desc limit 1) as id,
(select name from test order by length(name) desc limit 1) as name,
(select content2 from test order by length(content2) desc limit 1) as content2,
(select content3 from test order by length(content3) desc limit 1) as content3,
(select time from test order by time desc limit 1) as time