您好我尝试创建查询并添加此查询错误:
JOIN matakuliah ON nilai.idk = matakuliah.idk
WHERE matakuliah.semester = 1
完整查询:
SET @sql_dinamis = ( SELECT GROUP_CONCAT( DISTINCT CONCAT( 'max( IF(idk = ', idk, ',huruf,null) ) AS A', idk ) ) FROM nilai );
SET @SQL = CONCAT( 'SELECT mahasiswa.nim, mahasiswa.nama, ', @sql_dinamis, '
FROM nilai
JOIN mahasiswa ON nilai.idm = mahasiswa.idm
JOIN matakuliah ON nilai.idk = matakuliah.idk
WHERE matakuliah.semester = 1
GROUP BY nilai.idm' );
PREPARE stmt
FROM
@SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
错误消息:
FROM nilai
JOIN mahasiswa ON nilai.idm = mahasiswa.idm
JOIN matakuliah ON nilai.idk = matakuliah.idk
WHERE matakuliah.semester = 1
GROUP BY nilai.idm' )
> OK
> Time: 0s
PREPARE stmt
FROM
@SQL
> 1052 - Column 'idk' in field list is ambiguous
> Time: 0s
帮我解决这个问题。
我的数据库https://irhas.online/test/test2.txt请帮助我。
答案 0 :(得分:0)
您的查询引用了idk
(来自@sql_dinamis
部分),但nilai
和matakuliah
中都有idk
个字段,因此SQL不知道你想要哪一个。您需要更改
idk
SET @sql_dinamis = ( SELECT GROUP_CONCAT( DISTINCT CONCAT( 'max( IF(idk = ', idk, ',huruf,null) ) AS A', idk ) ) FROM nilai );
要么是nilai.idk
,要么是matakuliah.idk
,例如
SET @sql_dinamis = ( SELECT GROUP_CONCAT( DISTINCT CONCAT( 'max( IF(nilai.idk = ', idk, ',huruf,null) ) AS A', idk ) ) FROM nilai );
这对我有用(见DBFiddle)