这是我的MYSQL查询:
exports.user_update = (req, res) => {
const { params, body } = req;
const { userid } = params;
User.findByIdAndUpdate({ _id: userid }, body, { new: true }, (err, user) =>
{
if (err)
res.status(404).send({
message: "There was an issue finding and updating the user on the server."
});
else
res.status(200).send({
user
});
});
};
这是我雄辩的查询,但我还没有完成:
SELECT product_tasks.id AS product_task_id,
tasks.id AS tasks_id,
tasks.task_name AS label,
tasks.serial_id AS serial_id,
(SELECT serials.starting_series
FROM serials
WHERE serials.id = serial_id) AS starting_series
FROM product_tasks
JOIN tasks ON tasks.id = product_tasks.task_id
WHERE product_tasks.product_id = 1;
雄辩的查询中缺少什么?
答案 0 :(得分:0)
尝试这个雄辩的查询:
ProductTask::select(
'product_tasks.id as product_task_id',
'tasks.id as tasks_id',
'tasks.task_name as label',
'tasks.serial_id as serial_id',
'serials.starting_series as starting_series')
->join('tasks', 'tasks.id', '=','product_tasks.task_id')
->join('serials','serials.id','=','tasks.serial_id')
->where('product_tasks.product_id', 1)
->get();
答案 1 :(得分:0)
使用DB:raw()
ProductTask::select(
'product_tasks.id as product_task_id',
'tasks.id as tasks_id',
'tasks.task_name as label',
'tasks.serial_id as serial_id',
DB::raw("SELECT serials.starting_series as starting_series FROM serials WHERE serials.id = serial_id"))
->join('tasks', 'tasks.id', 'product_tasks.task_id')
->where('product_tasks.product_id', 1)
->get();
答案 2 :(得分:0)
请您使用以下格式创建子查询
$pickup = orders::select(DB::raw('*,COUNT(skey) as sk'))
->from(DB::raw('(SELECT a.*
,IF(surrogate_key="",order_no,surrogate_key) as skey
,b.client_name
FROM tb_orders a
LEFT JOIN tb_clients b ON a.client_id=b.client_name
WHERE order_status IN (1,2,3)
AND agent_id=' . $agent->agent_id . ') as tab1'))
->groupBy('skey')
->orderBy('order_id', 'desc')
->paginate($limit);
$data = $pickup->toArray();