2011-07-20 25 views
0

我有一個SQL查詢來選擇枚舉列並執行連接。下面是數據庫表參與和相關數據:在枚舉列上的SQL連接返回所有可能的值

OFFER表

-- 
-- Table structure for table `offer` 
-- 

CREATE TABLE `offer` (
    `id` int(11) NOT NULL auto_increment, 
    `companyID` int(11) NOT NULL, 
    `categoryID` int(11) NOT NULL, 
    `dateAdded` timestamp NOT NULL default CURRENT_TIMESTAMP, 
    `details` text NOT NULL, 
    `amount` decimal(11,0) NOT NULL, 
    `maxAmount` decimal(11,0) NOT NULL default '0', 
    `dateExpires` int(11) NOT NULL default '0', 
    `active` enum('YES','NO') NOT NULL, 
    `featured` enum('YES','NO') NOT NULL default 'NO', 
    `adType` enum('OFFER','URL') NOT NULL default 'OFFER', 
    `URL` varchar(255) NOT NULL, 
    `address1` varchar(50) NOT NULL, 
    `address2` varchar(50) NOT NULL, 
    `city` varchar(50) NOT NULL, 
    `state` char(2) NOT NULL, 
    `zip` varchar(10) NOT NULL, 
    PRIMARY KEY (`id`), 
    KEY `companyID` (`companyID`), 
    KEY `categoryID` (`categoryID`), 
    KEY `zip` (`zip`) 
) ENGINE=MyISAM AUTO_INCREMENT=66 DEFAULT CHARSET=utf8 AUTO_INCREMENT=66 ; 

-- 
-- Dumping data for table `offer` 
-- 

INSERT INTO `offer` (`id`, `companyID`, `categoryID`, `dateAdded`, `details`, `amount`, `maxAmount`, `dateExpires`, `active`, `featured`, `adType`, `URL`, `address1`, `address2`, `city`, `state`, `zip`) VALUES 
(24, 10, 6, '2011-05-19 14:55:54', 'Computer Software & Books', 25, 0, 0, 'NO', 'NO', 'OFFER', '', '912 N. Avenue 57', '', 'Los Angeles', 'CA', '90043') 

付款表

-- 
-- Table structure for table `payments` 
-- 

CREATE TABLE `payments` (
    `id` int(11) NOT NULL auto_increment, 
    `date` timestamp NOT NULL default CURRENT_TIMESTAMP, 
    `offerID` int(11) NOT NULL, 
    `amount` decimal(11,2) NOT NULL, 
    `memberID` int(11) NOT NULL, 
    `validationNumber` varchar(20) NOT NULL, 
    `status` enum('NEW','USED') NOT NULL default 'NEW', 
    `dateRedeemed` datetime NOT NULL, 
    PRIMARY KEY (`id`), 
    KEY `validationNumber` (`validationNumber`), 
    KEY `offerID` (`offerID`), 
    KEY `memberID` (`memberID`) 
) ENGINE=MyISAM AUTO_INCREMENT=33 DEFAULT CHARSET=utf8 AUTO_INCREMENT=33 ; 

-- 
-- Dumping data for table `payments` 
-- 

INSERT INTO `payments` (`id`, `date`, `offerID`, `amount`, `memberID`, `validationNumber`, `status`, `dateRedeemed`) VALUES 
(1, '2011-03-28 16:33:24', 24, 11.00, 8, '1A2B3-1', 'NEW', '2011-07-18 15:00:41'), 
(2, '2011-04-12 12:47:58', 16, 81.10, 8, '2C3D4-2', 'NEW', '0000-00-00 00:00:00'), 
(3, '2011-05-19 19:50:58', 24, 22.15, 14, 'ABCDE-3', 'USED', '2011-07-18 15:03:00'), 
(4, '2011-05-19 19:50:58', 24, 44.30, 5, 'FGHIJK-4', 'USED', '2011-07-18 15:03:45'); 

事務表

-- 
-- Table structure for table `transactions` 
-- 

CREATE TABLE `transactions` (
    `id` int(11) NOT NULL auto_increment, 
    `companyID` int(11) NOT NULL, 
    `offerID` int(11) NOT NULL, 
    `date` timestamp NOT NULL default CURRENT_TIMESTAMP, 
    `amount` decimal(10,2) NOT NULL, 
    `type` enum('CREDIT','DEBIT') NOT NULL, 
    PRIMARY KEY (`id`), 
    KEY `companyID` (`companyID`,`offerID`) 
) ENGINE=MyISAM AUTO_INCREMENT=38 DEFAULT CHARSET=utf8 AUTO_INCREMENT=38 ; 

-- 
-- Dumping data for table `transactions` 
-- 

INSERT INTO `transactions` (`id`, `companyID`, `offerID`, `date`, `amount`, `type`) VALUES 
(1, 10, 24, '2011-05-20 11:25:35', 11.00, 'CREDIT'), 
(2, 10, 24, '2011-05-20 11:25:35', 22.15, 'CREDIT'), 
(3, 10, 24, '2011-05-20 11:26:11', 44.30, 'CREDIT'), 
(4, 10, 0, '2011-05-20 14:55:13', -3.50, 'DEBIT'), 
(5, 10, 0, '2011-05-20 14:59:50', -5.00, 'DEBIT'), 
(6, 10, 0, '2011-05-20 15:45:29', -4.50, 'DEBIT'), 
(36, 10, 0, '2011-07-08 15:03:06', -10.00, 'DEBIT'), 
(37, 10, 0, '2011-07-08 15:03:19', -2.45, 'DEBIT'); 

