如何 select 从 mysql 中的 select 语句返回的多列中的一列

How to select one column from multiple columns returning from a select statement in mysql

我想 select 只是 select 语句中多列中的一列。

select 
    A, sum(B) paid
from
   T
where
    K LIKE '2015%'
group by A
having B >= 100

此查询将 return 两列,但如何 select 只是 select 查询的第一列?如果我这样做:

select A from (select 
    A, sum(B) paid
from
   T
where
    K LIKE '2015%'
group by A
having B >= 100 ) 

是运行进入错误?有没有办法在 mysql 到 select 唯一的第一列?

您可以选择以下两者之一:

一个

select A from (
  select A, sum(B) paid from T where K LIKE '2015%'
 group by A having sum(B) >= 100
) m

两个

select A from T where K like '2015%' group by A having sum(B)>=100

你只要你说的A。

select distinct A 
from T 
where k like '2015%' and B >= 100

顺便说一句,我们不知道 k 是什么数据类型(即:请参阅 Gordon 的评论)

Mysql year function 对于 date/datetime

所以会是 year(k)=2015

您的第二个查询是正确的,只是您没有在 b

之前添加 sum

试试这个

select A from (select 
    A, sum(B) paid
from
   T
where
    K LIKE '2015%'
group by A
having sum(B) >= 100 ) As temp

我猜 K 是约会对象。您不应该使用 like 作为日期。编写查询的最佳方式是:

select A
from T
where K >= '2015-01-01' and K < '2016-01-01'
group by A
having sum(B) >= 100;

这可以利用 T(K, A, B) 上的索引。