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.
$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.
what you want to use is mysql_num_rows($query1);
this returns the number of rows in the result.
select color, count(id) from color group by color
我相信您将需要2次数据库访问-一次检索当前总数,以及一次用新的总数更新。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.