SQL查询以查找在特定公司地址购买的精选客户

时间:2019-08-28 00:41:38

标签: sql database

这是我的桌子

def row(l):
    row = [ col.strip() for col in l.split(':') ]
    row[2:] = row[2].split()
    return [ row[i] if i < len(row) else '' for i in range(6) ]

Here is my sample data table

我试图找到所有购买的产品的客户,其公司地址位于“ Burwood”中,并列出该客户的ID,名称,产品ID,产品名称和产品地址

CREATE TABLE Customer 
(
     ID CHAR(50),
     Customer_FName CHAR(50),
     Customer_Lname CHAR(50)
);

CREATE TABLE Buying 
(
     Customer_ID CHAR(50),
     Product_ID CHAR(50),
     Order_Time CHAR(50)
);

CREATE TABLE Product 
(
    ID CHAR(50),
    Name CHAR(50),
    Address CHAR(50)
);

我想合并三个表,但显示“未选择行”。

我还提供了示例数据表

3 个答案:

答案 0 :(得分:2)

为什么选择 CHAR 作为所有表的所有列的数据类型?对于基于CHAR的列,DB倾向于将值填充到所定义的列宽。就是说,这不是为什么您没有得到结果。您可能需要检查在插入过程中是否在ID中添加了多余的空格或不可打印的字符,从而导致内部联接失败。

我建议将字段更改为 VARCHAR ,验证您的插入内容,然后按照下面的说明进行查询。和我一样,您将开始获得结果。

CREATE TABLE Customer (
    ID varchar(50),
    Customer_FName varchar(50),
    Customer_Lname varchar(50)
);

CREATE TABLE Buying (
    Customer_ID varchar(50),
    Product_ID varchar(50),
    Order_Time varchar(50)
);

CREATE TABLE Product (
    ID varchar(50),
    Name varchar(50),
    Address varchar(50)
);


insert into customer values('10001', 'John', 'Smith');
insert into Buying values('10001', '772', '2016/09/01');
insert into Product values('772', 'Telephone', '22 Ave, Burwood');

select b.product_id, p.name, b.customer_id, c.customer_fname, c.customer_lname
from buying b
join product p on b.product_id = p.id
join customer c on b.customer_id = c.id
where lower(p.address) like '%burwood%'

enter image description here

答案 1 :(得分:1)

请尝试一下,它对我有用:

Select B.Customer_ID, B.Product_ID, P.ID, P.Name, C.ID, C.Customer_FName, C.Customer_Lname
from Buying B
INNER JOIN Product P ON B.Product_ID = P.ID
INNER JOIN Customer C ON B.Customer_ID = C.ID
WHERE P.Address LIKE '%Burwood%'

答案 2 :(得分:-1)

尝试一下:

SELECT C.*,B.*, P.*
FROM Customer C,Buying B, Product P
WHERE B.Product_ID=P.ID
AND B.Customer_ID=C.ID
AND P.Address LIKE '%Burwood%';