這裏的查詢:

SELECT DISTINCT t.*, o.amount AS discount, p.status FROM transactions t 
      LEFT JOIN offer o ON o.id = t.offerID 
      LEFT JOIN payments p ON p.offerId = t.offerID 
      WHERE t.companyID = '10' ORDER BY date ASC 

最後,這裏的結果集:

id companyID offerID  date amount type discount status 
1 10 24 2011-05-20 11:25:35  11.00 CREDIT 25 NEW 
1 10 24 2011-05-20 11:25:35  11.00 CREDIT 25 USED 
2 10 24 2011-05-20 11:25:35  22.15 CREDIT 25 NEW 
2 10 24 2011-05-20 11:25:35  22.15 CREDIT 25 USED 
3 10 24 2011-05-20 11:26:11  44.30 CREDIT 25 USED 
3 10 24 2011-05-20 11:26:11  44.30 CREDIT 25 NEW 
4 10 0 2011-05-20 14:55:13  -3.50 DEBIT NULL NULL 
5 10 0 2011-05-20 14:59:50  -5.00 DEBIT NULL NULL 
6 10 0 2011-05-20 15:45:29  -4.50 DEBIT NULL NULL 
36 10 0 2011-07-08 15:03:06  -10.00 DEBIT NULL NULL 
37 10 0 2011-07-08 15:03:19  -2.45 DEBIT NULL NULL 

的問題是,信貸的結果與新的和用過枚舉值出現兩次。我只想要將付款行設置爲的枚舉值。如果有人能幫我理解爲什麼這個查詢不起作用,以及如何解決它,我將不勝感激。提前致謝!

@邁克爾,@chopikadze:

我已經加入到MEMBERID交易表。 我已經修改了我這樣的查詢:

SELECT t. * , o.amount AS discount, p.status 
FROM transactions t 
LEFT JOIN offer o ON o.id = t.offerID 
LEFT JOIN payments p ON p.offerId = t.offerID 
LEFT JOIN members m ON m.id = t.memberID 
WHERE t.companyID = '10' 
ORDER BY date ASC 

這是我的新的結果集:

id companyID offerID  memberID date amount type discount status 
1 10 24 8 2011-05-20 11:25:35  11.00 CREDIT 25 NEW 
1 10 24 8 2011-05-20 11:25:35  11.00 CREDIT 25 USED 
1 10 24 8 2011-05-20 11:25:35  11.00 CREDIT 25 USED 
2 10 24 14 2011-05-20 11:25:35  22.15 CREDIT 25 NEW 
2 10 24 14 2011-05-20 11:25:35  22.15 CREDIT 25 USED 
2 10 24 14 2011-05-20 11:25:35  22.15 CREDIT 25 USED 
3 10 24 5 2011-05-20 11:26:11  44.30 CREDIT 25 NEW 
3 10 24 5 2011-05-20 11:26:11  44.30 CREDIT 25 USED 
3 10 24 5 2011-05-20 11:26:11  44.30 CREDIT 25 USED 
4 10 0 0 2011-05-20 14:55:13  -3.50 DEBIT NULL NULL 
5 10 0 0 2011-05-20 14:59:50  -5.00 DEBIT NULL NULL 
6 10 0 0 2011-05-20 15:45:29  -4.50 DEBIT NULL NULL 
36 10 0 0 2011-07-08 15:03:06  -10.00 DEBIT NULL NULL 
37 10 0 0 2011-07-08 15:03:19  -2.45 DEBIT NULL NULL 

看來,加入MEMBERID導致多個重複的結果。我做錯了什麼?請糾正我在這一個傢伙。謝謝。

+0

爲什麼在給定報價的'payments'表中有兩條記錄?我可以理解存儲歷史,但你似乎也有'redeemedDate',如果'NEW'記錄永遠不會使用它,這很奇怪。我可能只是在將'payments'記錄改爲'USED'而不是插入第二條記錄 –

+0

@Michael時,我認爲結果集讓您感到困惑。每個優惠只有一個付款記錄。最初,該記錄具有新的狀態。一旦付款被兌換後,狀態將更改爲USED。但是在結果集中,兩個狀態值都出現在我不想要的地方。我想要付款記錄的當前狀態。我希望這是有道理的。謝謝。 –

+0

付款如何映射到交易?你需要指定在你的加入條件 - 這可能不僅僅是offerid –

回答

0
SELECT t. * , o.amount AS discount, p.status 
FROM transactions t 
    LEFT JOIN offer o ON o.id = t.offerID 
    LEFT JOIN payments p ON p.offerId = t.offerID and p.memberID = t.memberID 
WHERE t.companyID = '10' 
ORDER BY date ASC 

正如我說,你不應該只

MEMBERID字段添加到事務表

而且

加入交易和支付不僅OFFERID,但在memberID上也是

+0

或只是加入付款金額和交易金額,如果有可能(只是注意到他們是平等的),但它取決於邏輯規則在db – ksogor

+0

@chopikadze:這個伎倆!謝謝。 –