2016-05-15 54 views
2

我有两个表:SQL - 表可以通过别名列/字段获取自己的名称吗?

产品:

id 

图像:

id 
imageable_type 
imageable_id 


我试图执行一个查询到的效果:

SELECT * 
FROM product p 
INNER JOIN image i 
    ON i.imageable_id = p.id 
    AND i.imageable_type = "product"  # <--this 

但由于我的应用程序框架的制约,我必须一个字段名称添加到别名p.,所以我寻找的东西的影响:

AND i.imageable_type = p.TABLE_NAME # <--this

其中TABLE_NAME是一些建于“通用专业领域”可谓是引用父表的名称。有没有像MySQL或其他SQL数据库的功能?

+0

是否要在表格中返回表格名称? – Alex

+0

@Alex不,我想加入哪里'i.imageable_type ='表名。 – bsapaka

+0

在sql(或mysql)中,表名必须是显式的..您可以使用过程代码和dinamically生成的代码来执行此类事情。 – scaisEdge

回答

0

你的框架会接受这个吗?

SELECT * 
FROM product p 
INNER JOIN image i 
    ON i.imageable_id = p.id 
WHERE i.imageable_type = "product"