我从这里得到了一些关于我需要的SQL查询的建议,但现在看来,如果遇到没有分配属性的产品,它会返回错误的结果。 我将列出下面的查询,以及相关数据库表/数据的样本(清理数据以删除客户信息)。
查询:
SELECT *
FROM orders_products_attributes oa
INNER JOIN orders_products op ON oa.orders_id = op.orders_id
WHERE (
products_options_id
IN ( 1, 2 )
AND oa.orders_id
IN (
SELECT DISTINCT o.orders_id
FROM orders o
WHERE o.ExportedToTradebox = 0
AND o.orders_id > 22000))
DB:
CREATE TABLE `orders` (
`orders_id` int(11) NOT NULL AUTO_INCREMENT,
`customers_id` int(11) NOT NULL DEFAULT '0',
`customers_name` varchar(64) NOT NULL DEFAULT '',
`customers_company` varchar(64) DEFAULT NULL,
`customers_street_address` varchar(64) NOT NULL DEFAULT '',
`customers_suburb` varchar(32) DEFAULT NULL,
`customers_city` varchar(32) NOT NULL DEFAULT '',
`customers_postcode` varchar(10) NOT NULL DEFAULT '',
`customers_state` varchar(32) DEFAULT NULL,
`customers_country` varchar(32) NOT NULL DEFAULT '',
`customers_telephone` varchar(32) NOT NULL DEFAULT '',
`customers_email_address` varchar(96) NOT NULL DEFAULT '',
`customers_address_format_id` int(5) NOT NULL DEFAULT '0',
`delivery_name` varchar(64) NOT NULL DEFAULT '',
`delivery_company` varchar(64) DEFAULT NULL,
`delivery_street_address` varchar(64) NOT NULL DEFAULT '',
`delivery_suburb` varchar(32) DEFAULT NULL,
`delivery_city` varchar(32) NOT NULL DEFAULT '',
`delivery_postcode` varchar(10) NOT NULL DEFAULT '',
`delivery_state` varchar(32) DEFAULT NULL,
`delivery_country` varchar(32) NOT NULL DEFAULT '',
`delivery_address_format_id` int(5) NOT NULL DEFAULT '0',
`billing_name` varchar(64) NOT NULL DEFAULT '',
`billing_company` varchar(64) DEFAULT NULL,
`billing_street_address` varchar(64) NOT NULL DEFAULT '',
`billing_suburb` varchar(32) DEFAULT NULL,
`billing_city` varchar(32) NOT NULL DEFAULT '',
`billing_postcode` varchar(10) NOT NULL DEFAULT '',
`billing_state` varchar(32) DEFAULT NULL,
`billing_country` varchar(32) NOT NULL DEFAULT '',
`billing_address_format_id` int(5) NOT NULL DEFAULT '0',
`payment_method` varchar(128) NOT NULL DEFAULT '',
`payment_module_code` varchar(32) NOT NULL DEFAULT '',
`shipping_method` varchar(128) NOT NULL DEFAULT '',
`shipping_module_code` varchar(32) NOT NULL DEFAULT '',
`coupon_code` varchar(32) NOT NULL DEFAULT '',
`cc_type` varchar(20) DEFAULT NULL,
`cc_owner` varchar(64) DEFAULT NULL,
`cc_number` varchar(32) DEFAULT NULL,
`cc_expires` varchar(4) DEFAULT NULL,
`cc_cvv` blob,
`last_modified` datetime DEFAULT NULL,
`date_purchased` datetime DEFAULT NULL,
`orders_status` int(5) NOT NULL DEFAULT '0',
`orders_date_finished` datetime DEFAULT NULL,
`currency` char(3) DEFAULT NULL,
`currency_value` decimal(14,6) DEFAULT NULL,
`order_total` decimal(14,2) DEFAULT NULL,
`order_tax` decimal(14,2) DEFAULT NULL,
`paypal_ipn_id` int(11) NOT NULL DEFAULT '0',
`ip_address` varchar(96) NOT NULL DEFAULT '',
`ExportedToTradebox` int(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`orders_id`),
KEY `idx_status_orders_cust_zen` (`orders_status`,`orders_id`,`customers_id`),
KEY `idx_date_purchased_zen` (`date_purchased`),
KEY `idx_cust_id_orders_id_zen` (`customers_id`,`orders_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=23161
INSERT INTO `orders` VALUES (23110, 5801, 'Customer One', '', 'Any BUilding', '', 'Any Town', 'AN0 1TH', 'Lancashire', 'United Kingdom (Mainland)', '01234567890', 'email@customer.com', 6, 'Customer One', '', 'Any Building', 'Any Street', 'Any Town', 'AN0 1TH', 'Lancashire', 'United Kingdom (Mainland)', 6, 'Customer One', '', 'Any Building', 'Any Street', 'Any Town', 'AN0 1TH', 'Lancashire', 'United Kingdom (Mainland)', 6, 'Credit/Debit Card (Secured by Sage Pay)', 'ceon_sage_pay_direct', 'Table Rate (Best Way)', 'table', '', 'Visa', 'Customer One', 'XXXXXXXXXX', '0515', NULL, NULL, '2013-01-10 09:57:51', 1, NULL, 'GBP', 1.000000, 16.14, 2.69, 0, '88.96.93.217 - 88.96.93.217', 1);
INSERT INTO `orders` VALUES (23111, 11361, 'Customer Two', '', 'Any Street', '', 'Any Town', 'AN0 1TH', 'Cumbria', 'United Kingdom (Mainland)', '01234567890', 'email@customer.com', 6, 'Customer Two', '', 'Any Street', '', 'Any Town', 'AN0 1TH', 'Cumbria', 'United Kingdom (Mainland)', 6, 'Customer Two', '', 'Any Street', '', 'Any Town', 'AN0 1TH', 'Cumbria', 'United Kingdom (Mainland)', 6, 'Credit/Debit Card (Secured by Sage Pay)', 'ceon_sage_pay_direct', 'Table Rate (Best Way)', 'table', '', 'MasterCard', 'Customer Two', 'XXXXXXXXXX', '0713', NULL, NULL, '2013-01-10 18:29:23', 1, NULL, 'GBP', 1.000000, 25.50, 4.25, 0, '86.179.13.89 - 86.179.13.89', 0);
INSERT INTO `orders` VALUES (23112, 23787, 'Customer Three', '', 'Any Street', 'Any Town', 'SWANSEA', 'AN0 1TH', 'Swansea', 'United Kingdom (Mainland)', '01234567890', 'email@customer.com', 6, 'Customer Three', '', 'Any Street', '', 'Any Town', 'AN0 1TH', 'Kent', 'United Kingdom (Mainland)', 6, 'Customer Three', '', 'Any Street', 'Any Town', 'SWANSEA', 'AN0 1TH', 'Swansea', 'United Kingdom (Mainland)', 6, 'Credit/Debit Card (Secured by Sage Pay)', 'ceon_sage_pay_direct', 'Table Rate (Best Way)', 'table', '', 'MasterCard', 'Customer Three', 'XXXXXXXXXX', '0314', NULL, NULL, '2013-01-10 20:27:12', 1, NULL, 'GBP', 1.000000, 15.48, 2.58, 0, '176.24.1.143 - 176.24.1.143', 1);
INSERT INTO `orders` VALUES (23113, 23788, 'Customer Four, '', 'Any Street', 'Any Town', 'Reading', 'AN0 1TH', 'Berkshire', 'United Kingdom (Mainland)', '01234567890', 'email@customer.com', 6, 'Customer Four', '', 'Any Building', 'Any Street', 'Any Town', 'AN0 1TH', 'Berkshire', 'United Kingdom (Mainland)', 6, 'Customer Four', '', 'Any Street', 'Any Town', 'Reading', 'AN0 1TH', 'Berkshire', 'United Kingdom (Mainland)', 6, 'Credit/Debit Card (Secured by Sage Pay)', 'ceon_sage_pay_direct', 'Table Rate (Best Way)', 'table', '', 'Visa Debit', 'Customer Four', 'XXXXXXXXXX', '0315', NULL, NULL, '2013-01-10 22:52:17', 1, NULL, 'GBP', 1.000000, 19.74, 3.29, 0, '80.7.137.154 - 80.7.137.154', 1);
INSERT INTO `orders` VALUES (23114, 15693, 'Customer Five', '', 'Any Street', 'Any Town', 'Londonderry', 'AN0 1TH', 'Londonderry', 'United Kingdom (Mainland)', '01234567890', 'email@customer.com', 6, 'Customer Five', '', 'Any Street', 'Any Town', 'Londonderry', 'AN0 1TH', 'Londonderry', 'United Kingdom (Mainland)', 6, 'Customer Five', '', 'Any Street', 'Any Town', 'Londonderry', 'AN0 1TH', 'Londonderry', 'United Kingdom (Mainland)', 6, 'Credit/Debit Card (Secured by Sage Pay)', 'ceon_sage_pay_direct', 'Table Rate (Best Way)', 'table', '', 'Visa', 'Customer Five', 'XXXXXXXXXX', '0715', NULL, NULL, '2013-01-11 08:53:07', 1, NULL, 'GBP', 1.000000, 19.38, 3.23, 0, '92.17.232.252 - 92.17.232.252', 1);
INSERT INTO `orders` VALUES (23115, 23790, 'Customer Six', '', 'Any Street', 'Any Town', 'Huddersfield', 'AN0 1TH', 'West Yorkshire', 'United Kingdom (Mainland)', '01234567890', 'email@customer.com', 6, 'Customer Six', '', 'Any Street', 'Any Town', 'Huddersfield', 'AN0 1TH', 'West Yorkshire', 'United Kingdom (Mainland)', 6, 'Customer Six', '', 'Any Street', 'Any Town', 'Huddersfield', 'AN0 1TH', 'West Yorkshire', 'United Kingdom (Mainland)', 6, 'Credit/Debit Card (Secured by Sage Pay)', 'ceon_sage_pay_direct', 'Table Rate (Best Way)', 'table', '', 'Visa Debit', 'Customer Six', 'XXXXXXXXXX', '1214', NULL, NULL, '2013-01-11 12:50:34', 1, NULL, 'GBP', 1.000000, 428.40, 119.00, 0, '109.237.17.198 - 109.237.17.198', 1);
CREATE TABLE `orders_products` (
`orders_products_id` int(11) NOT NULL AUTO_INCREMENT,
`orders_id` int(11) NOT NULL DEFAULT '0',
`products_id` int(11) NOT NULL DEFAULT '0',
`products_model` varchar(32) DEFAULT NULL,
`products_name` varchar(64) NOT NULL DEFAULT '',
`products_price` decimal(15,4) NOT NULL DEFAULT '0.0000',
`final_price` decimal(15,4) NOT NULL DEFAULT '0.0000',
`products_tax` decimal(7,4) NOT NULL DEFAULT '0.0000',
`products_quantity` float NOT NULL DEFAULT '0',
`onetime_charges` decimal(15,4) NOT NULL DEFAULT '0.0000',
`products_priced_by_attribute` tinyint(1) NOT NULL DEFAULT '0',
`product_is_free` tinyint(1) NOT NULL DEFAULT '0',
`products_discount_type` tinyint(1) NOT NULL DEFAULT '0',
`products_discount_type_from` tinyint(1) NOT NULL DEFAULT '0',
`products_prid` tinytext NOT NULL,
PRIMARY KEY (`orders_products_id`),
KEY `idx_orders_id_prod_id_zen` (`orders_id`,`products_id`),
KEY `idx_prod_id_orders_id_zen` (`products_id`,`orders_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=35040 ;
INSERT INTO `orders_products` VALUES (34978, 23110, 31, '', 'A4 Teacher''s Planner', 10.5000, 10.5000, 20.0000, 1, 0.0000, 0, 0, 0, 0, '31:9964928c32c2a3849188981afa12a093');
INSERT INTO `orders_products` VALUES (34979, 23111, 46, '', 'Custom Built A5 Teacher''s Planner', 9.6000, 13.9000, 20.0000, 1, 0.0000, 0, 0, 0, 0, '46:ba39091f0bf22bf2e10c81adc942e4e1');
INSERT INTO `orders_products` VALUES (34980, 23111, 23, '', 'Notes Book (NB)', 3.5000, 3.5000, 20.0000, 1, 0.0000, 0, 0, 0, 0, '23:eb5397bb218cc175011cd7789237bd50');
INSERT INTO `orders_products` VALUES (34981, 23111, 21, '', 'Wall Chart 2013-2014 (CH34)', 0.9000, 0.9000, 20.0000, 1, 0.0000, 0, 0, 0, 0, '21');
INSERT INTO `orders_products` VALUES (34982, 23112, 46, '', 'Custom Built A5 Teacher''s Planner', 9.6000, 9.9500, 20.0000, 1, 0.0000, 0, 0, 0, 0, '46:f9cc81aecfe9f185caeaaefa16d74bb3');
INSERT INTO `orders_products` VALUES (34983, 23113, 49, '', 'Custom Built Primary Teacher''s Planner', 12.5000, 13.5000, 20.0000, 1, 0.0000, 0, 0, 0, 0, '49:265721a8b48ba787e0109dae30acb7b4');
INSERT INTO `orders_products` VALUES (34984, 23114, 30, '', 'Custom Built A4 Teacher''s Planner', 12.5000, 13.2000, 20.0000, 1, 0.0000, 0, 0, 0, 0, '30:b0018c6b89bd8efbbfa3dcd3a5c4a2ff');
INSERT INTO `orders_products` VALUES (34985, 23115, 22, '', 'Visitors Book (VB)', 5.9500, 5.9500, 20.0000, 100, 0.0000, 0, 0, 0, 0, '22:a2bedb3944abb52dd530db1c8e05b3b9');
CREATE TABLE `orders_products_attributes` (
`orders_products_attributes_id` int(11) NOT NULL AUTO_INCREMENT,
`orders_id` int(11) NOT NULL DEFAULT '0',
`orders_products_id` int(11) NOT NULL DEFAULT '0',
`products_options` varchar(32) NOT NULL DEFAULT '',
`products_options_values` text NOT NULL,
`options_values_price` decimal(15,4) NOT NULL DEFAULT '0.0000',
`price_prefix` char(1) NOT NULL DEFAULT '',
`product_attribute_is_free` tinyint(1) NOT NULL DEFAULT '0',
`products_attributes_weight` float NOT NULL DEFAULT '0',
`products_attributes_weight_prefix` char(1) NOT NULL DEFAULT '',
`attributes_discounted` tinyint(1) NOT NULL DEFAULT '1',
`attributes_price_base_included` tinyint(1) NOT NULL DEFAULT '1',
`attributes_price_onetime` decimal(15,4) NOT NULL DEFAULT '0.0000',
`attributes_price_factor` decimal(15,4) NOT NULL DEFAULT '0.0000',
`attributes_price_factor_offset` decimal(15,4) NOT NULL DEFAULT '0.0000',
`attributes_price_factor_onetime` decimal(15,4) NOT NULL DEFAULT '0.0000',
`attributes_price_factor_onetime_offset` decimal(15,4) NOT NULL DEFAULT '0.0000',
`attributes_qty_prices` text,
`attributes_qty_prices_onetime` text,
`attributes_price_words` decimal(15,4) NOT NULL DEFAULT '0.0000',
`attributes_price_words_free` int(4) NOT NULL DEFAULT '0',
`attributes_price_letters` decimal(15,4) NOT NULL DEFAULT '0.0000',
`attributes_price_letters_free` int(4) NOT NULL DEFAULT '0',
`products_options_id` int(11) NOT NULL DEFAULT '0',
`products_options_values_id` int(11) NOT NULL DEFAULT '0',
`products_prid` tinytext NOT NULL,
`tradebox_attributes_list` text NOT NULL,
`tradebox_options_price_list` decimal(15,4) NOT NULL DEFAULT '0.0000',
PRIMARY KEY (`orders_products_attributes_id`),
KEY `idx_orders_id_prod_id_zen` (`orders_id`,`orders_products_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=77370 ;
INSERT INTO `orders_products_attributes` VALUES (77193, 23110, 34978, 'Lesson format', '9 period day (TP9)', 0.0000, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 5, 57, '31:9964928c32c2a3849188981afa12a093', '', 0.0000);
INSERT INTO `orders_products_attributes` VALUES (77194, 23110, 34978, 'Cover', 'Navy (blue wire)', 0.0000, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 1, 74, '31:9964928c32c2a3849188981afa12a093', '9 period day (TP9), Navy (blue wire)', 0.0000);
INSERT INTO `orders_products_attributes` VALUES (77195, 23111, 34979, 'Lesson format', '5 period day (TP5)', 0.0000, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 5, 54, '46:ba39091f0bf22bf2e10c81adc942e4e1', '', 0.0000);
INSERT INTO `orders_products_attributes` VALUES (77196, 23111, 34979, 'Cover', 'Navy', 0.0000, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 1, 2, '46:ba39091f0bf22bf2e10c81adc942e4e1', 'Navy', 1.0000);
INSERT INTO `orders_products_attributes` VALUES (77197, 23111, 34979, 'Wire', 'Fluorescent Yellow (f)', 0.0000, '', 1, 0, '', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 4, 51, '46:ba39091f0bf22bf2e10c81adc942e4e1', '', 0.0000);
INSERT INTO `orders_products_attributes` VALUES (77198, 23111, 34979, 'Ribbon', 'Fuschia (7)', 0.0000, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 3, 19, '46:ba39091f0bf22bf2e10c81adc942e4e1', '', 0.0000);
INSERT INTO `orders_products_attributes` VALUES (77199, 23111, 34979, 'Initials (Max 4)', 'DBI', 3.3000, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 2, 0, '46:ba39091f0bf22bf2e10c81adc942e4e1', 'DBI', 0.0000);
INSERT INTO `orders_products_attributes` VALUES (77200, 23111, 34979, 'Plastic pockets', '2 at front, 2 at back', 1.0000, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 6, 87, '46:ba39091f0bf22bf2e10c81adc942e4e1', '', 0.0000);
INSERT INTO `orders_products_attributes` VALUES (77201, 23111, 34980, 'Cover', 'Navy', 0.0000, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 1, 2, '23:eb5397bb218cc175011cd7789237bd50', 'Navy', 1.0000);
INSERT INTO `orders_products_attributes` VALUES (77202, 23112, 34982, 'Lesson format', '6 period day (TP6)', 0.0000, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 5, 55, '46:f9cc81aecfe9f185caeaaefa16d74bb3', '', 0.0000);
INSERT INTO `orders_products_attributes` VALUES (77203, 23112, 34982, 'Cover', 'Yellow', 0.0000, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 1, 10, '46:f9cc81aecfe9f185caeaaefa16d74bb3', '6 period day (TP6), Yellow, 2 at back', 0.3500);
INSERT INTO `orders_products_attributes` VALUES (77204, 23112, 34982, 'Wire', 'Silver (i)', 0.0000, '', 1, 0, '', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 4, 50, '46:f9cc81aecfe9f185caeaaefa16d74bb3', '', 0.0000);
INSERT INTO `orders_products_attributes` VALUES (77205, 23112, 34982, 'Ribbon', 'Black (5)', 0.0000, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 3, 17, '46:f9cc81aecfe9f185caeaaefa16d74bb3', '', 0.0000);
INSERT INTO `orders_products_attributes` VALUES (77206, 23112, 34982, 'Plastic pockets', '2 at back', 0.3500, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 6, 85, '46:f9cc81aecfe9f185caeaaefa16d74bb3', '', 0.0000);
INSERT INTO `orders_products_attributes` VALUES (77207, 23113, 34983, 'Lesson format', '3 period (2 morning, 1 afternoon)', 0.0000, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 5, 59, '49:265721a8b48ba787e0109dae30acb7b4', '', 0.0000);
INSERT INTO `orders_products_attributes` VALUES (77208, 23113, 34983, 'Cover', 'Pink', 0.0000, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 1, 8, '49:265721a8b48ba787e0109dae30acb7b4', '3 period (2 morning, 1 afternoon), Pink, 2 at front, 2 at back', 1.0000);
INSERT INTO `orders_products_attributes` VALUES (77209, 23113, 34983, 'Wire', 'Blue (c)', 0.0000, '', 1, 0, '', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 4, 47, '49:265721a8b48ba787e0109dae30acb7b4', '', 0.0000);
INSERT INTO `orders_products_attributes` VALUES (77210, 23113, 34983, 'Ribbon', 'Dorothy (34)', 0.0000, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 3, 81, '49:265721a8b48ba787e0109dae30acb7b4', '', 0.0000);
INSERT INTO `orders_products_attributes` VALUES (77211, 23113, 34983, 'Plastic pockets', '2 at front, 2 at back', 1.0000, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 6, 87, '49:265721a8b48ba787e0109dae30acb7b4', '', 0.0000);
INSERT INTO `orders_products_attributes` VALUES (77212, 23114, 34984, 'Lesson format', '8 period day (TP8)', 0.0000, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 5, 56, '30:b0018c6b89bd8efbbfa3dcd3a5c4a2ff', '', 0.0000);
INSERT INTO `orders_products_attributes` VALUES (77213, 23114, 34984, 'Cover', 'Purple', 0.0000, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 1, 7, '30:b0018c6b89bd8efbbfa3dcd3a5c4a2ff', '8 period day (TP8), Purple, 1 at front, 2 at back', 0.7000);
INSERT INTO `orders_products_attributes` VALUES (77214, 23114, 34984, 'Wire', 'Metallic Red (g)', 0.0000, '', 1, 0, '', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 4, 52, '30:b0018c6b89bd8efbbfa3dcd3a5c4a2ff', '', 0.0000);
INSERT INTO `orders_products_attributes` VALUES (77215, 23114, 34984, 'Ribbon', 'Gold Lamé (17)', 0.0000, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 3, 29, '30:b0018c6b89bd8efbbfa3dcd3a5c4a2ff', '', 0.0000);
INSERT INTO `orders_products_attributes` VALUES (77216, 23114, 34984, 'Plastic pockets', '1 at front, 2 at back', 0.7000, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 6, 86, '30:b0018c6b89bd8efbbfa3dcd3a5c4a2ff', '', 0.0000);
INSERT INTO `orders_products_attributes` VALUES (77217, 23115, 34985, 'Cover', 'Burgundy', 0.0000, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 1, 1, '22:a2bedb3944abb52dd530db1c8e05b3b9', 'Burgundy', 0.0000);
原始查询过去是:
SELECT *
FROM orders_products
WHERE orders_id
IN
(
SELECT DISTINCT orders_id
FROM orders
WHERE ExportedToTradebox='0' and orders_id >'0'
)
订单号23111是问题所在。已排序的挂图没有属性,但在查询数据库时,它会应用为订单中其他项列出的属性。
相关数据的架构可在http://sqlfiddle.com/#!2/f0003找到 我只在那里包含订单号23111的数据。
查询将结果传递给xml文件。结果如下:
1个定制A5教师规划师|海军13.90 2.95
1个定制的A5教师规划师| DBI 3.30 0.00
1 Custom Custom A5教师规划师|海军13.90 2.95
1 NotesBook(NB)|海军3.50 0.00
1 NotesBook(NB)| DBI 3.50 0.00
1 NotesBook(NB)|海军3.50 0.00
1挂图2013-2014 |海军0.90 0.00
1挂图2013-2014 | DBI 0.90 0.00
1挂图2013-2014 |海军0.90 0.00
应该是:
1个定制A5教师规划师|海军13.90 2.95
1个定制的A5教师规划师| DBI 3.30 0.00
1 NotesBook(NB)|海军3.50 0.00
1挂图2013-2014 0.90 0.00
提前感谢您提供任何帮助。
DRapp为此提供了一个几乎可行的解决方案,但是,尽管查询返回了正确的结果,但在使用中会产生问题,因为从结果调用orders_id不会为没有属性的产品提供订单ID。这很可能是由表的连接方式引起的。如果产品没有属性,那么它将不在orders_products_attributes表中,因此订单ID在这些产品上返回NULL,这意味着它们不会被后面的代码处理。
答案 0 :(得分:0)
我会稍微调整一下。首先,从您想要包含的所有可能订单的预先查询开始。确保您有一个索引来帮助优化。我会索引(ExportedToTradeBox,Orders_ID)。
所以,现在我有一个“OnlyThese”的预查询结果集。现在,加入产品,因为所有订单都有产品,但所有产品都不需要属性选项。
然后,对订单产品属性表执行LEFT-JOIN,但仅对加入它们的键执行(确保正确的订单和产品)。现在,您可以查询所有合格订单,并加入最终产品属性表,无论是否具有属性。
最后,将WHERE子句应用为NULL OR或者是您期望的类型之一。这样,它会查看每个项目并询问。如果没有选项,请允许它...但是,如果它有一个属性,我只想要那些选项为1或2(通过IN(1,2)子句)。
SELECT
* (actual column list you want)
FROM
( SELECT DISTINCT o.orders_id
FROM orders o
WHERE o.ExportedToTradebox = 0
AND o.orders_id > 22000) OnlyThese
JOIN orders_products op
ON OnlyThese.orders_id = op.orders_id
LEFT JOIN orders_products_attributes oa
ON op.Orders_ID = oa.Orders_ID
AND op.orders_products_id = oa.orders_products_id
WHERE
oa.products_options_id IS NULL
OR oa.products_options_id IN ( 1, 2 )