我现在的一个场景是,一个用户表,一个用户详情信息表,还有一个行业表。用户详情里面的user_id
对应用户里面的id
,用户里面的hangye
是数值,对用的是行业表里面的type_id
,要取出type_name
$user = DB::table('userinfo as b')
->rightJoin('users as a','a.id','=','b.user_id')
->rightJoin('kehuanli_type as c','c.type_id','=','b.hangye')
->where('a.id',$id)
->select('a.name','a.email','a.created_at','b.tel','b.address','c.type_name')
->get()->toArray();
打开laravel的debugar工具
select * from `users` where `users`.`id` = '7' limit 1
select `a`.`name`, `a`.`email`, `a`.`created_at`, `b`.`tel`, `b`.`address`, `c`.`type_name` from `userinfo` as `b` right join `users` as `a` on `a`.`id` = `b`.`user_id` right join `kehuanli_type` as `c` on `c`.`type_id` = `b`.`hangye` where `a`.`id` = '7'
2.查询某个小时或者某天的信息day('time') hour('time')就可以取出一个时间格式的某天或者某小时
HAVING 子句
在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用。
SELECT Customer,SUM(OrderPrice) FROM Orders
WHERE Customer='Bush' OR Customer='Adams'
GROUP BY Customer
HAVING SUM(OrderPrice)>1500