2011-06-21 7 views
0

私は以下のSQLを持っています。サブクエリが何かを返す場合は、yes/no(true/false)を取り込めます。mysqlのselect文にisnullがある場合

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 

結果が返された場合、サブクエリが「はい」を返すようにします。

答えて

2

あなたはCOUNT(*)を使用することができます

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

サブクエリが何かを返した場合、あなたに1を与えます。サブクエリが返すようにするために

+0

サブクエリのWHERE条件ではどのようにしてpost.topic_idを取得できますか。あなたの例から、副問合せを2回ネストしているようです。私は親クエリからpost.topic_idを取得できません。 – madphp

0

「はい」の結果は、(空の文字列でない場合)を返した場合:

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

はあなたが指定しない、いくつかの列に参加している場合に動作しますあなたが示したサブクエリー結合は、「some_condition_goes_here」を置き換えるWHERE節に入ります。したがって、次のようになります。

WHERE table.id = some_other_table.tableid 
関連する問題