Php 使用相似词搜索的Laravel SQL查询
我想做什么:Php 使用相似词搜索的Laravel SQL查询,php,sql,laravel,Php,Sql,Laravel,我想做什么: $list = DB:: table("company") ->leftJoin("user", "company.company_id", "=", "user.company_id") ->select( "user.user_id", "company.company_name", "user.department",
$list = DB:: table("company")
->leftJoin("user", "company.company_id", "=", "user.company_id")
->select(
"user.user_id",
"company.company_name",
"user.department",
"user.user_name",
"user.mail",
"user.manager_type",
)
->where(
"company.company_name", "LIKE", "%".$companyName."%", "AND",
"user.department", "LIKE", "%".$department."%", "AND",
"(", "user.user_name", "LIKE", "%".$name_or_mail."%", "OR", "user.mail", "LIKE", "%".$name_or_mail."%", ")"
)
->whereNull("user.deleted_at")
->orderBy('user.created_at', 'desc')
->get();
您好,我正在尝试使用以下条件在Laravel中构建SQL查询:
用户将在3个搜索参数中输入值:
(s1)公司名称
(s2)部门
(s3)用户名/用户电子邮件
我必须编写一个laravel查询,该查询将获得以下所有用户的“用户id”、“公司名称”、“部门”、“用户名”、“电子邮件”、“经理类型”:
- 在其公司名称中包含(s1),以及
- 在其部门名称中包含(s2),以及
- 在他们的电子邮件或用户名中包含(s3)
$list = DB:: table("company")
->leftJoin("user", "company.company_id", "=", "user.company_id")
->select(
"user.user_id",
"company.company_name",
"user.department",
"user.user_name",
"user.mail",
"user.manager_type",
)
->where(
"company.company_name", "LIKE", "%".$companyName."%", "AND",
"user.department", "LIKE", "%".$department."%", "AND",
"(", "user.user_name", "LIKE", "%".$name_or_mail."%", "OR", "user.mail", "LIKE", "%".$name_or_mail."%", ")"
)
->whereNull("user.deleted_at")
->orderBy('user.created_at', 'desc')
->get();
输出:
当这个查询运行时,它只给出部分正确的答案。有些值也与查询无关。我对laravel很陌生,希望能得到所有帮助。使用where closure如下:
DB::表(“公司”)
->leftJoin(“用户”,“公司.公司id”,“用户.公司id”)
->挑选(
“user.user\u id”,
“公司。公司名称”,
“用户.部门”,
“user.user\u name”,
“user.mail”,
“用户管理器类型”,
)->其中(“company.company_name”、“LIKE”、“%.”$companyName.“%”)
->其中(“user.department”、“LIKE”、“%.”$department.“%”)
->其中(函数($query)使用($name\u或\u mail){
$query->where(“user.user\u name”、“LIKE”、“%.”$name\u或\u mail。“%”)
->或where(“user.mail”、“LIKE”、“%”)、$name\u或\u mail.“%”;
})->whereNull(“user.deleted\u at”)
->orderBy('user.created_at','desc')
->get();
您可以添加一些示例数据来说明当前逻辑的失败位置吗?检查where
Hello是否需要精确匹配查询使用where。如果不是,请查看用户名“%or%”在字段中的任何位置找到“or”的值的位置。您好!我试着按照你建议的方式做。有一点语法错误,我编辑了它。希望没问题^_^