如何使用 PHP 计算多个 MySQL 查询的唯一结果?

发布于 2024-11-28 17:01:39 字数 2108 浏览 2 评论 0原文

我有一个可能很简单的问题,但我似乎不知道该怎么做。我正在尝试编写一个查询,该查询计算每个查询的唯一结果的数量,并将值和查询结果的次数存储到不同的数据库。

这是我基本上想做的一个例子...

假设我有一个表“颜色”,

+------------+------------+------------+
|     id     |    color   |   letter   |
+------------+------------+------------+
|          1 | blue       |      a     |
|          2 | blue       |      b     |
|          3 | red        |      a     |
|          4 | red        |      b     |
|          5 | green      |      a     |
+------------+------------+------------+

我有一个查询将使用该表运行多次 查找相应的字母和颜色:

$query = ("SELECT * FROM colors WHERE letter = '$someletter'");
$query1 = mysql_query($query);
while($row = mysql_fetch_array($query1)) {
$id = $row['id'];
$color = $row['color'];
};

对于每个查询,我将 $id 和 $color 变量写入单独的日志文件。

例如,使用上面的查询,$someletter = "a"。 (将此查询称为#1) 结果将是:

+------------+------------+------------+
|     id     |    color   |   letter   |
+------------+------------+------------+
|          1 | blue       |      a     |
|          3 | red        |      a     |
|          5 | green      |      a     |
+------------+------------+------------+

蓝色、红色和绿色各有一个结果。

如果使用 $someletter = "b" 运行查询(将此查询称为 #2) 结果集将是:

+------------+------------+------------+
|     id     |    color   |   letter   |
+------------+------------+------------+
|          2 | blue       |      b     |
|          4 | red        |      b     |
+------------+------------+------------+

蓝色和红色各得到一个结果。

因此,查询 #1 和 #2 的结果总数:

+------------+------------+------------+
|     id     |    color   | totalresult|
+------------+------------+------------+
|          1 | blue       |      2     |
|          3 | red        |      2     |
|          5 | green      |      1     |
+------------+------------+------------+

基本上,我想找到一种方法来获取 X 个查询的所有唯一结果的计数。因为此查询将使用不同的变量运行多次,所以我想将结果存储在数据库中,该数据库可能有一个颜色列和一个总结果列,如我上面所示。另外,我只是使用表“颜色”作为示例,唯一条目的实际数量将有数百个,因此查询必须适用于此。

我正在考虑某种形式的 COUNT 或 GROUP,但我似乎无法将这些点联系起来。

任何帮助将不胜感激。

I have what might be a simple question but I can't seem to figure out how to do it. I am trying to write a query that counts the number of unique results per query and stores both the values and the number of times it was a query result to a different database.

Here's an example of what I am basically trying to do...

Say I have a table 'color'

+------------+------------+------------+
|     id     |    color   |   letter   |
+------------+------------+------------+
|          1 | blue       |      a     |
|          2 | blue       |      b     |
|          3 | red        |      a     |
|          4 | red        |      b     |
|          5 | green      |      a     |
+------------+------------+------------+

I have a query that will run multiple times using this table
to find the corresponding letter and color:

$query = ("SELECT * FROM colors WHERE letter = '$someletter'");
$query1 = mysql_query($query);
while($row = mysql_fetch_array($query1)) {
$id = $row['id'];
$color = $row['color'];
};

For each query I am writing the $id and $color variables to a seperate log file.

For example, using the above query, $someletter = "a". (call this query #1)
The results would be:

+------------+------------+------------+
|     id     |    color   |   letter   |
+------------+------------+------------+
|          1 | blue       |      a     |
|          3 | red        |      a     |
|          5 | green      |      a     |
+------------+------------+------------+

Blue, red, and green would have one result each.

If the query was run with $someletter = "b" (call this query #2)
The result set would be:

+------------+------------+------------+
|     id     |    color   |   letter   |
+------------+------------+------------+
|          2 | blue       |      b     |
|          4 | red        |      b     |
+------------+------------+------------+

Blue and red would each get one result.

So the total number of results for both queries #1 and #2:

+------------+------------+------------+
|     id     |    color   | totalresult|
+------------+------------+------------+
|          1 | blue       |      2     |
|          3 | red        |      2     |
|          5 | green      |      1     |
+------------+------------+------------+

Basically, I want to figure out a way to get a count of all the unique results for X number of queries. Because this query will be run multiple times with different variables, I would like to store the results in a database that could have a column for color and a column for total results as I have shown above. Also, I am just using the table 'color' as an example and the actual number of unique entries would be in the hundreds, so the query would have to work for that.

I was thinking of some form of COUNT or GROUP but I cant seem to connect the dots.

Any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

动次打次papapa 2024-12-05 17:01:39
$query = 'SELECT color, COUNT(letter) totalresult FROM colors GROUP BY color';

这应该会给你颜色和它出现的次数(每种颜色)。我省略了 id 列,因为您无法将一行映射到单个 id,因此该信息是无用的。

更新

为了存储搜索结果,您可以创建两个表。第一个 (search_query) 只需包含一个 id 和一个 varchar 列来保存查询。您还可以添加时间戳来了解人们何时使用搜索选项。

第二个表 (search_results) 每行包含一个结果。它由第一个表的外键和结果组成。您还可以记录结果行的 ID,而不是完整的结果。 (因此,如果有人搜索字母 b,您只需要记录颜色 ID 2 和 4。这意味着您在 search_results 表中需要两行。)

如果搜索结果可能来自多个表,您需要添加表名也添加到搜索结果表中。我只想使用 varchar 列。

$query = 'SELECT color, COUNT(letter) totalresult FROM colors GROUP BY color';

That should give you the colors and the number of times it appears (per color). I've left out the id column, because you can't map a row to a single id so that information is useless.

Update

In order to store the search results you could create two tables. The first (search_query) only needs to contain an id and a varchar column to hold the query. You could add a timestamp also to find out when people use the search option.

The second table (search_results) contains one result per row. It consists of a foreign key to the first table, and the result. Instead of a complete result you also could log the id of the result row. (So if someone searches for the letter b you only would need to log color ids 2 and 4. That means you need two rows in the search_results table.)

If the search results could come from more than one table, you would need to add the table name to the search results table too. I would just use a varchar column for that.

灼疼热情 2024-12-05 17:01:39

您想要使用的是 mysql_num_rows($query1); 这将返回结果中的行数。

what you want to use is mysql_num_rows($query1); this returns the number of rows in the result.

饭团 2024-12-05 17:01:39
select color, count(id) from color group by color
select color, count(id) from color group by color
多情出卖 2024-12-05 17:01:39

我相信您将需要 2 次数据库访问 - 一次用于检索当前总计,另一次用于使用新总计进行更新。

I believe you will need 2 database accesses - one to retrieve the current total, and one to update it with the new total.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文