لدي الكونترولر التالي في Laravel وهنا جزء من التعليمات البرمجية الخاصة بي لاستعلام SQL الذي قمت بإنشائه:
$query = <<<SQL
SELECT day, date, product1_shift1, product1_shift2, product1_shift3,
COALESCE(product1_shift1, 0) + COALESCE(product1_shift2, 0) + COALESCE(product1_shift3, 0) AS total_product1_qty
FROM (
SELECT DAYNAME(date) AS day, DATE_FORMAT(date, "%d-%m-%Y") AS date,
(SELECT SUM(qty) AS product1_shift1
FROM bill_details
JOIN bills ON bills.bill_id=bill_details.bill_id
WHERE bills.shift_id='48c73e3d-57de-4fea-aae4-7222d7e6afed'
AND bills.date=bills_master.date
AND bills_master.customer_id=bills.customer_id
AND bill_details.product_id='3516c43b-ffe3-4e90-9d93-924a9865c9cd') AS product1_shift1,
(SELECT SUM(qty) AS product1_shift2
FROM bill_details
JOIN bills ON bills.bill_id=bill_details.bill_id
WHERE bills.shift_id='463ec995-cfad-46ff-8e65-3115e6e651c5'
AND bills.date=bills_master.date
AND bills_master.customer_id=bills.customer_id
AND bill_details.product_id='3516c43b-ffe3-4e90-9d93-924a9865c9cd') AS product1_shift2,
(SELECT SUM(qty) AS product1_shift3
FROM bill_details
JOIN bills ON bills.bill_id=bill_details.bill_id
WHERE bills.shift_id='0a792c5f-84a6-4534-818c-427cfe8cd3ca'
AND bills.date=bills_master.date
AND bills_master.customer_id=bills.customer_id
AND bill_details.product_id='3516c43b-ffe3-4e90-9d93-924a9865c9cd') AS product1_shift3
FROM bills bills_master
WHERE bills_master.date BETWEEN '2023-04-07' AND '2023-04-09'
AND bills_master.customer_id='d9916741-ca61-4a57-b281-15c80fad833c'
GROUP BY bills_master.date
) subquery_alias
SQL;
dd(DB::select($query));
ماذا أفعل إذا أردت استبدال متغير PHP ، على سبيل المثال
السؤال
محمد لارافيل
لدي الكونترولر التالي في Laravel وهنا جزء من التعليمات البرمجية الخاصة بي لاستعلام SQL الذي قمت بإنشائه:
$query = <<<SQL SELECT day, date, product1_shift1, product1_shift2, product1_shift3, COALESCE(product1_shift1, 0) + COALESCE(product1_shift2, 0) + COALESCE(product1_shift3, 0) AS total_product1_qty FROM ( SELECT DAYNAME(date) AS day, DATE_FORMAT(date, "%d-%m-%Y") AS date, (SELECT SUM(qty) AS product1_shift1 FROM bill_details JOIN bills ON bills.bill_id=bill_details.bill_id WHERE bills.shift_id='48c73e3d-57de-4fea-aae4-7222d7e6afed' AND bills.date=bills_master.date AND bills_master.customer_id=bills.customer_id AND bill_details.product_id='3516c43b-ffe3-4e90-9d93-924a9865c9cd') AS product1_shift1, (SELECT SUM(qty) AS product1_shift2 FROM bill_details JOIN bills ON bills.bill_id=bill_details.bill_id WHERE bills.shift_id='463ec995-cfad-46ff-8e65-3115e6e651c5' AND bills.date=bills_master.date AND bills_master.customer_id=bills.customer_id AND bill_details.product_id='3516c43b-ffe3-4e90-9d93-924a9865c9cd') AS product1_shift2, (SELECT SUM(qty) AS product1_shift3 FROM bill_details JOIN bills ON bills.bill_id=bill_details.bill_id WHERE bills.shift_id='0a792c5f-84a6-4534-818c-427cfe8cd3ca' AND bills.date=bills_master.date AND bills_master.customer_id=bills.customer_id AND bill_details.product_id='3516c43b-ffe3-4e90-9d93-924a9865c9cd') AS product1_shift3 FROM bills bills_master WHERE bills_master.date BETWEEN '2023-04-07' AND '2023-04-09' AND bills_master.customer_id='d9916741-ca61-4a57-b281-15c80fad833c' GROUP BY bills_master.date ) subquery_alias SQL; dd(DB::select($query));
ماذا أفعل إذا أردت استبدال متغير PHP ، على سبيل المثال
WHERE bills.shift_id='$phpVariable'?
0 أجوبة على هذا السؤال
Recommended Posts
انضم إلى النقاش
يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.