1

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

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. 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

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

14
  • If you need further help you need to provide full query. Also, best way of debugging queries like that is to run simple SELECT statements separately to see "where the NULL happens" :) Commented Apr 15, 2015 at 21:02
  • I have added to full sql to main question. Commented Apr 15, 2015 at 21:06
  • Please put this in your question - let's respect the reader. It's unreadable this way. Commented Apr 15, 2015 at 21:07
  • yes.. I am sorry. I was doing that.. sorry again! Commented Apr 15, 2015 at 21:10
  • Your query should be rewritten - it's not efficient. But anyways, run SELECT date FROM alias WHERE id = latest_id and post number of rows that you get from this query. Commented Apr 15, 2015 at 21:18

1 Answer 1

5

When any value that's an arugment for CONCAT() function is NULL - the output of function evaluates to NULL. See manual for reference.

CONCAT() returns NULL if any argument is NULL.

Try CONCAT_WS() where you can also specify a separator.

CONCAT_WS() does not skip empty strings. However, it does skip any NULL values after the separator argument.

However, don't put NULL as separator - that would cause the result to yield 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.

  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.

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).

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.