return外键存在信息如何变为select结果?

How return foregin key existence information to select result?

我有一个问题:

    SELECT `Name`, `ID_dir`, 999 as `children` 
    FROM `dir` dir WHERE dir.`fid_parent` IS NULL 
    AND (
      EXISTS (
         SELECT 1
         FROM   `file` f
         WHERE  dir.ID_dir = f.fid_parent
         )
       OR (
         SELECT 1
         FROM   `dir` d2
         WHERE  dir.ID_dir = d2.fid_parent
       )
    )

我在哪里检查目录是否有任何外键。 我如何移动该信息来代替 "Select ... 999 as children" 中的 999? 我想 return (0 or 1) xor Boolean in that place as children.

EXISTS 子查询放入 SELECT 列表。

SELECT Name, ID_Dir, (
      EXISTS (
         SELECT 1
         FROM   `file` f
         WHERE  dir.ID_dir = f.fid_parent
         )
       OR (
         SELECT 1
         FROM   `dir` d2
         WHERE  dir.ID_dir = d2.fid_parent
       )
    ) AS children
FROM dir WHERE fid_parent IS NULL