将mysql查询转换为雄辩的laravel

0s7z1bwu  于 2021-06-17  发布在  Mysql
关注(0)|答案(3)|浏览(264)

这是我的mysql查询:

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;

这是我雄辩的问题,但我还没说完:

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', * INSERT missing query here *)
            ->JOIN('tasks', 'tasks.id', 'product_tasks.task_id') 
            ->WHERE('product_tasks.product_id', 1) 
            ->get();

我雄辩的提问中遗漏了什么?

mhd8tkvw

mhd8tkvw1#

你能用下面的格式创建子查询吗

$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();
z6psavjg

z6psavjg2#

试试这个雄辩的问题:

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();
vptzau2j

vptzau2j3#

使用 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();

相关问题