嗨,我有麻烦做一个左连接,输出所有客户谁还没有下订单。我得到一个转换错误,并试图使用铸造转换,但它不工作。任何帮助都将不胜感激
以下是我的表格和数据:`
创建表customers(cust\u code varchar(2)主键,[first\u name]varchar(30)not null,last\u name varchar(30)not null,address varchar(100)not null,city varchar(35)not null)
create table Cust_Order(
order_no int ,
Cust_code varchar(2) not null,
order_date date not null,
meth_pmt varchar(30) not null,
constraint Ord_order_no_pk primary key(order_no),
constraint Cus_cust_code_fk foreign key(Cust_code) references Customers)
create table Product(
product_id int ,
product_name varchar(100) not null,
product_price decimal(8, 2) not null,
constraint Pro_product_id_pk primary key(product_id))
create table Order_Line(
order_no int ,
product_id int ,
qty int not null,
sale_price decimal(8,2) not null,
constraint Ord_order_no_product_id_pk primary key(order_no, product_id),
constraint Pro_product_id_fk foreign key(product_id) references Product)
insert into Customers (Cust_code, [first_name], last_name, address, city)
values('A1', 'Kath', 'Morgan','122 Lilain Street', 'Palmerston North'),
('A2','Mike','Smith','67 Golf Hill Drive','Wellington'),
('A3','Glen','Hoddle','San Quentin Ave','Palmerston North'),
('A4','Dan','Boone','Alamo Road','Wellington')
insert into Cust_Order (order_no, Cust_code, order_date, meth_payment)
values(1, 'A1','2014-01-16','CC'),
(2, 'A1','2014-02-16','CC'),
(3, 'A2','2014-01-16','CHEQUE'),
(4, 'A3','2014-03-17','CC')
insert into Product (product_id, product_name, product_price)
values(1, 'Network Card', 58.00),
(2, 'Motherboard', 150.00),
(3, 'Video Card', 232.00)
insert into Order_Line (order_no, product_id, qty, sale_price)
values(1,1,3,70.00),
(1,2,1,170.00),
(1,3,2,300.00),
(2,1,2,70.00),
(2,3,1,300.00),
(3,1,2,70.00),
(4,1,3,70.00)
我的select语句使用left join,它应该输出一个没有下订单的客户
select first_name + last_name as 'Customers who have not made an order',
order_no from Customers as c
left join Cust_Order as o
on c.Cust_code = o.order_no
where order_no is null
2条答案
按热度按时间fgw7neuy1#
您的加入条件需要与客户代码匹配。除此之外,您的查询逻辑是正确的,但是mysql不使用
+
对于字符串连接,这是sql server语法。使用CONCAT
改为函数:ansi连接运算符是
||
,如果设置了适当的模式,也可以在mysql中使用:ljo96ir52#
如果必须使用左连接,请尝试:
或者你可以利用存在。
请参阅mysql join made easy,了解如何使用连接。