SET @sql = '';
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'CASE WHEN w_date = ''',
w_date,
''' THEN w_detail else ''-'' END AS `',
w_date, '`'
)
) INTO @sql
FROM works;
SET @sql = CONCAT('SELECT p.name, p.position, ', @sql, '
from persons p
inner join works w
on p.id = w.id
group by p.id');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
เมื่อนำมาสร้าง view ไม่ได้ค่ะ ขึ้นว่า You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near SET @sql = ''; SELECT