लौटने में शामिल हों, और हम निम्न तालिकाओं है:स्व केवल एक सूची प्रबंधन प्रणाली पर कार्य करना एक रिकॉर्ड
================================================
| orders | order_line_items | product_options |
|--------|-------------------|-----------------|
| id | id | id |
| start | order_id | name |
| end | product_option_id | |
| | quantity | |
| | price | |
| | event_start | |
| | event_end | |
================================================
मैं एक निश्चित तिथि पर सूची की गणना करने के कोशिश कर रहा हूँ, इसलिए मैं एक आत्म में शामिल होने बनाने की जरूरत है order_line_items में अन्य रिकॉर्ड्स की मात्रा के SUM को उसी product_option_id के साथ मात्रा की तुलना करने के लिए, और जहां ईवेंट प्रारंभ और अंत एक सीमा के भीतर हैं।
तो, एक तिथि 2016/01/20 दिया, मेरे पास है:
SELECT order_line_items.id, order_line_items.product_option_id, order_line_items.order_id FROM order_line_items
WHERE order_line_items.event_end_date >= '2016-01-20 04:00:00'
AND order_line_items.event_start_date <= '2016-01-21 04:00:00'
AND order_line_items.product_option_id IS NOT NULL;
ऊपर रिटर्न 127 पंक्तियों
जब मैं एक आत्म करने के लिए शामिल हो, तो तरह का प्रयास करें:
SELECT
order_line_items.id,
order_line_items.product_option_id,
order_line_items.order_id,
order_line_items.quantity,
other_line_items.other_product_option_id,
other_line_items.other_order_id,
other_line_items.other_quantity,
other_line_items.total
FROM order_line_items
JOIN (
SELECT
id,
product_option_id AS other_product_option_id,
order_id AS other_order_id,
quantity AS other_quantity,
SUM(quantity) total
FROM order_line_items
WHERE order_line_items.event_end_date >= '2016-01-20 04:00:00'
AND order_line_items.event_start_date <= '2016-01-21 04:00:00'
) other_line_items ON order_line_items.product_option_id = other_line_items.other_product_option_id
WHERE order_line_items.event_end_date >= '2016-01-20 04:00:00'
AND order_line_items.event_start_date <= '2016-01-21 04:00:00'
AND order_line_items.product_option_id IS NOT NULL;
यह केवल 1 रिकॉर्ड देता है। आप यहाँ देख के रूप में कर सकते हैं:() रिकॉर्ड के बहुत ही product_option_id अतः यह अंतिम क्वेरी के लिए जोड़ा SUM(...)
पंक्तियों
एक सुझाव, आपको दूसरी तालिका का नाम बदलने की आवश्यकता है, क्योंकि सिस्टम भ्रमित हो सकता है, और दूसरे स्थान की आवश्यकता नहीं है, क्योंकि आपके पास उप क्वेरी में कोई है। –
धन्यवाद - दूसरे को हटाकर मुझे और रिकॉर्ड मिला, लेकिन अब लौटाए गए सभी रिकॉर्ड एक ही product_option_id हैं .. अजीब: https: // goo।gl/itL5bF – Laravelian
ठीक है, इसे 'join' विकल्प', try_id = other_order_id और production_order_id = other_production_order_id' –