Reputation: 13
I came across the query below to dynamically convert rows into columns with Mysql. Does anyone know what the the 3 single quotes do before & after the Product_Name field?
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'count(case when Product_Name = ''',
Product_Name,
''' then 1 end) AS ',
replace(Product_Name, ' ', '')
)
) INTO @sql
from products;
SET @sql = CONCAT('SELECT pt.partner_name, ', @sql, ' from partners pt
left join sales s
on pt.part_id = s.partner_id
left join products pd
on s.product_id = pd.prod_id
group by pt.partner_name');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Upvotes: 1
Views: 126
Reputation: 562270
https://dev.mysql.com/doc/refman/5.7/en/string-literals.html says:
A
'
inside a string quoted with'
may be written as''
.
There are lots of examples on that manual page.
Upvotes: 2