u013685578
蓝兔先生
采纳率0%
2021-03-08 21:02

初学 MYSQL,LEFT JOIN 后多了一行数据?

SELECT
	t.id,
	t.is_group_buy AS `is_group_buy`,
	t.`name` AS `client_name`
FROM
	(SELECT oi.id, oi.user_id, oi.is_group_buy, c.name
	 FROM order_info AS `oi` LEFT JOIN client AS `c` ON oi.client_id=c.id
	 WHERE DATE(oi.date)>"2025-10-15" AND oi.`status`="completed" AND oi.product_name IN("C++", "Java", "Python")) AS `t`
	 
	 INNER JOIN
	 
	 (SELECT user_id, COUNT(*)
		FROM order_info
		GROUP BY user_id HAVING COUNT(*)>=2) AS `b`
		
	 ON t.user_id=b.user_id
ORDER BY t.id;

 

建表语句:

drop table if exists order_info;
drop table if exists client;
CREATE TABLE order_info (
id int(4) NOT NULL,
user_id int(11) NOT NULL,
product_name varchar(256) NOT NULL,
status varchar(32) NOT NULL,
client_id int(4) NOT NULL,
date date NOT NULL,
is_group_buy varchar(32) NOT NULL,
PRIMARY KEY (id));

CREATE TABLE client(
id int(4) NOT NULL,
name varchar(32) NOT NULL,
PRIMARY KEY (id)
);

INSERT INTO order_info VALUES
(1,557336,'C++','no_completed',1,'2025-10-10','No'),
(2,230173543,'Python','completed',2,'2025-10-12','No'),
(3,57,'JS','completed',0,'2025-10-23','Yes'),
(4,57,'C++','completed',3,'2025-10-23','No'),
(5,557336,'Java','completed',0,'2025-10-23','Yes'),
(6,57,'Java','completed',1,'2025-10-24','No'),
(7,557336,'C++','completed',0,'2025-10-25','Yes');

INSERT INTO client VALUES
(1,'PC'),
(2,'Android'),
(3,'IOS'),
(4,'H5');

 

题目要求:

查询在2025-10-15以后,同一个用户下单2个以及2个以上状态为购买成功的C++课程或Java课程或Python课程的订单id,是否拼团以及客户端名字信息,最后一列如果是非拼团订单,则显示对应客户端名字,如果是拼团订单,则显示NULL,并且按照order_info的id升序排序。

 

运行结果:

4|No|IOS
5|Yes|None
6|No|PC
7|Yes|None
8|Yes|None
9|No|H5   <--- 咋多了这一行呢?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • technologist_08 CSDN技术专家团—wangjie 1月前

    我用题主的SQL运行试了一下,结果中没有 H5 这一行。检查下是不是数据库 order_info 表里里有多的数据行?

    点赞 评论 复制链接分享
  • theo789 Metz 1月前

    运行结果确定有H5吗?

    点赞 评论 复制链接分享