MYSQL和PHP:没有结果的SELECT子查询返回空值为

时间:2015-12-21 10:59:38

标签: php mysql

我有一个包含游戏,文章(评论+预览)和视频(视频评论+视频预览)的数据库。我有一个身份233的游戏,我想找一个评论,一个预览,一个视频评论和视频预览(如果存在的话)。

为此,我使用以下查询:

SELECT (SELECT article_url
        FROM   articles
        WHERE  articles_game_id = 233
               AND artikel_sort = 'review'
               AND article_online = 1
        ORDER  BY article_datetime DESC
        LIMIT  1) AS review,

       (SELECT article_url
        FROM   articles
        WHERE  articles_game_id = 233
               AND artikel_sort = 'preview'
               AND article_online = 1
        ORDER  BY article_datetime DESC
        LIMIT  1) AS preview,

       (SELECT trailer_url
        FROM   trailers
        WHERE  trailer_game_id = 233
               AND trailer_sort = 'video review'
               AND trailer_online = 1
        ORDER  BY trailer_datetime DESC
        LIMIT  1) AS videoreview,

       (SELECT trailer_url
        FROM   trailers
        WHERE  trailer_game_id = 233
               AND trailer_sort = 'video preview'
               AND trailer_online = 1
        ORDER  BY trailer_datetime DESC
        LIMIT  1) AS videopreview
FROM   articles 

但是,此查询有两个问题:

  • 它查询数据库五次,这对我来说似乎不是很有效。
  • 如果没有评论(例如),则生成的PHP数组确实具有键'/ review'的键/值对,但其值为空。但是,我希望这个键/值对完全不存在,所以我可以简单地遍历数组并打印每个键/值对作为链接。

有人知道解决这个问题的有效方法吗?非常感谢任何帮助!

0 个答案:

没有答案