Homer_J
Homer_J

Reputation: 3323

MySQL PHP query issue

I'm trying to work out the best way to perform the following:

In the database I have two fields - q1, q2.

I want to first count, order and group response for q1 - easy enough.

1. SELECT q1, COUNT(q1) FROM xyz GROUP BY q1 ORDER BY q1

Now, for arguments sake, let's assume that this brings back the following:

A = 5
B = 12
C = 3

The I need the following information via this query:

2. SELECT q2, COUNT(q2) FROM xyz WHERE q1 = A GROUP BY q2 ORDER by q2 

Which is fine but is there a way to iterate through the above in a Loop for each time the first query brings back a result, so in this example the code for 2. is iterated and looped for q1 = A, q1 = B and q1 = C?

Make sense? I'm not sure how well I have described that, so I apologise.

Upvotes: -1

Views: 64

Answers (1)

Omesh
Omesh

Reputation: 29081

You can achieve this using single query as:

SELECT q2, COUNT(q2)
FROM xyz
GROUP BY q1, q2
ORDER by q1, q2;

Upvotes: 1

Related Questions