检索客户MySQL购买的每个产品

时间:2017-05-24 21:00:39

标签: mysql

我正在为数据库课做家庭作业。部分任务是使用以下数据库创建SQL查询以完成各种任务:

create database homework3;
use homework3;

create table customer(
    cus_code int, 
    cus_lname varchar(20), 
    cus_fname varchar(20), 
    cus_initial char, 
    cus_areacode int, 
    cus_phone int, 
    primary key(cus_code));

create table invoice(
    inv_number int, 
    cus_code int, 
    inv_date date, 
    primary key(inv_number), 
    foreign key(cus_code) references customer(cus_code));

create table vendor(
    vend_code int, 
    vend_name varchar(30), 
    vend_contact varchar(30), 
    vend_areacode int, 
    vend_phone int, 
    primary key(vend_code));

create table product(
    prod_code int, 
    prod_desc varchar(50), 
    prod_price int, 
    prod_quant int, 
    vend_code int, 
    primary key(prod_code), 
    foreign key(vend_code) references vendor(vend_code));

create table line(
    inv_number int, 
    prod_code int, 
    line_units int, 
    primary key(inv_number, prod_code), 
    foreign key(inv_number) references invoice(inv_number), 
    foreign key(prod_code) references product(prod_code));

/* cus_code, cus_lname, cus_fname, cus_initial, cus_areacode, cus_phone */
insert into customer values (10010, "Ramas", "Alfred", 'A', 615, 8442573);
insert into customer values (10011, "Dunne", "Leona", 'K', 713, 8941238);
insert into customer values (10012, "Smith", "Kathy", 'W', 615, 8942285);
insert into customer values (10013, "Olowski", "Paul", 'F', 615, 2221672);
insert into customer values (10014, "Orlando", "Myron", NULL, 615, 2971228);

/* inv_number, cus_code, inv_date */
insert into invoice values (1001, 10011, '2008-08-03');
insert into invoice values (1002, 10014, '2008-08-04');
insert into invoice values (1003, 10012, '2008-03-20');
insert into invoice values (1004, 10011, '2008-09-23');

/* vend_code, vend_name, vend_contact, vend_areacode, vend_phone */
insert into vendor values (232, "Bryson", "Smith", 615, 2233234);
insert into vendor values (235, "SuperLoo", "Anderson", 615, 2158995);

/* prod_code, prod_desc, prod_price, prod_quant, vend_code */
insert into product values (12321, "hammer", 189 ,20, 232);
insert into product values (65781, "chain", 12, 45, 235);
insert into product values (34256, "tape", 35, 60, 235);
insert into product values (12333, "hanger", 200 ,10, 232);

/* inv_number, prod_code, line_units */
insert into line values (1001, 12321, 1);
insert into line values (1001, 65781, 3);
insert into line values (1002, 34256, 6);
insert into line values (1003, 12321, 5);
insert into line values (1002, 12333, 6);

有问题的陈述是:

Query

我试过了:

use homework3;
select product.prod_desc, customer.cus_fname, customer.cus_lname from product, 
customer 
natural join line where line.inv_number is not null;

每个人购买了几种产品后,返回了绝对错误的结果。在此先感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

您需要指定line与其他表之间的连接条件。您需要包含invoice表以获得与客户的关系。

SELECT cus_fname, cus_lname, prod_desc
FROM customer AS c
JOIN invoice AS i ON c.cus_code = i.cus_code
JOIN line AS l ON l.inv_number = i.inv_number
JOIN product AS p ON p.prod_code = l.prod_code
ORDER BY cus_code

不要使用隐式联接(FROM customer, product)的习惯,列出您与具有相应JOIN条件的ANSI ON专门加入的每个表。

答案 1 :(得分:1)

如果您从查询中删除了natural join,那么您将拥有产品和客户的交叉产品:将每个产品与每个客户相匹配。添加联接只会将无关信息与每个配对相匹配。

相反,你想要的是line中的每一行(我认为是购买)与相应的客户和产品,以便您可以获得每个产品的所需字段。