2017-11-15 92 views
7

क्या कोई मुझे नीचे दिए गए SQL कथन का डीबी क्वेरी संस्करण लिखने में मदद कर सकता है। मुझे चुनिंदा कथन और विभाजन में शामिल होने के आसपास थोड़ी मदद चाहिए।कच्चे एसक्यूएल से लार्वेल क्वेरी

मैंने अब तक ऐसा करने में कामयाब रहे हैं।

$query = DB::table(raw('SapakInAdminOrder a')) 
->select(raw('a.*')) 
->leftJoin(raw('currency cu'), 'a.currency', '=', 'cu.id') 
->leftJoin(raw('moodboards m'), 'a.orderMoodboardID', '=', 'm.id') 
->join(raw('clients b'), 'a.clientID', '=', 'b.id') 
->leftJoin(raw('moodboards mc'), 'b.moodboardID', 'mc.id') 
->join(raw('sapakim c'), 'b.sapakID', '=', 'c.id') 
->leftJoin(raw('sapakim sm'), 'c.managerid', '=', 'sm.id') 
->leftJoin(raw('products p'), 'a.productKey', '=', 'p.id') 
->where(function ($query) { 
    $query->whereNull('a.isDeleted'); 
    $query->orWhere('a.isDeleted', '!=', 1); 
}); 

लेकिन मुझे इसे प्राप्त करने की आवश्यकता है।

select * from (select ROW_NUMBER() OVER(ORDER BY case when (indesign.status=4 or indesign.statusdate is null) then getdate()+2 else indesign.statusdate end ASC) AS RowNum,a.* 
FROM sapakInAdminOrder a 
left join currency cu on cu.id=a.currency 
left join moodboards m on m.id=a.orderMoodboardID 
inner join Clients b on a.clientID=b.id 
left join moodboards mc on mc.id=b.moodboardID 
inner join Sapakim c on b.sapakID=c.id 
left join Sapakim sm on sm.id=c.managerid 
left join products p on p.id=a.productKey 
left join (select * from (select ROW_NUMBER() over(PARTITION BY orderID ORDER BY id DESC) r, * from orderCommunication) f where r=1) chat on chat.orderId = a.id 
left join (select id,[status],orderid,approveSMSDate,coverImage,statusDate from (SELECT id,[status],statusDate,approveSMSDate,coverImage,orderid,ROW_NUMBER() OVER(PARTITION BY orderid ORDER BY id DESC) AS r FROM SapakimInAdminDesigns) f where r=1) indesign on a.id=indesign.orderid 
where (a.isDeleted is null or a.isDeleted != 1) and 
     c.inAdminManagerID=(select id from sapakim where sapakguid='test') and 
     c.sapakguid='test' and 
     a.isFreeDesign=0 and 
     a.transactionID = -1 and 
     (a.designerPaid is null or a.designerPaid=0) and 
     (chat.sentToPrinter is null and chat.sentToManager is null and chat.sentToDesigner is null) 
) bb where RowNum>=1 and RowNum<31 
ORDER BY RowNum asc 

मैं साधारण लोगों को कर सकते हैं, लेकिन नहीं काफी वास्तव में विभाजित जुड़ जाता है और चुनिंदा बयान के आसपास मेरे सिर लपेटो सकता है।

मैं वास्तव में इस पर एक सहायता की सराहना करता हूं।

अग्रिम धन्यवाद।

+0

कुछ एसक्यूएल को एक परीक्षण स्कीमा सेट करने के लिए साझा करना और आपके अपेक्षित परिणाम –

उत्तर

0

मैं भी इस तरह की समस्या से फंस गया हूं लेकिन मुझे एक समाधान मिला है और यह मेरे लिए ठीक काम कर रहा है!

उपयोग डीबी :: अप्रस्तुत() क्वेरी के इस प्रकार के लिए: Laravel में

$path = base_path() . "/storage/agency.sql"; 
$sql = file_get_contents($path); 
DB::unprepared(DB::raw($sql)); 

सभी एसक्यूएल आदेश डिफ़ॉल्ट रूप से तैयार कर रहे हैं, लेकिन कभी कभी आप एक अप्रस्तुत मोड में एक आदेश पर अमल करने की जरूरत है , क्योंकि कुछ डेटाबेस में कुछ आदेश तैयार मोड में नहीं चलाया जा सकता है।

+0

के साथ कुछ डेटा बीज करना उपयोगी होगा, मैं बस इसे डीबी :: क्वेरी() का उपयोग करके पूछताछ कर सकता हूं लेकिन यह एक सुरुचिपूर्ण समाधान नहीं है। –

+0

