GROUP_CONCAT using the CASE statement can produce multiple outputs.
P粉652495194
2023-07-25 08:17:06
<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>
You must use the CONCAT() function to connect item_no, '.' and item_name:
... THEN CONCAT(t2.item_no, '.', t2.item_name) END ...