【发布时间】:2019-06-04 22:06:15
【问题描述】:
我一直在尝试加入另一个表,其表名与另一个表中的列中的值匹配。
如果我将表名硬编码为 __gun 之类的匹配表,它可以正常工作,但我无法获取要使用的 col 值 + 开头的 concat 下划线。
问题出在left join这里:
left join CONCAT('__', b.related_table) c on b.related_id = c.id
我需要在连接中使用related_table 列。前面有__。
尝试:
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
感谢您考虑我的问题
【问题讨论】:
-
FROM 采用表名文字或子选择。这似乎是一个糟糕的设计。 How can you represent inheritance in a database?