डीबी :: तैयार नहीं कच्चे माल चलाता है, डीबी :: कच्चे की आवश्यकता नहीं है। एक पाठ फ़ाइल (विशेष रूप से भंडारण फ़ोल्डर में से एक) के सवालों लोड हो रहा है मुसीबत के लिए पूछ रहा है फ़ाइल समझौता किया जाना चाहिए।रिपोजिटरी पैटर्न का उपयोग करने के लिए बेहतर है और अपने स्रोत कोड में डीबी :: चयन() का उपयोग करें। –

0

क्या यह आपका मतलब है?

$result = DB::Select("select * from (select ROW_NUMBER() OVER(ORDER BY case when (indesign.status=4 or indesign.statusdate is null) then getdate()+2 else indesign.statusdate end ASC) AS RowNum,a.* 
FROM sapakInAdminOrder a 
left join currency cu on cu.id=a.currency 
left join moodboards m on m.id=a.orderMoodboardID 
inner join Clients b on a.clientID=b.id 
left join moodboards mc on mc.id=b.moodboardID 
inner join Sapakim c on b.sapakID=c.id 
left join Sapakim sm on sm.id=c.managerid 
left join products p on p.id=a.productKey 
left join (select * from (select ROW_NUMBER() over(PARTITION BY orderID ORDER BY id DESC) r, * from orderCommunication) f where r=1) chat on chat.orderId = a.id 
left join (select id,[status],orderid,approveSMSDate,coverImage,statusDate from (SELECT id,[status],statusDate,approveSMSDate,coverImage,orderid,ROW_NUMBER() OVER(PARTITION BY orderid ORDER BY id DESC) AS r FROM SapakimInAdminDesigns) f where r=1) indesign on a.id=indesign.orderid 
where (a.isDeleted is null or a.isDeleted != 1) and 
     c.inAdminManagerID=(select id from sapakim where sapakguid='test') and 
     c.sapakguid='test' and 
     a.isFreeDesign=0 and 
     a.transactionID = -1 and 
     (a.designerPaid is null or a.designerPaid=0) and 
     (chat.sentToPrinter is null and chat.sentToManager is null and chat.sentToDesigner is null) 
) bb where RowNum>=1 and RowNum<31 
ORDER BY RowNum asc"); 
+0

यह डीबी :: क्वेरी() होगा लेकिन फिर भी यह नहीं है कि मेरा मतलब –

0

हो सकता है कि आप इन विभाजित प्रश्नों के लिए एक डेटाबेस दृश्य बनाना चाहिए? फिर आप बाद में डेटाबेस से दृश्य में शामिल हो सकते हैं।

तकनीकी तौर पर इन विश्लेषणात्मक कार्यों आमतौर पर चौखटे द्वारा समर्थित नहीं हैं।

+0

है जिसका मतलब है कि लार्वेल में ऐसा करने का कोई तरीका नहीं है? –

+0

सभी डीबी-एस विश्लेषणात्मक कार्यों का समर्थन नहीं कर रहे हैं। ओआरएम-एस को सार्वभौमिक और सभी संभावित डीबी इंजनों का समर्थन करने के लिए बनाया जाता है। ओआरएम में इन परिष्कृत प्रश्नों को लागू करने का कोई मतलब नहीं है, ज्यादातर लोग अपने अस्तित्व को भी नहीं जानते हैं। उदाहरण के लिए, कुछ निष्पादन, JOOQ हैं, लेकिन यह PHP भाषा नहीं है (https://blog.jooq.org/2013/11/03/probably-the-coolest-sql-feature-window-functions/)। – Aret

0

ऑरेटर नामक इस नए लार्वा टूल को देखें। यह आपको बस "विरासत" एसक्यूएल पेस्ट करने देता है और लैरावेल के डीबी क्वेरी बिल्डर सिंटैक्स को वापस देता है।

Laravel Orator News Article

Online Conversion Tool

0

मान लिया जाये कि आप दृश्यपटल पर इस प्रदर्शित का उपयोग रिश्तों वाक्पटु मॉडल बुद्धि करना चाहते हैं, https://laravel.com/docs/master/eloquent-relationships#one-to-one

मॉडल उपयोगकर्ता में कहा जाता है एक वन के संबंध की पहचान की है विधि फोन() के द्वारा, आप शो दृश्य जो यह सोचते हैं iM के लिए जाना यदि कोई उपयोगकर्ता $ चर गुजरता निम्न कार्य

<?php dd($user->phone); ?> 

यदि आप इसका मतलब नहीं है तो मुझे प्रश्न का पूरा बिंदु याद आ रहा है।

संबंधित मुद्दे