Search code examples
mysqlsqlunionleft-join

sql UNION breaks query


Query :

            (
                SELECT
                    upd.uid,
                    upd.update_id,
                    upd.update,
                    upd.date,
                    upd.type,
                    upd.total_likes,
                    upd.total_comments,
                    upd.deleted,
                    usr.username AS `username`,
                    usr.profile_picture AS `profile_picture`
                    ,(
                        SELECT
                            COUNT(lik.id)
                        FROM
                            likes as lik
                        WHERE
                            upd.update_id = lik.item_id
                            AND
                            lik.uid = 118697835834
                            AND lik.type=0
                    ) as liked_update,
                    (
                        SELECT
                            COUNT(fav.id)
                        FROM
                            favorites as fav
                        WHERE
                            upd.update_id = fav.item_id
                            AND
                            fav.uid = 118697835834
                            AND fav.type=0
                    ) as favorited_update

                FROM 
                    updates AS upd
                    LEFT JOIN 
                        users AS usr
                            ON upd.uid = usr.uid
                WHERE
                    upd.deleted=0
                  AND
                    (
                        upd.uid=118697835834
                           OR EXISTS
                          (
                              SELECT *
                            FROM
                                subscribers AS sub
                            WHERE
                                upd.uid = sub.suid
                                  AND sub.uid = 118697835834
                          )
                    )

            )
            UNION
            (
                SELECT
                    topic.uid,
                    topic.tid,
                    topic.title,
                    topic.body,
                    topic.total_likes,
                    topic.total_replies,
                    topic.views,
                    topic.date,
                    usr.username AS `username`,
                    usr.profile_picture AS `profile_picture`
                    ,(
                        SELECT
                            COUNT(lik.id)
                        FROM
                            likes as lik
                        WHERE
                            topic.update_id = lik.item_id
                            AND
                            lik.uid = 118697835834
                            AND lik.type=1
                    ) as liked_update,
                    (
                        SELECT
                            COUNT(fav.id)
                        FROM
                            favorites as fav
                        WHERE
                            topic.update_id = fav.item_id
                            AND
                            fav.uid = 118697835834
                            AND fav.type=1
                    ) as favorited_update

                FROM 
                    topics AS topic
                    LEFT JOIN 
                        users AS usr
                            ON topic.uid = usr.uid
                WHERE
                    topic.deleted=0
                  AND
                    (
                        topic.uid=118697835834
                           OR EXISTS
                          (
                              SELECT *
                            FROM
                                subscribers AS sub
                            WHERE
                                topic.uid = sub.suid
                                  AND sub.uid = 118697835834
                          )
                    )

            )
            ORDER BY date DESC

I have added the UNION ( SELECT ) and the query takes forever to load and finally it stops returning a blank page...

removing UNION ( SELECT ) works fine...


Solution

  • OK after all the comments can you please say what changes you made to get the script working.

    In light of your comment about the data from two tables being in the same columns that is because you have told the engine to do that by using UNION. If you want them in seperate columns you will need to place Nulls in between:

    SELECT
        upd.uid,
        upd.update_id,
        upd.update,
        upd.date,
        upd.type,
        upd.total_likes,
        upd.total_comments,
        upd.deleted,
        null AS `topicuid`,
        null AS `topictid`,
        null AS `topictitle`,
        null AS `topicbody`,
        null AS `topictotal_likes`,
        null AS `topictotal_replies`,
        null AS `topicviews`,
        null AS `topicdate`,
        usr.username AS `username`,
        usr.profile_picture AS `profile_picture`,
        (SELECT COUNT(lik.id) FROM likes as lik WHERE upd.update_id = lik.item_id AND lik.uid = 118697835834 AND lik.type=0) as liked_update,
        (SELECT COUNT(fav.id) FROM favorites as fav WHERE upd.update_id = fav.item_id AND fav.uid = 118697835834 AND fav.type=0) as favorited_update
    FROM
        ......
    UNION ALL
    SELECT
        null AS `upd.uid`,
        null AS `upd.update_id`,
        null AS `upd.update`,
        null AS `upd.date`,
        null AS `upd.type`,
        null AS `upd.total_likes`,
        null AS `upd.total_comments`,
        null AS `upd.deleted`,
        topic.uid,
        topic.tid,
        topic.title,
        topic.body,
        topic.total_likes,
        topic.total_replies,
        topic.views,
        topic.date,
        usr.username AS `username`,
        usr.profile_picture AS `profile_picture`,
        (SELECT COUNT(lik.id) FROM likes as lik WHERE topic.update_id = lik.item_id AND lik.uid = 118697835834 AND lik.type=1) as liked_update,
        (SELECT COUNT(fav.id) FROM favorites as fav WHERE topic.update_id = fav.item_id AND fav.uid = 118697835834 AND fav.type=1) as favorited_update
    

    You might need to do a bit more work on the column names but you should get there. You should read up on what the UNION operator actually does to two or more queries.