我正在努力实现案例3 :
案例1 :客户已注册,但尚未下订单。该查询给出了案例1的结果。
SELECT c.customers_id,
c.customers_firstname,
c.customers_lastname,
c.customers_email_address,
c.customers_telephone,
c.customers_fax,
ab.entry_street_address,
ab.entry_city,
ab.entry_state,
ab.entry_country_id,
ab.entry_postcode
FROM customers c
JOIN address_book ab
ON c.customers_id = ab.customers_id
JOIN customers_info ci
ON c.customers_id = ci.customers_info_id
WHERE ci.customers_info_date_account_created BETWEEN
'2014-10-25' AND '2015-10-10'
案例2:客户已注册并已下订单。此查询从订单表获取其他交付信息。以下查询适用于案例2。
SELECT c.customers_id,
c.customers_firstname,
c.customers_lastname,
c.customers_email_address,
c.customers_telephone,
c.customers_fax,
ab.entry_street_address,
ab.entry_city,
ab.entry_state,
ab.entry_country_id,
ab.entry_postcode,
o.delivery_street_address,
o.delivery_city,
o.delivery_state,
o.delivery_country,
o.delivery_postcode
FROM customers c
JOIN address_book ab
ON c.customers_id = ab.customers_id
JOIN orders o
ON c.customers_id = o.customers_id
WHERE o.date_purchased BETWEEN '2014-10-25' AND '2015-10-10'
案例3(我想要实现的目标):我想让所有客户在特定时间段内注册使用
ci.customers_info_date_account_created BETWEEN' 2014-10-25'和' 2015-10-10'
另外,我想检查客户是否使用o.date_purchased BETWEEN' 2014-10-25'和' 2015-10-10'。如果下订单,则从订单表中获取所有交货信息。如果没有下订单,则将这些列留空。
o.delivery_street_address,o.delivery_city,o.delivery_state,o.delivery_country,o.delivery_postcode
答案 0 :(得分:0)
将JOIN orders o
更改为LEFT JOIN orders o
(将填充NULL行),然后将您的条件更改为
WHERE (o.date_purchased BETWEEN '2014-10-25' AND '2015-10-10' OR o.customers_id IS NULL)
这会找到任何在该时间范围内有订单的人,或者尚未下订单的客户。
答案 1 :(得分:0)
SELECT c.customers_id,
c.customers_firstname,
c.customers_lastname,
c.customers_email_address,
c.customers_telephone,
c.customers_fax,
ab.entry_street_address,
ab.entry_city,
ab.entry_state,
ab.entry_country_id,
ab.entry_postcode,
o.delivery_street_address, o.delivery_city, o.delivery_state, o.delivery_country, o.delivery_postcode
FROM customers c
INNER JOIN address_book ab
ON c.customers_id = ab.customers_id
INNER JOIN customers_info ci
ON c.customers_id = ci.customers_info_id
LEFT JOIN (select * from orders where date_purchased BETWEEN '2014-10-25' AND '2015-10-10') o
ON c.customers_id = o.customers_id
WHERE ci.customers_info_date_account_created BETWEEN
'2014-10-25' AND '2015-10-10'
您可以在子查询中列出字段名称而不是*。
答案 2 :(得分:0)
使用LEFT JOIN
。这将从第一个表中返回第二个表中没有任何匹配项的行。
SELECT c.customers_id,
c.customers_firstname,
c.customers_lastname,
c.customers_email_address,
c.customers_telephone,
c.customers_fax,
ab.entry_street_address,
ab.entry_city,
ab.entry_state,
ab.entry_country_id,
ab.entry_postcode,
o.delivery_street_address,
o.delivery_city,
o.delivery_state,
o.delivery_country,
o.delivery_postcode
FROM customers c
JOIN address_book ab
ON c.customers_id = ab.customers_id
JOIN customer_info ci
ON c.customers_id = ci.customers_info_id
LEFT JOIN orders o
ON c.customers_id = o.customers_id AND o.date_purchased BETWEEN '2014-10-25' AND '2015-10-10'
WHERE ci.customers_info_date_account_created BETWEEN '2014-10-25' AND '2015-10-10'
o.date_purchased
上的配置被移入ON
子句,因为从不匹配的行返回的NULL
值与BETWEEN
子句不匹配。