1

Forgive the newbie question, but I'm still fairly new to mySQL...just getting things to run is tricky. I just spent a few hours writing this monstrosity:

SELECT orders_comments.orderID, orders_comments.mediaID, orders_comments.imageURL, orders_comments_comments.comment
    FROM orders_comments 
    JOIN orders_comments_comments 
    ON orders_comments.orderID = orders_comments_comments.orderID
    JOIN 
    (
        SELECT DISTINCT orderID
        FROM 
        (
            SELECT * FROM orders_comments
            WHERE orders_comments.orderID NOT IN 
            ( 
                SELECT log_comments.orderID 
                FROM log_comments 
                JOIN users_comments 
                ON (log_comments.userID = users_comments.userID) 
                WHERE users_comments.userID = :userID 
            )
            AND orders_comments.userID != :userID 
        ) validOrders
        ORDER BY orderID ASC 
        LIMIT :numOrdersToReceive
    ) distinctOrders
    ON orders_comments.orderID = distinctOrders.orderID

I've read that subqueries are generally not good and that you can do better with JOIN operations, but the complexity of this query has me beat. At a quick glance, are there any obvious improvements to be made?

GoldenJoe
  • 7,874
  • 7
  • 53
  • 92

0 Answers0