简体   繁体   中英

What is the best way to handle this sub query in laravel using DB:raw?

After several attempts I am having trouble getting this raw query formatted using DB::raw in laravel. Could someone point me in the right direction? I am trying to avoid using DB::table or spliting it up into multiple queries if possible.

select id 
from ParticipantDetail 
where testId= (
    select testId from PacketDetail where PacketDetail.packetid=ParticipantDetail.packetid and  [order]='1'
) 
and packetid = (
    select packetid from ParticipantDetail where id = '1f4716e9-6e8b-41ce-b746-60a013fab38f'
)
and masterid = (
    select masterid from ParticipantDetail where id = '1f4716e9-6e8b-41ce-b746-60a013fab38f'
)

I think there is no need to make these sub-queries

I changed the query a little bit. and used DB::select() to run it.

DB::select("select partDtl.id from ParticipantDetail partDtl
    INNER JOIN PacketDetail packDtl on packDtl.packetid = partDtl.packetid
    WHERE partDtl.id = ? and packDtl.order = ?",
     ['1f4716e9-6e8b-41ce-b746-60a013fab38f', '1']);

I hope it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM