Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to count unique results from multiple MySQL queries using PHP?
    primarykey
    data
    text
    <p>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.</p> <p>Here's an example of what I am basically trying to do...</p> <p>Say I have a table 'color'</p> <pre><code>+------------+------------+------------+ | id | color | letter | +------------+------------+------------+ | 1 | blue | a | | 2 | blue | b | | 3 | red | a | | 4 | red | b | | 5 | green | a | +------------+------------+------------+ </code></pre> <p>I have a query that will run multiple times using this table to find the corresponding letter and color:</p> <pre><code>$query = ("SELECT * FROM colors WHERE letter = '$someletter'"); $query1 = mysql_query($query); while($row = mysql_fetch_array($query1)) { $id = $row['id']; $color = $row['color']; }; </code></pre> <p>For each query I am writing the $id and $color variables to a seperate log file. </p> <p>For example, using the above query, $someletter = "a". (call this query #1) The results would be:</p> <pre><code>+------------+------------+------------+ | id | color | letter | +------------+------------+------------+ | 1 | blue | a | | 3 | red | a | | 5 | green | a | +------------+------------+------------+ </code></pre> <p>Blue, red, and green would have one result each.</p> <p>If the query was run with $someletter = "b" (call this query #2) The result set would be:</p> <pre><code>+------------+------------+------------+ | id | color | letter | +------------+------------+------------+ | 2 | blue | b | | 4 | red | b | +------------+------------+------------+ </code></pre> <p>Blue and red would each get one result.</p> <p>So the total number of results for both queries #1 and #2:</p> <pre><code>+------------+------------+------------+ | id | color | totalresult| +------------+------------+------------+ | 1 | blue | 2 | | 3 | red | 2 | | 5 | green | 1 | +------------+------------+------------+ </code></pre> <p>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.</p> <p>I was thinking of some form of COUNT or GROUP but I cant seem to connect the dots.</p> <p>Any help would be much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload