laravel - Laravel Left join 不使用 where 子句返回所有记录
问题描述
我没有在左连接中获得所有记录。我的核心查询就像SELECT cities.*, states.name as state FROM cities LEFT JOIN states ON cities.states_id = states.id
它运行良好
我的 laravel 查询是这样的
$all_city = DB::table('cities')
->leftjoin('states', 'cities.states_id', '=', 'states.id')
->where([['states.name', 'LIKE', '%'.$state.'%'], ['cities.name', 'like', '%'.$name.'%']])
->select('cities.*', 'states.name as state')
->orderBy($sort_field, $sort_type)
->paginate($pagination);
return response()->json($all_city);
Laravel 只返回那些在两者中都是唯一的记录。如果我从中删除 where 子句,那么它运行良好。有人可以建议出了什么问题。
提前致谢!!
解决方案
你可以简单地这样做:
$all_city = DB::table('cities')
->leftJoin('states', 'cities.states_id', '=', 'states.id')
->where('states.name', 'like', '%'.$state.'%')
->where('cities.name', 'like', '%'.$name.'%')
->select('cities.*', 'states.name as state')
->orderBy($sort_field, $sort_type)
->paginate($pagination);
return response()->json($all_city);
推荐阅读
- php - php XLSXWriter - 链接有时只出现?
- sql - Postgresql外键作为主键不是唯一的?
- python - 没有名为“sklearn.metrices”的模块
- python - 使用 Apache+Supervisor 设置 Django
- javascript - 猫鼬找到返回空数组,即使它们是数据库上的值
- r - R 中的 step() 如何处理交互和分类变量?
- reactjs - 在 React 中将动态导入组件传递给纯组件时如何防止重新渲染
- javascript - 使用 create-react-app 时在热替换时再次初始化组件
- xaml - 错误 XDG0062:无法设置“内容”。关于 MUCC 导航视图
- javascript - D3-force - 没有重叠节点标签的对称布局