复杂的MySQL查询问题

时间:2016-03-16 11:19:05

标签: mysql

我有两个具有以下结构的表:

表1: ap_form_payments

CREATE TABLE IF NOT EXISTS ap_form_payments (
    afp_id int(11) unsigned NOT NULL AUTO_INCREMENT,
    form_id int(11) unsigned NOT NULL,
    record_id int(11) unsigned NOT NULL,
    payment_id varchar(255) DEFAULT NULL,
    date_created datetime DEFAULT NULL,
    payment_date datetime DEFAULT NULL,
    payment_status varchar(255) DEFAULT NULL,
    payment_fullname varchar(255) DEFAULT NULL,
    payment_amount decimal(62 , 2 ) NOT NULL DEFAULT '0.00',
    payment_currency varchar(3) NOT NULL DEFAULT 'usd',
    payment_test_mode int(1) NOT NULL DEFAULT '0',
    payment_merchant_type varchar(25) DEFAULT NULL,
    status int(1) NOT NULL DEFAULT '1',
    billing_street varchar(255) DEFAULT NULL,
    billing_city varchar(255) DEFAULT NULL,
    billing_state varchar(255) DEFAULT NULL,
    billing_zipcode varchar(255) DEFAULT NULL,
    billing_country varchar(255) DEFAULT NULL,
    same_shipping_address int(1) NOT NULL DEFAULT '1',
    shipping_street varchar(255) DEFAULT NULL,
    shipping_city varchar(255) DEFAULT NULL,
    shipping_state varchar(255) DEFAULT NULL,
    shipping_zipcode varchar(255) DEFAULT NULL,
    shipping_country varchar(255) DEFAULT NULL,
    payment_fee decimal(62 , 2 ) NOT NULL DEFAULT '0.00',
    payment_method varchar(255) DEFAULT NULL,
    PRIMARY KEY (afp_id),
    KEY form_id (form_id , record_id)
)  ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

表2: ap_form_10242

CREATE TABLE IF NOT EXISTS ap_form_10242 (
    id int(11) NOT NULL AUTO_INCREMENT,
    date_created datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
    date_updated datetime DEFAULT NULL,
    ip_address varchar(15) DEFAULT NULL,
    status int(4) unsigned NOT NULL DEFAULT '1',
    resume_key varchar(10) DEFAULT NULL,
    element_1 text COMMENT 'Single Line Text',
    element_2 decimal(62 , 2 ) DEFAULT NULL COMMENT 'Price',
    element_3 varchar(11) NOT NULL DEFAULT '0' COMMENT 'Drop Down',
    PRIMARY KEY (id),
    KEY ip_address (ip_address),
    KEY date_created (date_created)
)  ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

两个表都有以下数据:

ap_form_payments的数据

INSERT INTO `ap_form_payments` (`afp_id`, `form_id`, `record_id`, `payment_id`, `date_created`, `payment_date`, `payment_status`, `payment_fullname`, `payment_amount`, `payment_currency`, `payment_test_mode`, `payment_merchant_type`, `status`, `billing_street`, `billing_city`, `billing_state`, `billing_zipcode`, `billing_country`, `same_shipping_address`, `shipping_street`, `shipping_city`, `shipping_state`, `shipping_zipcode`, `shipping_country`, `payment_fee`, `payment_method`) VALUES
(1, 10242, 1, '787878', '2016-03-03 00:00:00', '2016-03-23 00:00:00', 'paid', 'SSSS', '14.00', 'usd', 0, NULL, 1, 'OOO', 'OOO', 'OOO', '1212', 'USA', 1, NULL, NULL, NULL, NULL, NULL, '0.00', 'Credit Card'),
(2, 10242, 1, '787878', '2016-03-03 00:00:00', '2016-03-23 00:00:00', 'paid', 'SSSS', '14.00', 'usd', 0, NULL, 1, 'OOO', 'OOO', 'OOO', '1212', 'USA', 1, NULL, NULL, NULL, NULL, NULL, '0.00', 'Credit Card'),
(3, 10242, 1, '787878', '2016-03-03 00:00:00', '2016-03-23 00:00:00', 'paid', 'SSSS', '14.00', 'usd', 0, NULL, 1, 'OOO', 'OOO', 'OOO', '1212', 'USA', 1, NULL, NULL, NULL, NULL, NULL, '0.00', 'Credit Card');

ap_form_10242的数据

INSERT INTO `ap_form_10242` (`id`, `date_created`, `date_updated`, `ip_address`, `status`, `resume_key`, `element_1`, `element_2`, `element_3`) VALUES
(1, '2016-03-11 11:23:42', NULL, '127.0.0.1', 1, NULL, 'oo', '12.00', '1'),
(2, '2016-03-11 11:23:42', NULL, '127.0.0.1', 1, NULL, 'oo', '12.00', '1'),
(3, '2016-03-11 11:23:42', NULL, '127.0.0.1', 1, NULL, 'oo', '12.00', '1');

当我使用以下列名称('payment_amount','payment_status','payment_id')运行以下查询时,一切正常,但如果我使用这两个列名称('payment_fee','payment_method'),我会得到错误

查询失败:

select 
    id,
    id as row_num,
    date_created,
    (select 
            payment_amount
        from
            ap_form_payments
        where
            form_id = '10242' and record_id = A.id
        order by afp_id desc
        limit 1) payment_amount,
    ifnull((select 
                    payment_status
                from
                    ap_form_payments
                where
                    form_id = '10242' and record_id = A.id
                order by afp_id desc
                limit 1),
            'unpaid') payment_status,
    (select 
            payment_id
        from
            ap_form_payments
        where
            form_id = '10242' and record_id = A.id
        order by afp_id desc
        limit 1) payment_id
from
    ap_form_10242 A
WHERE
    status = 1
order by payment_fee asc
LIMIT 0 , 15

错误我得到:Query Failed: SQLSTATE[42S22]: Column not found: 1054 column 'payment_method' unknown in order clause

通过的查询:

select 
    id,
    id as row_num,
    date_created,
    (select 
            payment_amount
        from
            ap_form_payments
        where
            form_id = '10242' and record_id = A.id
        order by afp_id desc
        limit 1) payment_amount,
    ifnull((select 
                    payment_status
                from
                    ap_form_payments
                where
                    form_id = '10242' and record_id = A.id
                order by afp_id desc
                limit 1),
            'unpaid') payment_status,
    (select 
            payment_id
        from
            ap_form_payments
        where
            form_id = '10242' and record_id = A.id
        order by afp_id desc
        limit 1) payment_id
from
    ap_form_10242 A
WHERE
    status = 1
order by payment_id asc
LIMIT 0 , 15

有人可以帮我解决这个问题吗?

感谢。

1 个答案:

答案 0 :(得分:0)

实际错误为Unknown column 'payment_fee' in 'order clause'

原因是payment_fee不在表ap_form_10242中或任何已命名的子查询结果中。您可以按iddate_createddate_updatedip_addressstatusresume_keyelement_1,{{1}订购},element_2element_3payment_amountpayment_status

要按其他表格中的值排序,您需要加入。也许是这样的

payment_id