如何在Laravel中使用FIND_IN_SET获取最后更新的记录?

时间:2019-01-27 07:30:31

标签: mysql laravel find-in-set

我在查询中使用find_in_set来从逗号分隔的字段中获取数据。但这显示了错误。

$results = DB::select("SELECT basic.updated_at,basic.updated_by,,ccbi.team_group_id,basic.id
FROM basic_info as  basic 
INNER JOIN emp_info  as  emp_details 
ON basic.emp_master_id =  emp_details.emp_id
WHERE basic.id IN (SELECT basic.id FROM basic_info as basic WHERE basic.updated_at = (SELECT MAX(basic.updated_at) FROM basic_info as basic)) AND basic.team_id =  '1' AND ('FIND_IN_SET(?,emp_details.emp_grp)' , '18')   ORDER BY basic.id DESC LIMIT 1");

以上查询显示我的错误:

Cardinality violation: 1241 Operand should contain 1 column(s)

如果我要删除AND ('FIND_IN_SET(?,emp_details.emp_grp)' , '18')这部分,它将返回正确的结果。

1 个答案:

答案 0 :(得分:0)

您的查询中有几种错别字。除了将所有内容添加到DB::select()中之外,我还清理了一下您的查询:

return DB::table('basic_info')
    ->select('basic_info.updated_at', 'basic_info.updated_by', 'basic_info.id')
    ->join('emp_info', 'basic.emp_master_id', 'emp_info.emp_id')
    ->whereRaw("basic_info.id IN (SELECT basic_info.id FROM basic_info WHERE basic_info.updated_at = (SELECT MAX(basic_info.updated_at) FROM basic_info) AND basic_info.team_id =  '1' AND FIND_IN_SET('18',emp_details.emp_grp) ORDER BY basic_info.id DESC LIMIT 1)")
    ->get();

以下工作适合您吗?