concat()与select查询?

时间:2015-07-31 04:52:37

标签: mysql

我需要使用一个concat函数,它具有select查询返回的参数,如

CONCAT(classification_version.NAME,' ',
(select x.NAME,min(DATEDIFF(e.SEND_DATE, x.OPTENTION_DATE)) 
AS DT_DIFF 
from classification_version x inner join classification_element y 
on x.id_project=y.ID_PROJECT inner join email e 
on x.id_project=e.ID_PROJECT_AMENDMENT
where  
y.ID_PROJECT=project.id and y.ID_COMPANY=company.id 
and e.SEND_DATE>x.OPTENTION_DATE  group by e.id,e.SEND_DATE 
order by e.SEND_DATE ASC)

这只是我的查询的一部分。我需要返回min(datediff),因为我只需要相应的名称值。但是它会抛出一个错误,操作数应该包含1列。我不能对select语句使用内部查询,因为project.id和company.id将无法使用。有没有办法解决这个问题?

1 个答案:

答案 0 :(得分:2)

我修改了你的SQL检查一次。

SELECT CONCAT(q1.NAME,' ',DT_DIFF) FROM 
(SELECT x.NAME,MIN(DATEDIFF(e.SEND_DATE, x.OPTENTION_DATE)) 
AS DT_DIFF 
FROM classification_version x INNER JOIN classification_element y
ON x.id_project=y.ID_PROJECT INNER JOIN email e 
ON x.id_project=e.ID_PROJECT_AMENDMENT
WHERE  
y.ID_PROJECT=project.id AND y.ID_COMPANY=company.id 
AND e.SEND_DATE>x.OPTENTION_DATE  GROUP BY e.id,e.SEND_DATE 
ORDER BY e.SEND_DATE ASC) AS q1

谢谢。