Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果mysql select语句中为空_Mysql - Fatal编程技术网

如果mysql select语句中为空

如果mysql select语句中为空,mysql,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.to

我有以下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。

您可以使用count*:

选择0!=从子查询中选择COUNT*


如果子查询返回任何结果,则返回1。

如果返回结果,则子查询返回yes;如果返回结果,则返回空字符串:

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
SELECT IF(condition = 1, 'yes', '') AS isTrue FROM table WHERE some_condition_goes_here
如果您要加入某个列,而您没有在显示的子查询中指定该列,那么这将起作用。连接将进入WHERE子句,替换此处的某些条件。比如:

WHERE table.id = some_other_table.tableid

如何在子查询的WHERE条件中获取post.topic_id。在您的示例中,您似乎嵌套了一个子查询两次。我无法从父查询中获取post.topic\u id。