search

Home  >  Q&A  >  body text

GROUP_CONCAT using the CASE statement can produce multiple outputs.

<p>I have a SQL query that contains the following line of code: </p> <pre class="brush:php;toolbar:false;">GROUP_CONCAT(CASE WHEN t3.ship=1 AND t4.item=0 THEN t2.item_name END ORDER BY item_id SEPARATOR '<br>') `My Item List`</pre> <p>The current output is: Lamp. It's working fine, but I want the item number stored in the item_no column to be displayed in the list. The desired output is: 1. Lamp. I tried adding some code like this without success: </p><p><br /></p> <pre class="brush:php;toolbar:false;">GROUP_CONCAT(CASE WHEN t3.ship=1 AND t4.item=0 THEN t2.item_no, '.' ,t2.item_name END ORDER BY item_id SEPARATOR ' <br>') `My Item List`</pre> <p>How to achieve it? </p>
P粉652495194P粉652495194486 days ago577

reply all(1)I'll reply

  • P粉343408929

    P粉3434089292023-07-26 00:08:57

    You must use the CONCAT() function to connect item_no, '.' and item_name:

    ... THEN CONCAT(t2.item_no, '.', t2.item_name) END ...

    reply
    0
  • Cancelreply