SELECT查询输出每个单个结果六次

时间:2011-11-19 06:07:32

标签: php mysql

此查询输出每个结果6次:因此,对于一个主体为“ayo”的行,它将输出为:ayo
ayo
ayo
亚青
亚青
亚青

function get_comments($page_type, $spot) {
        $query = mysql_query("
                              SELECT
                              comments.user_id AS user_id,
                              comments.page_type AS page_type,
                              comments.spot AS spot,
                              comments.comment AS comment,
                              comments.timestamp AS timestamp,
                              users.first_name AS first_name,
                              users.last_name AS last_name
                              FROM comments, users
                              WHERE page_type = '$page_type' AND spot = '$spot' AND (comments.user_id = users.user_id OR comments.user_id = '0')
                              ORDER BY timestamp DESC
                             ");

        while($fetch = mysql_fetch_assoc($query)) {

            $comment = $fetch['comment'];
            $timestamp = $fetch['timestamp'];
            $timestamp_date = date('M d y\'', $timestamp);
            $timestamp_time = date('g:m', $timestamp);
            $comments_user_id = $fetch['user_id'];

                if(date('H', $timestamp) > 12) {
                    $am_pm = 'pm';
                    } else {
                        $am_pm = 'am';
                        }

            $first_name = $fetch['first_name'];
            $last_name = $fetch['last_name'];

            ?>

            <div id="replies">

                <?php if($comments_user_id == '0') {echo 'Guest';} else { echo $first_name.' '.$last_name; } ?><br />
                <?php echo $timestamp_date; ?><br />
                <?php echo $comment; ?>

            </div>

            <?php

            }
        }

在这里$ page_type和$ spot请参阅: http://localhost/ $ page_type / $ spot

5 个答案:

答案 0 :(得分:2)

您正在获取重复项,因为您的隐式连接条件包含:

OR comments.user_id = '0'

并且您有多行comments.user_id = 0。这些user_id = 0行中的每一行都会与您的加入条件相匹配,这将导致重复。解决方案是修复您的数据,以便每个评论都有一个有效的user_id,其中“有效”表示“在users中有一个条目”,并删除您的加入条件的comments.user_id = 0部分。当你在它的时候,切换到InnoDB表并添加外键来强制执行这种参照完整性约束。

BTW,如果user_id是一个整数,那么你真的不应该引用它;你正在使数据库做不必要的工作,并养成一个坏习惯,如果你每个人都使用另一个数据库会导致你的痛苦和痛苦。您还应该切换到显式联接:

from comments join users on comments.user_id = users.id
where page_type = '$page_type' and spot = '$spot'

答案 1 :(得分:1)

由于您的(comments.user_id = users.user_id OR comments.user_id = '0')子句,每个访客评论的显示次数与users中的记录一样多。显然,这是六个。

已编辑添加:

我猜你想要这样的东西:

SELECT comments.user_id AS user_id,
       comments.page_type AS page_type,
       comments.spot AS spot,
       comments.comment AS comment,
       comments.timestamp AS timestamp,
       COALESCE(users.first_name, 'Guest') AS first_name,
       COALESCE(users.last_name, 'User') AS last_name
  FROM comments
  LEFT
 OUTER
  JOIN users
    ON users.user_id = comments.user_id
 WHERE comments.page_type = '$page_type'
   AND comments.spot = '$spot'
 ORDER BY comments.timestamp DESC
;

但我同意“mu太短”,你应该重新评估你的桌面设计。使用comments.user_id = 0表示“客人评论”时,如果没有users.user_id = 0的记录,则不是很好,因为它会给您留下“几乎是外键”的关系。完整的外键关系将使您的表结构更容易理解和推理。

答案 2 :(得分:0)

$query = mysql_query("
                      SELECT
                      comments.user_id AS user_id,
                      comments.page_type AS page_type,
                      comments.spot AS spot,
                      comments.comment AS comment,
                      comments.timestamp AS timestamp,
                      users.first_name AS first_name,
                      users.last_name AS last_name
                      FROM comments, users
                      WHERE page_type = '$page_type' AND spot = '$spot' AND (comments.user_id = users.user_id OR comments.user_id = '0')
                      GROUP BY comments.user_id
                      ORDER BY timestamp DESC
                    ");

答案 3 :(得分:0)

我对MySQL不熟悉MS T-SQL。但在我看来,你正在交叉加入你的牌桌。用户和评论之间应该有一个JOIN。否则,每个评论都将加入每个用户。

HERE是MySQL中JOIN细节的链接。

我可能会在这里错过一些关于语法的MySQL特定内容,我可以测试一下,但是这里有:

这将为具有相关评论的用户返回这些记录:

SELECT 
    comments.user_id AS user_id,
    comments.page_type AS page_type,
    comments.spot AS spot,
    comments.comment AS comment,
    comments.timestamp AS timestamp,
    users.first_name AS first_name,
    users.last_name AS last_name
FROM 
    comments INNER JOIN users
    ON comments.user_id = users.user_id
WHERE page_type = '$page_type' 
AND spot = '$spot' 
AND (comments.user_id = users.user_id OR comments.user_id = '0')
ORDER BY timestamp DESC 

并且THIS将返回所有用户记录,并将匹配注释记录:     选择         comments.user_id AS user_id,         comments.page_type AS page_type,         comments.spot AS spot,         comments.com作者AS评论,         comments.timestamp AS时间戳,         users.first_name AS first_name,         users.last_name AS last_name     从         评论INNER JOIN用户     ON comments.user_id = users.user_id     WHERE page_type ='$ page_type'     AND spot ='$ spot'     AND(comments.user_id = users.user_id OR comments.user_id ='0')     ORDER BY时间戳DESC

答案 4 :(得分:0)

while($fetch = mysql_fetch_array($query))

将while语法更改为

并更改此语法代码

WHERE page_type = '$page_type' AND spot = '$spot' AND (comments.user_id = users.user_id OR comments.user_id = '0')

WHERE page_type = '$page_type' AND spot = '$spot' AND (comments.user_id = users.user_id)