Ad
How To Get Last Updated Record With FIND_IN_SET In Laravel?
I am using find_in_set with my query to get data from comma separated field. But it shows me error.
$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");
above query shows me error of :
Cardinality violation: 1241 Operand should contain 1 column(s)
If i will remove AND ('FIND_IN_SET(?,emp_details.emp_grp)' , '18')
this part it will return correct result.
Ad
Answer
There were several typos in your query. Instead of adding everything into a DB::select()
, I also cleaned your query a bit:
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();
Would the following work for you?
Ad
source: stackoverflow.com
Related Questions
- → I can't do a foreign key, constraint error
- → How to implement DbDongle::convertTimestamps as workaround of invalid timestamps with MySql strict
- → MySQL error "Foreign key constraint is incorrectly formed"
- → Eloquent Multitable query
- → "Laravel 5.1" add user and project with userId
- → Database backup with custom code in laravel 5 and get the data upto 10 rows from per table in database
- → Laravel 5.1 QueryException when trying to delete a project
- → Using Array in '->where()' for Laravel Query Building
- → Chaining "Count of Columns" of a Method to Single Query Builder
- → Laravel Eloquent Joining Strange query
- → convert time using mysql laravel 5
- → How to update a column after an expiration date in MySQL?
- → Foreign key constraint fails on existing key
Ad