MySQL查询 - 即使相关表中不存在一个字段,也请返回咨询

时间:2016-04-17 13:13:36

标签: php mysql sql

我有这个查询:

 SELECT of2ab_content.id, of2ab_content.title, of2ab_content.metakey, of2ab_content.metadesc, of2ab_content.introtext, of2ab_content.fulltext AS completetext, of2ab_content.publish_up, of2ab_users.name AS author 
 FROM of2ab_content, of2ab_users 
 WHERE of2ab_content.id = 5039
 AND (of2ab_users.id=of2ab_content.created_by) 

此处的问题是,当字段存在于 of2ab_content.created_by 中时,但它不存在于表 of2ab_users.id 中,则查询返回空。如果作者在users表中不存在,我需要查询返回其余字段。

有什么建议吗?

1 个答案:

答案 0 :(得分:3)

你想要一个Article::whereHas('categories', function($q) use ($cat_id) { $q->where('category_id', $cat_id); })->get(); 。简单的规则:从不if (!IsPostBack) { GridView1.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions"); GridView1.DataBind(); foreach (GridViewRow row in GridView1.Rows) { if (row.RowType == DataControlRowType.DataRow) { Panel panel1 = (Panel)row.FindControl("Panel1"); Panel anspanel = (Panel)panel1.FindControl("anspanel"); anspanel.Style.Add("display", "none"); } } } 子句中使用逗号; 始终使用显式left join语法。

FROM

请注意,使用表别名总是使查询更容易编写和阅读。