我有一个简单的类别名称表,它使用 category_parent = id 与自身相关。我需要选择没有孩子的记录。
从其他答案中,我拼凑出这个声明:
SELECT cat.category_name AS 'Child Name', cat.id AS 'Child ID',
cat.category_parent AS 'Childs Parent', cat_par.category_name AS 'Parent Name',
cat_par.id AS 'Parent ID'
FROM category AS cat
LEFT JOIN category AS cat_par ON cat.category_parent = cat_par.id
WHERE cat_par.id IS NULL;
这将成功选择没有 PARENT 的记录。我尝试将最后一个子句更改为,WHERE cat.category_parent IS NULL
但结果为空。
我还根据另一个答案尝试了这种说法:
SELECT cat.category_name AS 'Child Name', cat.id AS 'Child ID',
cat.category_parent AS 'Childs Parent' cat_par.category_name AS 'Parent Name',
cat_par.id AS 'Parent ID'
FROM category AS cat
WHERE NOT EXISTS
(SELECT id FROM category AS cat_par WHERE cat.category_parent = cat_par.id);
返回错误No value given for one or more required parameters.