我一直在尝试联接另一个表,该表的名称与另一个表的列中的值匹配。
如果我将表名硬编码为匹配的表
__gun
它工作得很好,但我不能得到col值要使用+concat下划线开始。
问题在于
left join
left join CONCAT('__', b.related_table) c on b.related_id = c.id
我需要在联接中使用相关的表列。在它前面加上。
尝试:
SELECT a.*, c.*, b.equipable, b.related_table FROM inventory a
inner join items b on a.item_id = b.id
left join CONCAT('__', b.related_table) c on b.related_id = c.id
WHERE 1=1
and a.id = :inventory_id
and a.user_id = :user_id
SELECT a.*, c.*, b.equipable, b.related_table FROM inventory a
inner join items b on a.item_id = b.id
left join '__'+b.related_table c on b.related_id = c.id
WHERE 1=1
and a.id = :inventory_id
and a.user_id = :user_id
SELECT a.*, c.*, b.equipable, b.related_table FROM inventory a
inner join items b on a.item_id = b.id
left join "__"+Cast(b.related_table as nvarchar(4000)) c on b.related_id = c.id
WHERE 1=1
and a.id = :inventory_id
and a.user_id = :user_id