时间:2021-07-01 10:21:17 帮助过:4人阅读
//left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 //取出文章表的id title字段 栏目表的栏目id 栏目名(name)字段 文章表clas_id-关联栏目表ID 条件文章表id大于30 栏目ID小于22 $sql = "SELECT problem.id,problem.title,cate.id AS cateid,cate.name AS catename FROM dp_problem AS problem lEFT JOIN dp_class_hyw AS cate ON problem.class_id = cate.id WHERE problem.id > 25 AND cate.id<22"; $list = Db::query($sql); echo "<pre>"; print_r($list); echo "</pre>"; 得到结果集为 Array ( [0] => Array ( [id] => 26 [title] => 2017年独生子女交不交合作医疗 [cateid] => 20 [catename] => 婚姻家庭 ) [1] => Array ( [id] => 27 [title] => 今年独生子女还用缴合作医疗保险吗 [cateid] => 20 [catename] => 婚姻家庭 ) [2] => Array ( [id] => 29 [title] => 想要离婚怎么办? [cateid] => 20 [catename] => 婚姻家庭 ) [3] => Array ( [id] => 30 [title] => 公司想上市怎么办 [cateid] => 17 [catename] => 公司法律顾问 ) [4] => Array ( [id] => 34 [title] => 测试下这个是不是最新栏目 [cateid] => 17 [catename] => 公司法律顾问 ) )
本文出自 “YBT-PHP” 博客,请务必保留此出处http://ybtphp.blog.51cto.com/13257319/1976385
LEFT JOIN用法,好久没用过原生sql了
标签:mysql