Sorry but this is a general MySQL question and not strictly Laravel related. But I wouldn't ask anywhere else as this community is the best.
I'm trying to INSERT INTO by using this query
INSERT INTO songboxes (plays)
select count(`songbox_id`)
from activity_updates as au
join songboxes on au.songbox_id = songboxes.token
where `status` = 1
Group by songbox_id
I am getting the error
Field 'user_id' doesn't have a default value
But I'm not trying to do anything with the user ID, as you can see from the query.
I think the problem may lie in that not every row in the destination table needs updated. However... maybe that's not the problem at all. I really am quite stuck.
// Get the values from table one that I need
$activity_update = DB::table('activity_updates')
->select(DB::raw('songbox_id, count(songbox_id) as count'))
->where('status', '=', 1)
->groupBy('songbox_id')
->get();
// Loop through the values, check if the row exists then update it.
foreach ($activity_update as $update) {
$check = Songbox::query()->where('token', '=', $update->songbox_id)->exists();
if ($check) {
$s = Songbox::query()->where('token', '=', $update->songbox_id)->first();
$s->plays = $update->count;
$s->save();
}
}