简体   繁体   English

MySQL PHP查询问题

[英]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. 在数据库中,我有两个字段-q1,q2。

I want to first count, order and group response for q1 - easy enough. 我想先对q1进行计数,排序和分组响应-很简单。

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? 很好,但是有一种方法可以在每次第一个查询带回结果时在循环中循环访问上述内容,因此在此示例中,针对q1 = A,q1 = B和q1迭代并循环执行2的代码。 = C?

Make sense? 说得通? I'm not sure how well I have described that, so I apologise. 我不确定我对此的描述程度如何,所以我表示歉意。

You can achieve this using single query as: 您可以使用单个查询来实现此目的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM