0

ここから、必要なSQLクエリについてアドバイスがありましたが、属性が割り当てられていない製品に遭遇すると、誤った結果が返されるようになりました。関連するdbテーブル/データ(クライアント情報を削除するためにサニタイズされたデータ)のサンプルとともに、以下のクエリをリストします。

クエリ:

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が問題の原因です。注文されたウォールチャートには属性がありませんが、dbが照会されると、注文内の他のアイテムにリストされている属性が適用されます。

関連データのスキーマはhttp://sqlfiddle.com/#!2/f0003 にあります。そこには注文番号23111のデータのみが含まれています。

クエリは結果をxmlファイルに渡します。結果を以下に示します。

1カスタムビルドA5ティーチャープランナー|ネイビー13.902.951カスタムビルドA5ティーチャープランナー
| DBI3.300.001
カスタムビルドA5ティーチャープランナー|ネイビー
13.902.951ノートブック(NB)|ネイビー3.50 0.00
1ノートブック(NB)|
DBI3.500.001ノートブック(NB)|ネイビー3.500.001
ウォールチャート2013-2014|ネイビー0.900.001
ウォールチャート2013-2014|DBI 0.900.001
ウォールチャート2013-2014|ネイビー0.900.00

あるべき姿:

1カスタムビルドA5ティーチャープランナー|ネイビー13.902.951
カスタムビルドA5ティーチャープランナー|DBI3.30 0.00
1 NotesBook(NB)|ネイビー3.500.001
ウォールチャート2013-20140.900.00

これについて助けてくれてありがとう。

DRappはこれに対してほぼ機能するソリューションを提供しましたが、クエリは正しい結果を返しますが、使用中は、結果からorders_idを呼び出すと、属性のない製品の注文IDが得られないという問題が発生します。これは、テーブルの結合方法が原因である可能性があります。商品に属性がない場合、その商品はorders_products_attributesテーブルに含まれないため、これらの商品では注文IDがNULLを返します。これは、次のコードで商品が処理されないことを意味します。

4

1 に答える 1

0

少し調整します。まず、含めたいすべての可能な注文の事前クエリから始めます。最適化に役立つインデックスがあることを確認してください。(ExportedToTradeBox, Orders_ID) にインデックスを付けます。

これで、"OnlyThese" のプレクエリ結果セットができました。ここで、すべての注文に製品があるため、製品に結合しますが、すべての製品に属性オプションは必要ありません。

次に、注文商品属性テーブルに LEFT-JOIN を実行しますが、それらを結合するキーに対してのみ行います (適切な注文と商品を保証します)。これで、属性の有無に関係なく、すべての適格な注文のクエリが作成され、最終的な製品属性テーブルに結合されました。

最後に、WHERE 句を NULL または予想される型の 1 つとして適用します。このように、各項目を見て質問します。オプションがない場合は許可します...ただし、属性がある場合は、(IN (1,2) 句を介して) 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 )
于 2013-01-28T12:10:06.593 に答える