0

我想知道你们中的任何人是否可以提供帮助,因为我已经坚持了好几天了。

这是我第一次尝试编写这种规模的查询。

我正在创建一个社交网络,并正在查询活动提要,该提要显示人们何时喜欢某事、评论某事等。除了有人回复某个状态之外,我所有的一切都在工作。

我想要做的是将相关评论附加到相关状态,这有点工作,但它显示原始状态的重复条目。请看下面的查询

SELECT
                d1.d_id AS d1_d_id, d1.profile_picture AS d1_profile_picture, d1.username AS d1_username, d1.forename AS d1_forename, d1.surname AS d1_surname,
                d2.d_id AS d2_d_id, d2.profile_picture AS d2_profile_picture, d2.username AS d2_username, d2.forename AS d2_forename, d2.surname AS d2_surname,
                d3.d_id AS d3_d_id, d3.profile_picture AS d3_profile_picture, d3.username AS d3_username, d3.forename AS d3_forename, d3.surname AS d3_surname,
                d4.d_id AS d4_d_id, d4.profile_picture AS d4_profile_picture, d4.username AS d4_username, d4.forename AS d4_forename, d4.surname AS d4_surname,
                a.ra_id AS ra_id, a.r_id AS r_id, a.comment AS a_comment, a.type AS a_type, a.status AS status, a.date_posted AS date_posted,
                ar.r_id AS ar_r_id, ar.comment AS ar_comment, ar.type AS ar_type, ar.status AS ar_status, ar.date_posted AS ar_date_posted, 
                c.slug AS cat_slug,
                p.p_id AS p_id, p.title AS title, p.slug AS slug,
                m.media_path AS p_media_path,
                m1.i_id AS m1_i_id, m1.media_path AS m1_media_path, 
                m2.i_id AS m2_i_id, m2.media_path AS m2_media_path,
                m3.i_id AS m3_i_id, m3.media_path AS m3_media_path,
                m4.i_id AS m4_i_id, m4.media_path AS m4_media_path
                FROM activity AS a
                INNER JOIN designers AS d1 ON d1.d_id = a.d_id
                LEFT JOIN media AS m1 ON m1.i_id = d1.profile_picture
                LEFT JOIN projects AS p ON p.p_id = a.r_id
                LEFT JOIN media AS m ON m.i_id = p.image
                LEFT JOIN categories AS c ON c.c_id = p.c_id
                LEFT JOIN designers AS d2 ON d2.d_id = p.d_id
                LEFT JOIN media AS m2 ON m2.i_id = d2.profile_picture
                LEFT OUTER JOIN designers AS d3 ON d3.d_id = a.t_id
                LEFT JOIN media AS m3 ON m3.i_id = d3.profile_picture
                LEFT JOIN activity AS ar ON ar.r_id = a.ra_id
                LEFT JOIN designers AS d4 ON d4.d_id = ar.d_id
                LEFT JOIN media AS m4 ON m4.i_id = d4.d_id
                WHERE a.status != 1 AND a.type != 'Follow'

                UNION

                SELECT
                d1.d_id AS d1_d_id, d1.profile_picture AS d1_profile_picture, d1.username AS d1_username, d1.forename AS d1_forename, d1.surname AS d1_surname,
                d2.d_id AS d2_d_id, d2.profile_picture AS d2_profile_picture, d2.username AS d2_username, d2.forename AS d2_forename, d2.surname AS d2_surname,
                d3.d_id AS d3_d_id, d3.profile_picture AS d3_profile_picture, d3.username AS d3_username, d3.forename AS d3_forename, d3.surname AS d3_surname,
                d4.d_id AS d4_d_id, d4.profile_picture AS d4_profile_picture, d4.username AS d4_username, d4.forename AS d4_forename, d4.surname AS d4_surname,
                a.ra_id AS ra_id, a.r_id AS r_id, a.comment AS a_comment, a.type AS a_type, a.status AS status, a.date_posted AS date_posted,
                ar.r_id AS ar_r_id, ar.comment AS ar_comment, ar.type AS ar_type, ar.status AS ar_status, ar.date_posted AS ar_date_posted, 
                c.slug AS cat_slug,
                p.p_id AS p_id, p.title AS title, p.slug AS slug,
                m.media_path AS p_media_path,
                m1.i_id AS m1_i_id, m1.media_path AS m1_media_path, 
                m2.i_id AS m2_i_id, m2.media_path AS m2_media_path,
                m3.i_id AS m3_i_id, m3.media_path AS m3_media_path,
                m4.i_id AS m4_i_id, m4.media_path AS m4_media_path
                FROM activity AS a
                INNER JOIN designers AS d1 ON d1.d_id = a.d_id
                LEFT JOIN media AS m1 ON m1.i_id = d1.profile_picture
                LEFT JOIN projects AS p ON p.p_id = a.r_id
                LEFT JOIN media AS m ON m.i_id = p.image
                LEFT JOIN categories AS c ON c.c_id = p.c_id
                LEFT JOIN designers AS d2 ON d2.d_id = a.r_id
                LEFT JOIN media AS m2 ON m2.i_id = d2.profile_picture
                LEFT OUTER JOIN designers AS d3 ON d3.d_id = a.t_id
                LEFT JOIN media AS m3 ON m3.i_id = d3.profile_picture
                LEFT JOIN activity AS ar ON ar.r_id = a.ra_id
                LEFT JOIN designers AS d4 ON d4.d_id = ar.d_id
                LEFT JOIN media AS m4 ON m4.i_id = d4.d_id
                WHERE a.status != 1 AND a.type = 'Follow'
                ORDER BY date_posted DESC

我已经尝试按照表格评论 id 进行分组,但因此只在提要中显示状态和一条评论。

我正在使用一个名为 activity 的表,然后有一个名为 type 的列,其中将包含诸如“状态”、“状态回复”、“喜欢”等条目,并有一个名为 r_id 的列(不是唯一 ID),当您回复时此处输入原始状态的唯一 ID 的状态。

如果我遗漏了什么或需要更好地解释,请告诉我。

干杯,丹

4

1 回答 1

0

您可以尝试SELECT DISTINCT代替SELECT

于 2013-11-06T12:54:43.587 回答