0

是否可以将这两个查询组合成一个查询?

第一个查询:

SELECT products_options_values, options_values_price, orders_id
FROM orders_products_attributes oa
WHERE (products_options_id = 5 OR products_options_id = 1 OR products_options_id = 2)
AND oa.orders_id
IN (
SELECT DISTINCT o.orders_id
FROM orders o
WHERE o.ExportedToTradebox =  0
AND o.orders_id >  22000)

第二个查询:

SELECT * FROM orders_products
WHERE orders_id 
IN(
SELECT DISTINCT orders_id 
FROM orders 
WHERE ExportedToTradebox='0' 
and orders_id >'22000')

我已经尝试了几次,我只是得到了 phpMyAdmin 报告的 SQL 错误 对不起,如果它很简单,我的 sql 知识非常不完整。

组合起来,以便在运行时将所有必需的信息放入一个数组中。我试图从它生成的两个单独的数组中使用它,但是程序中的 php 代码设计不佳,带有多个 while 循环和 else-if 语句。尝试引入另一个 while-MoveNext 循环只会使一切崩溃。我想如果所有数据都可以收集到原始数组中,那么回显新字段就足够简单了。

相关数据库表结构如下:

订单表:

CREATE TABLE IF NOT EXISTS `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=23075 ;

订单属性:

CREATE TABLE IF NOT EXISTS `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,
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=77065 ;

订购产品:

CREATE TABLE IF NOT EXISTS `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=34938 ;
4

3 回答 3

1
SELECT *
FROM orders_products op
LEFT JOIN orders_products_attributes oa
ON op.orders_id = oa.orders_id
WHERE (oa.products_options_id = 5 OR oa.products_options_id = 1 OR oa.products_options_id = 2)
AND oa.orders_id
IN (
SELECT DISTINCT o.orders_id
FROM orders o
WHERE o.ExportedToTradebox =  0
AND o.orders_id >  22000)

这将在一个结果集中为您提供这两个表中的所有列。我使用了左连接,因此如果 orders_products 记录没有属性,那么它仍然会被返回。

您可能面临的一个问题是,如果每个订单 ID 有多个,orders_product_attribute那么您将复制 orders_products 记录。您可能需要查看某种转置函数(我不确定如何在 MySQL 中执行此操作,或者是否可能)。

于 2013-01-08T23:53:42.463 回答
1
SELECT *  --You can select your own columns later
    FROM orders_products_attributes oa
         RIGHT OUTER JOIN orders_products op
             ON oa.orders_products_id = op.orders_products_id
                 AND products_options_id IN (5, 1, 2)
    WHERE op.orders_id IN (
            SELECT DISTINCT o.orders_id
                FROM orders o
                WHERE o.ExportedToTradebox =  0
                    AND o.orders_id >  22000
        )

假设您的 order_products_ids 引用相同的信息,这将使您在一个表中获得所有内容。每个属性都有一行,其中 Order_Products 数据重复。如果您想将信息串联在一起,您可以随时尝试 GROUP_CONCAT:

SELECT op.orders_products_id
        , GROUP_CONCAT(
          oa.products_options_id 
          ORDER BY CASE oa.products_options_id
              WHEN 5 THEN 1
              WHEN 1 THEN 2
              WHEN 2 THEN 3
              ELSE 4
          END ASC
          SEPARATOR '::'
        ) AS Options
    FROM orders_products_attributes oa
        RIGHT OUTER JOIN orders_products op
            ON oa.orders_products_id = op.orders_products_id
                AND oa.products_options_id IN (5, 1, 2)
    WHERE op.orders_id IN (
            SELECT DISTINCT o.orders_id
                FROM orders o
                WHERE o.ExportedToTradebox =  0
                    AND o.orders_id >  22000
        )
    GROUP BY op.orders_products_id

;

这会给你一个逗号分隔的列表。只是有点取决于你需要什么。

于 2013-01-09T00:01:19.517 回答
0

我认为您正在寻找的是 UNION ALL:

SELECT products_options_values, options_values_price, orders_id 
FROM orders_products_attributes oa 
WHERE (products_options_id = 5 OR products_options_id = 1 OR products_options_id = 2) AND oa.orders_id IN ( SELECT DISTINCT o.orders_id FROM orders o WHERE o.ExportedToTradebox = 0 AND o.orders_id > 22000)
UNION ALL
SELECT products_options_values, options_values_price, order_number as orders_id 
FROM orders_products WHERE orders_id IN( SELECT DISTINCT orders_id FROM orders 
WHERE ExportedToTradebox='0' and orders_id >'22000')

请注意,我在第二个查询中命名了字段,以便与第一个查询中的名称完全匹配。

于 2013-01-08T23:29:13.720 回答