简体   繁体   English

CONCAT函数中的MYSQL SELECT语句

[英]MYSQL SELECT statement inside CONCAT function

I was trying to do something like this in my very big select statement, this code snippet is from the SELECT part of the query 我正在尝试在很大的select语句中执行类似的操作,此代码段来自查询的SELECT部分

CONCAT((SELECT alias.`date` FROM alias WHERE id IN(latest_id)),'<-',GROUP_CONCAT(
`alias`.`date` ORDER BY `alias`.`date` DESC SEPARATOR '<-')) AS "date_chain"

but I am getting NULL in "date_chain" column. 但我在“ date_chain”列中为NULL。 If I only write this 如果我只写这个

GROUP_CONCAT(
`alias`.`date` 
ORDER BY `alias`.`date` DESC SEPARATOR '<-') AS "date_chain"

It works. 有用。

But I want to concat the latest date in the start of this chain. 但是,我想了解这个链条开始的最新日期。

adding full SQL 添加完整的SQL

SELECT latest_id,CONCAT((SELECT alias.`date`FROM alias WHERE id IN (latest_id)),'<-',
GROUP_CONCAT(
  `alias`.`date` 
  ORDER BY `alias`.`date` DESC SEPARATOR '<-'
)) AS "date_chain" FROM alias WHERE latest_id IS NOT NULL GROUP BY latest_id;

Kindly can someone help me what is missing in my first syntax? 有人可以帮助我,我的第一个语法中缺少什么吗? Thank you 谢谢

When any value that's an arugment for CONCAT() function is NULL - the output of function evaluates to NULL . 当任何适合CONCAT()函数的NULLNULL -函数的输出结果为NULL See manual for reference. 请参阅手册以供参考。

CONCAT() returns NULL if any argument is NULL. 如果任何参数为NULL,则CONCAT()返回NULL。

Try CONCAT_WS() where you can also specify a separator. 尝试CONCAT_WS() ,您也可以在其中指定分隔符。

CONCAT_WS() does not skip empty strings. CONCAT_WS()不会跳过空字符串。 However, it does skip any NULL values after the separator argument. 但是,它会跳过分隔符参数之后的所有NULL值。

However, don't put NULL as separator - that would cause the result to yield NULL . 但是,请勿将NULL用作分隔符-否则会导致结果产生NULL

Edit after comments 评论后编辑

We established that the reason for this was improper use of outer query column latest_id as a feed for inner SELECT . 我们确定这样做的原因是不正确地使用外部查询列latest_id作为内部SELECT的提要。

  SELECT alias.`date`FROM alias WHERE id IN (latest_id)

was just comparing each id to latest_id from the same row, while the desired result was to compare it to column from outer SELECT block. 只是将每个id与同一行中的latest_id进行比较,而所需的结果是将其与外部SELECT块中的列进行比较。

Query after changes should be 更改后查询应为

SELECT 
  latest_id,
  CONCAT(
    (SELECT alias.`date`FROM alias WHERE id IN (o.latest_id)),
    '<-',
    GROUP_CONCAT(
      `alias`.`date` ORDER BY `alias`.`date` DESC SEPARATOR '<-'
    )
  ) 
  AS "date_chain" 
FROM alias o
WHERE latest_id IS NOT NULL
GROUP BY latest_id;

Though your query gives you what you want it is not the best way to achieve this result. 尽管查询可以为您提供所需的内容,但这并不是实现此结果的最佳方法。 You should in most cases avoid using CONCAT() because of NULL values and your SELECT inside an outer SELECT block as a column feed would make the query run slow (it must compute values for every row). 你应该在大多数情况下,避免使用CONCAT()因为NULL值和你的SELECT外内SELECT块作为塔进料将使查询运行慢(它必须计算每一行的值)。

Please consider the following code as better practice to get the same result 请考虑以下代码作为更好的做法,以获得相同的结果

SELECT
  foo.latest_id,
  CONCAT_WS('<-', a.date, foo.group_concat) AS date_chain
FROM(
  SELECT  
    latest_id, 
    GROUP_CONCAT(date ORDER BY date DESC SEPARATOR '<-') AS group_concat
  FROM alias
  WHERE latest_id IS NOT NULL
  GROUP BY latest_id
) foo
LEFT JOIN alias a ON
  foo.latest_id = a.id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM