0

表格1

id | author       | number_of_view |
---+--------------+-----------------
1  | john         | 10    
2  | Jack         | 20   
3  | Anna         | 80    
4  | Neri         | 100  

下面我有一个 PHP 脚本,它检查 MySQL 数据库表并向用户提供徽章。该脚本完美运行。

$from1=10;
$to1=15;

$from2=20;
$to2=30;

$query=mysql_query("SELECT SUM( number_of_view ) AS view_number, author FROM `table1`  GROUP BY author");

 while ($row=  mysql_fetch_array($query))
        {
            $view_number=$row['view_number'];
            $author=$row['author'];


            if ($viewnumber>$from1 && $viewnumber<=$to1)
            {
                echo "special user";

            }
            elseif ($viewnumber>$from2 && $viewnumber<=$to2)
            {
                echo "plain user";
            }
        }
}

但问题是我想从数据库表中获取$from变量$to值:

表2

id | badge       | from |to
---+-------------+------+---
1  | special user| 10   |15
2  | plain user  | 20   |30

这是第二个表的第二个查询和 PHP 脚本:

$query2=mysql_query("SELECT * FROM table2");
while ($row= mysql_fetch_array($query2)) {
    $badgeName=$row['view_number'];
 //I would like to use this variable above. So instead of "echo 'special user';" I would like use "echo $badgeName" $from=$row['author'];
// I would like to use this variable above instead of $from1 and $from2 $to=$row['to'];
// I would like to use this variable above instead of $to1 and $to2 }

我怎样才能做到这一点?

4

2 回答 2

1

我最初认为这很简单,但我错了,因为你必须注意一些复杂的因素。

首先,您必须对两个表进行交叉乘积。最简洁的方法是在FROM语句中有两个表。你会得到这样的东西:

SELECT SUM(number_of_view) AS view_number, author, badge
FROM table1, table2

这将为每个徽章从第一个表中的每一行一次。我们还没有总结意见,所以这是我们接下来要做的事情。第一次尝试是这样的:

SELECT SUM(number_of_view) AS view_number, author, badge
FROM table1, table2
GROUP BY number_of_view

但是,您现在会看到视图计数不正确,我们只会看到每个人一个徽章,而我们希望每个用户的每个徽章都有一行。事实上,徽章也是分组的,这就是为什么显示的 number_of_view 是实际视图数乘以系统中的徽章数量的原因。为了解决这个问题,我们确实将徽章按列添加到分组中,因此它不会被压缩到其他结果中:

SELECT SUM(number_of_view) AS view_number, author, badge
FROM table1, table2
GROUP BY number_of_view, badges

如果你的第二个表有一个 id 列,我会改用它,因为它对性能更好(取决于用于徽章字段的实际类型),但表中没有给出,这确实有效。

现在,我们需要过滤掉用户没有获得的徽章,以确保只剩下正确的徽章。这将是这样的:

SELECT SUM(number_of_view) AS view_number, author, badge
FROM table1, table2
GROUP BY number_of_view, badges
HAVING number_of_view >= table2.`from`
AND    number_of_view <  table2.to

(请注意,我必须转义,因为它是 SQL 中的关键字。)但是,在这里我们会发现查询不知道 table2.from 列。这是因为HAVING查询的一部分不查看表(WHERE这样做),而是查看选定的列(因为那是由 过滤的GROUP BY)。因此,我们需要将这些列添加到选定的列中:

SELECT SUM(number_of_view) AS view_number, author, badge, `from`, to
FROM table1, table2
GROUP BY number_of_view, badges
HAVING number_of_view >= table2.`from`
AND    number_of_view <  table2.to

我们终于得到了我们想要的。您可以在此处查看实际查询:http ://sqlfiddle.com/#!8/e78c8/1

于 2013-08-10T01:35:22.827 回答
0

您应该在两个表之间运行 JOIN 查询。我已将 table2 字段名称从“from”和“to”修改为“start”和“end”,因为 FROM 是保留字。

SELECT author, number_of_view, badge
FROM authors LEFT JOIN scores 
ON score >start AND score < end

请注意,上面的 JOIN 查询将返回 table1 中的所有结果,即使它们在 table2 中没有匹配项。在这种情况下,徽章值将为 NULL。如果您不想获取这些值,可以将 LEFT JOIN 替换为 INNER JOIN。

于 2013-08-10T00:55:10.183 回答