选择多列子选择查询

时间:2017-08-10 12:58:33

标签: php mysql

我正在努力解决以下问题。

我总共有3张桌子。

1)有故事(帖​​子)的表格

---------------------------------------------------
| ts_story_stories                                |
---------------------------------------------------
| id | title | user_id | creation_date | category |

2)包含用户的表

---------------------------
| ts_user                 |
---------------------------
| id | username | ...| ...|

3)带有章节元素的表格 (一个故事包含2< n< 20章节,每章包含2&n; n章节元素(如段落,图像或视频))

---------------------------------
| ts_story2_segments            |
---------------------------------
| id | type | value_0 | value_1 |

我想创建一个故事预览 像这样的预览

Story Preview

我需要一个MySQL查询,它返回一组最多包含6行的查询 user_id,story_id,story_title,story_first_image,story_first_text

只检索user_id,story_id和story_title相当容易。

SELECT  ts_story_stories.id AS story_id, 
        ts_story_stories.title, 
        ts_story_stories.user_id AS user_id

FROM    ts_story_stories

INNER JOIN ts_user 
        ON ts_user.id = ts_story_stories.user_id  

WHERE   user_id = '{$STORY_DATA["user_id"]}' 
        AND ts_story_stories.id <> '{$STORY_DATA["story_id"]}' 

ORDER   BY RAND()
LIMIT   6;

问题在于story_first_image,story_first_text 这实际上也很容易。

获取第一张图片:

SELECT ts_story2_segments.type, 
       ts_story2_segments.value0, 
       ts_story2_segments.value1 

FROM   ts_story2_segments 

WHERE  (ts_story2_segments.type = '1' 
          OR ts_story2_segments.type = '2') 
       AND ts_story2_segments.story_id = ts_story_stories.id 

ORDER  BY ts_story2_segments.id ASC 

LIMIT  1 

如果type为1(图像),则value0为图像,如果type为2(video),则value1为图像(缩略图)。

和第一个文字:

SELECT ts_story2_segments.value0 

FROM   ts_story2_segments 

WHERE  ts_story2_segments.type = '0' 
       AND ts_story2_segments.story_id = ts_story_stories.id 

ORDER  BY ts_story2_segments.id ASC 

LIMIT  1 

我不知道如何从主要查询中的这两个子查询转到story_first_text / story_first_image。

目前,我的完整查询如下所示。

SELECT * FROM (
    (SELECT  ts_story_stories.id AS _story_id, 
             ts_story_stories.title, 
             ts_story_stories.user_id AS _user_id, 
             ts_story_stories.creation_date, 
             ts_story_stories.category,     
             ts_user.username,

            (SELECT   ts_story2_segments.type
            FROM ts_story2_segments
            WHERE   (ts_story2_segments.type = '1' OR ts_story2_segments.type = '2') AND ts_story2_segments.story_id = ts_story_stories.id
            ORDER   BY  ts_story2_segments.id ASC LIMIT 1) AS thumbnail_type,

            (SELECT   ts_story2_segments.value0
            FROM ts_story2_segments
            WHERE   (ts_story2_segments.type = '1' OR ts_story2_segments.type = '2') AND ts_story2_segments.story_id = ts_story_stories.id
            ORDER   BY  ts_story2_segments.id ASC LIMIT 1) AS thumbnail_value0,

            (SELECT   ts_story2_segments.value1
            FROM ts_story2_segments
            WHERE   (ts_story2_segments.type = '1' OR ts_story2_segments.type = '2') AND ts_story2_segments.story_id = ts_story_stories.id
            ORDER   BY  ts_story2_segments.id ASC LIMIT 1) AS thumbnail_value1,


            (SELECT   ts_story2_segments.value0
            FROM ts_story2_segments
            WHERE   ts_story2_segments.type = '0' AND ts_story2_segments.story_id = ts_story_stories.id
            ORDER   BY  ts_story2_segments.id ASC LIMIT 1) AS description

    FROM ts_story_stories

    INNER JOIN ts_user 
            ON ts_user.id = ts_story_stories.user_id  

    WHERE   user_id = '{$STORY_DATA["_user_id"]}' AND ts_story_stories.id <> '{$STORY}' 
    ORDER   BY RAND()
    LIMIT 6)

    UNION ALL

    (SELECT  ts_story_stories.id AS _story_id, 
             ts_story_stories.title, 
             ts_story_stories.user_id AS _user_id, 
             ts_story_stories.creation_date, 
             ts_story_stories.category,     
             ts_user.username,

            (SELECT   ts_story2_segments.type
            FROM ts_story2_segments
            WHERE   (ts_story2_segments.type = '1' OR ts_story2_segments.type = '2') AND ts_story2_segments.story_id = ts_story_stories.id
            ORDER   BY  ts_story2_segments.id ASC LIMIT 1) AS thumbnail_type,

            (SELECT   ts_story2_segments.value0
            FROM ts_story2_segments
            WHERE   (ts_story2_segments.type = '1' OR ts_story2_segments.type = '2') AND ts_story2_segments.story_id = ts_story_stories.id
            ORDER   BY  ts_story2_segments.id ASC LIMIT 1) AS thumbnail_value0,

            (SELECT   ts_story2_segments.value1
            FROM ts_story2_segments
            WHERE   (ts_story2_segments.type = '1' OR ts_story2_segments.type = '2') AND ts_story2_segments.story_id = ts_story_stories.id
            ORDER   BY  ts_story2_segments.id ASC LIMIT 1) AS thumbnail_value1,


            (SELECT   ts_story2_segments.value0
            FROM ts_story2_segments
            WHERE   ts_story2_segments.type = '0' AND ts_story2_segments.story_id = ts_story_stories.id
            ORDER   BY  ts_story2_segments.id ASC LIMIT 1) AS description

    FROM ts_story_stories

    INNER JOIN ts_user 
            ON ts_user.id = ts_story_stories.user_id  

    WHERE   user_id <> '{$STORY_DATA["_user_id"]}'
    ORDER   BY RAND() 
    LIMIT 6)
) AS gg;

这很慢,因为我有很多子查询。 有没有人知道更好的方法来解决这个问题?

0 个答案:

没有答案