MYSQL并非显示所有插入的值

时间:2017-11-07 21:23:45

标签: mysql

我应该能够在一个SQL SELECT语句中显示客户名称,宠物名称,过程,成本和总成本。我只获取一行值,其中一个值为NULL。我无法弄清楚我的SQL语句或表是什么问题导致它不返回所有输入的值。 这是我得到的结果的图像: enter image description here

CREATE DATABASE IF NOT EXISTS vet;
USE vet;

CREATE TABLE IF NOT EXISTS customer (
    customer_id INT PRIMARY KEY AUTO_INCREMENT,
    customer_name VARCHAR(70)
    );

CREATE TABLE IF NOT EXISTS invoice (
    invoice_id INT PRIMARY KEY AUTO_INCREMENT,
    invoice_date DATE,
    customer_id INT ,
    CONSTRAINT FOREIGN KEY (customer_id) REFERENCES customer(customer_id)
    ON DELETE CASCADE
    ON UPDATE CASCADE
);

CREATE TABLE IF NOT EXISTS pet (
    pet_id INT PRIMARY KEY AUTO_INCREMENT,
    pet_name VARCHAR(50),
    customer_id INT,
    CONSTRAINT FOREIGN KEY (customer_id) REFERENCES customer(customer_id)
    ON DELETE CASCADE
    ON UPDATE CASCADE
);

CREATE TABLE IF NOT EXISTS procedures (
    procedure_id INT PRIMARY KEY AUTO_INCREMENT,
    procedure_name VARCHAR(70),
    amount DECIMAL
);

CREATE TABLE IF NOT EXISTS invoice_pet (
    invoice_id INT,
    pet_id INT,
    CONSTRAINT FOREIGN KEY (invoice_id) REFERENCES invoice(invoice_id),
    CONSTRAINT FOREIGN KEY (pet_id) REFERENCES pet(pet_id)
    ON DELETE CASCADE
    ON UPDATE CASCADE
);

CREATE TABLE IF NOT EXISTS procedures_pet (
    procedure_id INT,
    pet_id INT,
    CONSTRAINT FOREIGN KEY (procedure_id) REFERENCES procedures(procedure_id),
    CONSTRAINT FOREIGN KEY (pet_id) REFERENCES pet(pet_id)
    ON DELETE CASCADE
    ON UPDATE CASCADE
);


INSERT INTO invoice (invoice_date) VALUES ('2004-04-05');
INSERT INTO invoice (invoice_date) VALUES ('2014-12-05');
INSERT INTO invoice (invoice_date) VALUES ('2009-08-29');
INSERT INTO invoice (invoice_date) VALUES ('2016-07-15');

INSERT INTO customer (customer_name) VALUES ('John Garett');
INSERT INTO customer (customer_name) VALUES ('Mary Wist');
INSERT INTO customer (customer_name) VALUES ('Beth Smith');
INSERT INTO customer (customer_name) VALUES ('Rick Sanchez');

INSERT INTO pet (pet_name, customer_id) VALUES ('Rover', 1);
INSERT INTO pet (pet_name, customer_id) VALUES ('Max', 3);
INSERT INTO pet (pet_name, customer_id) VALUES ('Munchie', 4);
INSERT INTO pet (pet_name, customer_id) VALUES ('Dixon', 2);
INSERT INTO pet (pet_name, customer_id) VALUES ('Lucky', 4);

INSERT INTO procedures (procedure_name, amount) VALUES ('Rabies Vaccination', 30.00);
INSERT INTO procedures (procedure_name, amount) VALUES ('Sterilization', 190.00);
INSERT INTO procedures (procedure_name, amount) VALUES ('Dental Surgery', 120.00);
INSERT INTO procedures (procedure_name, amount) VALUES ('Cystotomy', 200.00);

INSERT INTO invoice_pet (invoice_id, pet_id) VALUES (1, 1);
INSERT INTO invoice_pet (invoice_id, pet_id) VALUES (2, 1);
INSERT INTO invoice_pet (invoice_id, pet_id) VALUES (1, 4);
INSERT INTO invoice_pet (invoice_id, pet_id) VALUES (3, 2);

INSERT INTO procedures_pet (procedure_id, pet_id) VALUES (1, 1);
INSERT INTO procedures_pet (procedure_id, pet_id) VALUES (3, 2);
INSERT INTO procedures_pet (procedure_id, pet_id) VALUES (4, 4);
INSERT INTO procedures_pet (procedure_id, pet_id) VALUES (2, 1);


SELECT inv.invoice_id, cust.customer_name, p.pet_name, pro.procedure_name, 
pro.amount AS cost, SUM(amount) AS totalcost
FROM vet.procedures pro LEFT JOIN vet.procedures_pet propet
ON pro.procedure_id = propet.procedure_id
LEFT JOIN vet.pet p
ON propet.pet_id = p.pet_id
LEFT JOIN vet.invoice_pet invpet
ON p.pet_id = invpet.pet_id
LEFT JOIN vet.invoice inv
ON invpet.invoice_id = inv.invoice_id
LEFT JOIN vet.customer cust
ON inv.customer_id = cust.customer_id

2 个答案:

答案 0 :(得分:0)

问题是您的发票永远不会获得任何“customer_id”,因为您没有在insert语句中插入值。

所以对于customer_id,所有元组都有“null”。

在此回答,因为我无法发表评论:

当然,在这种情况下,您必须先将您的客户添加到数据库中。否则他们无法被引用。

答案 1 :(得分:0)

以下内容为每位顾客,他们的宠物,宠物所拥有的程序(如果有的话)以及这些程序的费用。

你没有说“总费用”是什么意思(每个客户的总数?每只宠物?总计?)所以我已按每位客户的费用计算(参见相关的子查询)

SELECT    c.customer_name,
          p.pet_name,
          pr.procedure_name,
          pr.amount,
          (
            SELECT  SUM(amount)
            FROM    procedures pr1
                    JOIN procedures_pet pp1
                      ON pr1.procedure_id = pp1.procedure_id
                    JOIN pet p1
                      ON p1.pet_id = pp1.pet_id
                    JOIN customer c1
                      ON p1.customer_id = c1.customer_id
            WHERE c1.customer_id = c.customer_id
          ) AS totalcost
FROM      customer c
          JOIN pet p
              ON c.customer_id = p.customer_id
          LEFT JOIN procedures_pet pp
              ON pp.pet_id = p.pet_id
          LEFT JOIN procedures pr
              ON pr.procedure_id = pp.procedure_id
ORDER BY customer_name,
         pet_name,
         procedure_name;