为什么别名会导致我的查询在phpMyAdmin中失败?

时间:2011-12-19 15:56:36

标签: php mysql phpmyadmin

以下查询在我的php脚本中运行正常,但是当我在phpMyAdmin中运行它(针对同一个数据库)时,我没有得到任何结果(甚至没有“0结果” - 它只是刷新页面)。

  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

我注意到如果我取出别名,查询将在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

我很好奇为什么会这样。 (运行phpMyAdmin - 2.11.9.5)

编辑:Richard的重新制定在phpMyAdmin中有效。所以现在的难题是为什么一个配方工作而另一个没有(当它们在PHP脚本中执行时它们都工作正常)。

根据Richard的建议删除子查询后的查询的最终形式:

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 

1 个答案:

答案 0 :(得分:1)

你有一个非常奇怪的查询,我也不接受它......

   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

重写:

     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

此查询的目标是什么?当然这可以写得更好......

这个怎么样:

    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

您的查询中没有理由使用子查询。