如何在 laravel 中使用 FIND_IN_SET 获取最后更新的记录?
How to get last updated record with FIND_IN_SET in laravel?
我在查询中使用 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')
这部分,它将 return 正确结果。
您的查询中有几个拼写错误。我没有将所有内容都添加到 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();
以下内容对您有用吗?
我在查询中使用 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')
这部分,它将 return 正确结果。
您的查询中有几个拼写错误。我没有将所有内容都添加到 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();
以下内容对您有用吗?