在一个查询中从两个表中选择一个记录,从另一个表中选择多个记录

时间:2013-10-12 16:09:45

标签: mysql sql

截至目前,我有4个表格(与此问题相关); picturesphotographersmodels& picture_models

一张照片只能有1位摄影师,但有多种型号。在pictures表中有一个photographer_id列,也可以在photographers表中找到。

我想从pictures表(具有特定pictures.picture_id),photographers表中的所有内容以及picture_models中已分配给照片的每个模型中选择所有内容1}}。 picture_models表看起来像这样;

   Table: picture_models
   `picture_id` int(10) unsigned NOT NULL,
   `model_id` int(10) unsigned NOT NULL

因此每张图片在picture_models中都可以有多条记录。回到我的问题..有可能以某种方式选择models.*以及图片信息和摄影师信息。截至目前,我的查询看起来像这样;

    SELECT p.*,
           ph.*,
           COUNT(pv.vote) vote_count,
           SUM(pv.vote) vote_sum,
           (SELECT COUNT(vote) FROM picture_votes WHERE vote > 0 AND picture_id = ?) plus_votes,
           (SELECT COUNT(vote) FROM picture_votes WHERE vote < 0 AND picture_id = ?) minus_votes
              FROM pictures p
                 LEFT JOIN picture_votes pv
                    ON pv.picture_id = p.picture_id
                 LEFT JOIN photographers ph
                    ON p.photographer_id = ph.photographer_id
                 WHERE p.authenticated = 1
                    AND
                 p.picture_id = ?

做我想做的事是没有意义的吗?如果只是选择所有模型进行另一个查询会更好吗?

2 个答案:

答案 0 :(得分:1)

你可以这样做;你会得到每个模型的图片/投票数据的副本。这比执行两个单独的查询更有效,尤其是在模型数量较少的情况下。

您可能需要将ph。*扩展为单个列,因为没有它,内部查询中将有两个photographer_id列,这可能是不允许的。此外,许多数据库都希望group by p.*, ph.*包含内部查询中的所有列。不过,我认为MySQL会让你远离它。

Select
    p.*,
    m.*
From (
    Select
        p.*,
        ph.*,
        count(pv.vote) vote_count,
        sum(pv.vote) vote_sum,
        sum(case when pv.vote > 0 Then 1 else 0 end) plus_votes,
        sum(case when pv.vote < 0 then 1 else 0 end) minus_votes
    From
        pictures p
            left join
        picture_votes pv
            On pv.picture_id = p.picture_id
            left join
        photographers ph
            On p.photographer_id = ph.photographer_id
    Where
        p.authenticated = 1 And
        p.picture_id = ?
    ) p
        left join
    picture_models m
        On p.model_id = m.model_id

答案 1 :(得分:0)

感谢@ Laurence的回答,我能够提出这个问题;

SELECT
    p.*,
    m.*
    FROM (
        SELECT
            p.*,
             ph.photographer_name,
             ph.photographer_website,
             ph.photographer_instagram,
             ph.photographer_facebook,
             ph.photographer_twitter,
             ph.photographer_googleplus,
            pm.model_id,
            COUNT(pv.vote) vote_count,
            SUM(pv.vote) vote_sum,
            SUM(CASE WHEN pv.vote > 0 THEN 1 ELSE 0 END) plus_votes,
            SUM(CASE WHEN pv.vote < 0 THEN 1 ELSE 0 END) minus_votes
        FROM
            pictures p
                LEFT JOIN
            picture_votes pv
                    ON pv.picture_id = p.picture_id
                LEFT JOIN
            picture_models pm
                    ON pm.picture_id = p.picture_id
                LEFT JOIN
            photographers ph
                    ON ph.photographer_id = p.photographer_id
        WHERE
            p.authenticated = 1
                AND
            p.picture_id = 1
        GROUP BY
            pm.model_id
        ) p
    LEFT JOIN
        models m
            ON p.model_id = m.model_id