0

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

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

我想从pictures表格中选择所有内容(带有特定的pictures.picture_id),从表格中选择所有内容photographers,然后选择分配给照片中的每个模型picture_modelspicture_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 = ?

做我想做的事是否毫无意义/不可能?只做另一个专门选择所有模型的查询会更好吗?

4

2 回答 2

1

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

您可能需要将 ph.* 扩展为单独的列,因为没有它,内部查询中将有两个摄影师 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
于 2013-10-12T17:02:18.037 回答
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
于 2013-10-12T18:54:14.877 回答