MYSQL PHP将大量评论与社交网络活动供稿中的正确状态相匹配

时间:2013-11-06 12:51:58

标签: php mysql

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

这是我第一次尝试按此比例编写查询。

我正在创建一个社交网络,并且正在查询活动供稿,该查询显示人们何时喜欢某些内容,对某些内容发表评论等等。当所有人回复某个状态时,我都会将其全部工作。

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

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进行分组,但因此只会在Feed中显示状态和一条评论。

我正在使用一个名为activity的表,然后有一个名为type的列,它将包含诸如'Status','Status Reply''Like'等条目,并且有一个名为r_id的列(不是唯一的id),当你回复一个状态,在这里输入原始状态的唯一ID。

如果我遗漏了任何内容或需要更好地解释,请告诉我。

干杯, 丹

1 个答案:

答案 0 :(得分:0)

您可以尝试使用SELECT DISTINCT代替SELECT