2017-05-24 69 views
0

我正在爲數據庫類的作業分配工作。該任務的一部分是使用以下數據庫中創建SQL查詢來完成各種任務:檢索客戶購買的每個產品MySQL

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; 

當每個人購買了幾種產品時,這些結果都返回了錯誤的結果。先謝謝您的幫助。

+0

順便提一句,電話號碼真的是字符串。它們可能只包含整數,但這只是一個設計怪癖。你可以知道他們不是真正的數字,因爲你永遠不會從另一個電話號碼中減去一個電話號碼。 – Strawberry

+0

我們在1992年停止加入桌子,這讓我想起了一點...... – Strawberry

回答

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)的習慣,列出你用ANSI JOIN與適當的ON條件具體加入的每個表。

+0

太棒了。這正是我需要的。我現在回去重寫我的其他查詢,並且它們更具可讀性和邏輯性。謝謝。 –

1

如果您從查詢中刪除了natural join,那麼您將產生產品和客戶的交叉產品:將每個產品與每個客戶進行匹配。添加連接只需將不相關的信息匹配到每個配對。

相反,你想要的是line(我相信這是一種購買)的每一行加入相應的客戶&產品,所以你可以得到所需的每個領域。

+0

沒錯。每個客戶都有一個'cus_code',它與'invoice'中的'inv_number'鏈接,然後'inv_number'和'prod_code'在表'行'中鏈接,這意味着我應該能夠獲得誰買的名字和描述什麼? –