如果是mysql select语句中的isnull

时间:2011-06-21 18:57:26

标签: mysql

我有以下sql。如果子查询返回任何内容,我希望能够引入yes / no(true / false)。

SELECT
              post.topic_id,
              topic.topic_posts,
              topic.topic_title,
              topic.topic_poster_name,
              topic.topic_last_post_id,
              topic.topic_start_time,
              (SELECT post_time FROM bb_posts WHERE post_id = topic.topic_last_post_id) as last_post_time,
              topic.topic_slug,
              topic.topic_posts,
              `group`.name AS group_name,
              `group`.slug AS child_slug,
              `parent`.slug AS parent_slug,
                      (SELECT id FROM table WHERE condition = 1) as isTrue << subquery
            FROM bb_posts post
            LEFT JOIN bb_topics topic
              ON topic.topic_id = post.topic_id
            LEFT JOIN bb_forums forum
              ON topic.forum_id = forum.forum_id
            LEFT JOIN wp_bp_groups_groupmeta groupmeta
              ON forum.forum_id = groupmeta.meta_value
            LEFT JOIN wp_bp_groups `group`
              ON groupmeta.group_id = `group`.id
            LEFT JOIN wp_bp_groups `parent`
              ON `group`.parent_id = `parent`.id
            $conditions
            GROUP BY topic_id
            ORDER BY $sort_col $dir
            LIMIT $offset,$num

如果返回结果,我希望子查询返回yes。

2 个答案:

答案 0 :(得分:2)

您可以使用count(*):

SELECT 0 != (SELECT COUNT(*) FROM (subquery))

如果子查询返回任何内容,则为1。

答案 1 :(得分:0)

如果返回结果,子查询返回“是”(如果没有则返回空字符串):

SELECT IF(condition = 1, 'yes', '') AS isTrue FROM table WHERE some_condition_goes_here

如果您要加入某些列,那么这将无效,您未在所显示的子查询中指定该列。连接将在WHERE子句中替换“some_condition_goes_here”。如下所示:

WHERE table.id = some_other_table.tableid