Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Conditions which always matches returns no result with CTE

+3
−0

I have the following scenario (in MySQL 8):

CREATE TABLE `steps` (
`id` int NOT NULL AUTO_INCREMENT,
`number` varchar(30) DEFAULT NULL,
`parent_number` varchar(30) DEFAULT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`step` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
);

INSERT INTO `steps` (`number`, `parent_number`, `step`) VALUES 
    ('1', null, '1'), 
    (1, null, 'FINAL'), 
    (2, null, '1'), 
    (2, null, 'FINAL'), 
    (3, 1, '1'), 
    (3, 1, 'FINAL'), 
    (4, 1, '1'), 
    (4, 1, 'FINAL'), 
    (5, 3, '1'), 
    (5, 3, 'FINAL'), 
    (6, 4, '1'), 
    (6, 4, 'FINAL');

WITH RECURSIVE root_from_child(number, parent_number, timestamp, step) AS 
(
SELECT s1.number, s1.parent_number, s1.timestamp, s1.step FROM steps s1 
	WHERE s1.number = 5
UNION ALL 
SELECT s2.number, s2.parent_number, s2.timestamp, s2.step FROM root_from_child r 
	INNER JOIN steps s2 ON r.parent_number = s2.number 
//this line is commented	-- WHERE s2.number <> NULL
) 
SELECT DISTINCT r1.number, r1.parent_number, r1.timestamp, r1.step FROM root_from_child r1

My problem is that if I uncomment last line from the CTE (the one which I marked with //this line is commented), I get nothing but the rows returned by the pivot query (first query from CTE). Why this? I think the uncommenting of that line shouldn't have any impact on the result.

Any idea?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

1 answer

+5
−0

Wrong operator. To test for NULL, use the IS NULL and IS NOT NULL operators ... You cannot use arithmetic comparison operators such as =, <, or <> to test for NULL.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »