jela
jela

Reputation: 1469

why do aliases cause my query to fail in phpMyAdmin?

The following query works fine in my php script, but when I run it in phpMyAdmin (against the same database), I get no results (not even "0 results" -- it just refreshes the page).

  SELECT comment, username, image, post_date, ((us.user_id != 22) AND

        ((SELECT read_date FROM
        bookmarks WHERE
        product_id=456029 AND
        user_id=22) < post_date)) AS
        new_comment FROM

        comments AS
        co, users AS
        us, flair WHERE co.product_id=456029 AND
        co.user_id=us.user_id AND
        flair_id=us.active_flair_id ORDER BY
        co.post_date ASC

I noticed that if I take out the aliases, the query works in phpMyAdmin

SELECT comment, username, image, post_date, ((users.user_id != 22) AND

        ((SELECT read_date FROM
        bookmarks WHERE
        product_id=456029 AND
        user_id=22) < post_date)) AS
        new_comment FROM

        comments, users, flair WHERE comments.product_id=456029 AND
        comments.user_id=users.user_id AND
        flair_id=users.active_flair_id ORDER BY
        comments.post_date ASC

I am curious why this would happen. (running phpMyAdmin - 2.11.9.5)

EDIT: Richard's reformulation below works in phpMyAdmin. So now the puzzle is why one formulation works and the other doesn't (when they both work fine when executed in a php script).

the final form of the query following Richard's advice to remove the subquery:

SELECT c.comment AS 
COMMENT , username, image, DATE_FORMAT( c.post_date,  '%b %D, %Y' ) AS post_date, IF( (
(
b.read_date IS NULL
)
OR (
b.read_date < c.post_date
) ) , IF( c.user_id !=1, 1, 0 ) , 0
) AS new_comment
FROM comments c
INNER JOIN users u ON c.user_id = u.user_id
INNER JOIN flair f ON flair_id = u.active_flair_id
LEFT JOIN bookmarks b ON ( b.product_id = c.product_id
AND b.user_id =1 ) 
WHERE c.product_id =538968
ORDER BY c.post_date ASC 

Upvotes: 1

Views: 623

Answers (1)

Richard
Richard

Reputation: 4415

You have a very weird query, I wouldn't accept it either...

   SELECT comment, username, image, post_date
        , (
              (users.user_id != 22) 
          AND (
                  (
                       SELECT read_date 
                         FROM bookmarks 
                        WHERE product_id=456029 
                          AND user_id=22
                  ) 
              < post_date)
         ) AS new_comment 
    FROM comments, users, flair 
   WHERE comments.product_id=456029 
     AND comments.user_id=users.user_id 
     AND flair_id=users.active_flair_id 
ORDER BY comments.post_date ASC

little rewrite:

     SELECT comment, username, image, post_date
          , (
                (u.user_id != 22) # What is this for? First you check if it is not user 22
            AND (
                    (
                       SELECT read_date 
                           FROM bookmarks 
                         WHERE product_id=456029 
                            AND user_id=22  # And now you check if it is
                    ) 
                < post_date)
           ) AS new_comment 
      FROM comments c
INNER JOIN users u ON c.user_id = u.user_id 
INNER JOIN flair f ON flair_id = u.active_flair_id 
     WHERE c.product_id = 456029 
  ORDER BY c.post_date ASC

What is your goal with this query? Surely this can be written better...

How about this:

    SELECT c.comment, username, image, c.post_date
         , IF(b.read_date < c.post_date, 1, 0) AS new_comment 
      FROM comments c
INNER JOIN users u ON c.user_id = u.user_id 
INNER JOIN flair f ON flair_id = u.active_flair_id 
 LEFT JOIN bookmarks b ON (b.product_id = c.product_id AND b.user_id = u.user_id)
     WHERE c.product_id = 456029 
  ORDER BY c.post_date ASC

There is no reason in your query to use a subquery.

Upvotes: 1

Related Questions