Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

4 Answers

$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.

share|improve this answer
Thanks, but I am trying to figure out the query to create the table with the total results column. I want to find out how to compile the unique results queries 1 and 2 into a database. Once I have that database I know how to extract the counts, I just cant make the table to begin with. – Jeff Aug 9 '11 at 0:47
Can't you just create another table called QueryResults and for each time a query is run, populate that table with the rows. It would also help if you added a time-stamp column so you can later know when the query was run that produced the result. – DeviantSeev Aug 9 '11 at 17:45
@Jeff: I updated my answer with query/results table layouts. – Arjan Aug 9 '11 at 20:19

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

share|improve this answer
select color, count(id) from color group by color
share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.