获取日期和null之间的数据

时间:2015-03-13 11:26:35

标签: mysql date join

我必须获得客户的正确数据,这些数据可能分别过时而不是最新数据。

表:

Customer
+---------------------------+---------------+------+-----+---------+
| Field                     | Type          | Null | Key | Default |
+---------------------------+---------------+------+-----+---------+
| customer_id               | int(11)       | NO   | PRI | NULL    |
| customer_data             | varchar(255)  | NO   |     | NULL    |
| customer_valid_from       | datetime      | NO   |     | NULL    |
| customer_valid_till       | datetime      | YES  |     | NULL    |
+---------------------------+---------------+------+-----+---------+

Address
+---------------------------+---------------+------+-----+---------+
| Field                     | Type          | Null | Key | Default |
+---------------------------+---------------+------+-----+---------+
| address_id                | int(11)       | NO   | PRI | NULL    |
| address_street            | varchar(255)  | NO   |     | NULL    |
| address_valid_from        | datetime      | NO   |     | NULL    |
| address_valid_till        | datetime      | YES  |     | NULL    |
| customer_id               | int(11)       | NO   |     | NULL    |
+---------------------------+---------------+------+-----+---------+

Details
+---------------------------+---------------+------+-----+---------+
| Field                     | Type          | Null | Key | Default |
+---------------------------+---------------+------+-----+---------+
| detail_id                 | int(11)       | NO   | PRI | NULL    |
| detail_street             | varchar(255)  | NO   |     | NULL    |
| detail_valid_from         | datetime      | NO   |     | NULL    |
| detail_valid_till         | datetime      | YES  |     | NULL    |
| customer_id               | int(11)       | NO   |     | NULL    |
+---------------------------+---------------+------+-----+---------+

一开始我得到了一个日期和customer_id。 现在我想选择所有数据,这些数据与客户在日期的给定customer_id有关,也是给定的。

我的SQL目前看起来像这样:

SELECT customer.*,address.*,details.*
FROM customer
JOIN address
ON address.customer_id = customer.customer_id
JOIN details
ON details.customer.customer_id = customer.customer.id
WHERE customer.customer_valid_from <= givenDate
AND customer.customer_valid_till >= givenDate
AND address.address_valid_from <= givenDate
AND address.address_valid_till >= givenDate
AND details.details_valid_from <= givenDate
AND details.details_valid_till >= givenDate
customer.customer_id = givenID;

在我看来,有一部分缺失。 如果记录与valid_from&lt; = givenDate匹配但具有valid_till,即NULL或&#34; 0000-00-00 00:00:00&#34;?

我考虑过IF或CASE子句,但我对MySQL的经验并不令人满意。

2 个答案:

答案 0 :(得分:2)

试试这个from条款:

WHERE customer.customer_valid_from <= givenDate AND
      (customer.customer_valid_till >= givenDate OR
       customer.customer_valid_till IS NULL OR
       customer.customer_valid_till  = '0000-00-00 00:00:00'
      ) AND
      address.address_valid_from <= givenDate AND
      (address.address_valid_till >= givenDate OR
       address.address_valid_till IS NULL OR
       address.address_valid_till = '0000-00-00 00:00:00'
      ) AND
      details.details_valid_from <= givenDate AND
      (details.details_valid_till >= givenDate OR
       details.details_valid_till IS NULL OR
       details.details_valid_till = '0000-00-00 00:00:00'
      )

它只是添加了你似乎要求的逻辑。

答案 1 :(得分:0)

SELECT customer.*,address.*,details.*
FROM customer
JOIN address
ON address.customer_id = customer.customer_id
JOIN details
ON details.customer.customer_id = customer.customer.id
WHERE customer.customer_valid_from <= givenDate
AND customer.customer_valid_till >= givenDate
AND address.address_valid_from <= givenDate
AND address.address_valid_till >= givenDate
AND details.details_valid_from <= givenDate
AND (details.details_valid_till >= givenDate OR details.details_valid_till IS NULL OR details.details_valid_till = '0000-00-00 00:00:00')
customer.customer_id = givenID;