我的CMS中有2个表格。一个包含模板变量,例如:
table: template_variables
||id||contentid||value||
||1 ||3 ||"Some template variable"||
||1 ||4 ||"Another template variable"||
||2 ||5 ||"Some other template variable"||
另一个表包含内容,并且重要的是,有一个列用于说明我是否发布了该内容,例如:
table: content
||id||published||value||
||3 ||0 ||"I am not published"||
||4 ||1 ||"I am published"||
||5 ||1 ||"I am published too"||
我想进行查询,以template_variables
根据其关联的内容是否已发布返回值。例如,使用上述方法,我想返回这些值,Another template variable
并在表Some other template variable
中将它们的关联内容(4和5)标记为已发布content
。
我尝试使用各种查询,INNER JOIN
但没有成功,例如:
SELECT `value`, `contentid` FROM `template_variables` INNER JOIN content WHERE template_variables.contentid=content.id
但无法正常工作-我的语法错误或结果错误。
有人能为我指出正确的方向吗?
您不需要联接,因为您只想使用内容表作为已发布列的参考
SELECT * from template_variables
Where contentid in
(select id from content where
published =1)
或者有更好的表现用Exists(..)
作为
SELECT * from template_variables t
Where Exists(select 1 from content c
Where c.published=1 and
t.contentid=c.id)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句