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

如何从mysql中select语句返回的多个列中选择一列,mysql,sql,mysql-workbench,Mysql,Sql,Mysql Workbench,我只想在select语句中从多个列中选择一列 select A, sum(B) paid from T where K LIKE '2015%' group by A having B >= 100 此查询将返回两列,但如何仅从select查询中选择第一列?如果我这样做: select A from (select A, sum(B) paid from T where K LIKE '2015%' group by A having B

我只想在select语句中从多个列中选择一列

select 
    A, sum(B) paid
from
   T
where
    K LIKE '2015%'
group by A
having B >= 100
此查询将返回两列,但如何仅从select查询中选择第一列?如果我这样做:

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

它正在出错?在mysql中有没有办法只选择第一列

您可以执行以下两种操作之一:

一个

两个


你说你只是想要一个新的

select distinct A 
from T 
where k like '2015%' and B >= 100
顺便说一句,我们不知道什么是数据类型k(即:见Gordon的评论)

用于日期/日期时间的Mysql


因此,这将是
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
是一个日期。你不应该像那样使用日期。编写查询的最佳方法是:

select A
from T
where K >= '2015-01-01' and K < '2016-01-01'
group by A
having sum(B) >= 100;
选择一个
从T
其中K>='2015-01-01'和K<'2016-01-01'
分组
总和(B)>=100;

这可以利用
T(K,A,B)

上的索引。如果您不想要求和,为什么要查询它?做您正在做的事情怎么样,但在前端,只使用其中一个!抱歉更新了我的问题。sumdo you want or not?不要像那样使用having子句表示日期。第一个子句将抛出此错误
错误代码:1248。每个派生表都必须有自己的别名
Oh wait!
m
在这里的意义是什么?:)这正在工作
m
是别名。不要忽视它。括号内的子查询
选择一个..
需要有别名
m
是别名。我缺少此部分。谢谢:)你必须像@zedfoxus answer那样附加
m
,否则它会抛出一个错误。谢谢你的分享。
select A from (select 
    A, sum(B) paid
from
   T
where
    K LIKE '2015%'
group by A
having sum(B) >= 100 ) As temp
select A
from T
where K >= '2015-01-01' and K < '2016-01-01'
group by A
having sum(B) >= 100;