我在第12章中使用Murach的MySql。我需要做的是:
*创建一个名为customer_addresses的视图,显示送货和结算 每个客户的地址。 此视图应从Customers表返回这些列:customer_id, email_address,last_name和first_name。 此视图应从Addresses表中返回这些列:bill_line1, bill_line2,bill_city,bill_state,bill_zip,ship_line1,ship_line2,ship_city,ship_state, 和ship_zip。 此视图中的行应按last_name和first_name排序 列。
问题在于所有" bill"和"船舶"需要根据customers表中的billing_address_id或shipping_address_id是否与addresses表中的address_id列匹配来创建列。我不知道如何去做这件事。我一直在玩if语句而没有运气。任何人都有和如何让这个工作的想法?
这是我的代码留下的内容,在没有运气的情况下将其分开几次:
CREATE OR REPLACE VIEW customer_addresses
AS
SELECT c.customer_id, email_address, last_name,
first_name,
IF (billing_address_id = address_id, line1, (AS ship_line1)) AS bill_line1,
IF (billing_address_id = address_id, line2, '') AS bill_line2,
IF (billing_address_id = address_id, city, '') AS bill_city,
IF (billing_address_id = address_id, state, '') AS bill_state,
IF (billing_address_id = address_id, zip_code, '') AS bill_zip,
FROM customers c JOIN addresses a
ON c.customer_id = a.customer_id
WHERE shipping_address_id = address_id
ORDER BY last_name, first_name;