简体   繁体   English

Mysql运行计数在另一个查询中是否不同?

[英]Mysql run count distinct inside another query?

I have a mysql query that returns an array of rows. 我有一个mysql查询,它返回一个行数组。 I need to get a count of distinct values from one column. 我需要从一列中获取不同值的计数。 My query is dynamic, where multiple conditions evaluated by php change the WHERE clause, so I really want to do it all inside one query if possible to avoid duplicating every single dynamic condition. 我的查询是动态的,其中php评估的多个条件会更改WHERE子句,因此,我真的想在一个查询中全部执行此操作,以避免重复每个动态条件。

Here is a simple version of what I would like to do -- 这是我想做的一个简单版本-

If this represents a very simple version of my sql table: 如果这表示我的sql表的非常简单的版本:

Column1, Column2, Column3, Group, Active
   a   ,    b   ,    c   ,   1  ,  YES
   d   ,    e   ,    f,  ,   2  ,  YES
   g   ,    h   ,    i   ,   1  ,  YES
   j   ,    k   ,    l   ,   3  ,  NO

I would like to do something like this: 我想做这样的事情:

"SELECT Column1, Column2, Column3, Group,
(SELECT COUNT(DISTINCT Group) FROM 'table') AS totalGroups
 FROM 'table'
WHERE Active like 'YES';"

I want the result to be that I can report: 我希望结果是我可以报告:

a) the total rows in the query a)查询中的总行

mysql_num_rows($result) should equal = 3

b) the total number of distinct groups b)不同组的总数

$phparray['totalgroups'] should equal = 2
(Right now, 'totalgroups' equals = 3)

(1) Can this be done? (1)能做到吗? Currently, 'totalgroups' returns 3 instead of two. 当前,“ totalgroups”返回3而不是2。 Can I get it to use the same "Where" clause as the rest of the query? 我可以使用与其余查询相同的“ Where”子句吗?

In my actual code, which is much more complex, my count is returning 1,945 when I'm only wanting it to return 2. :) 在更复杂的实际代码中,当我只希望它返回2时,我的计数返回1,945。

(2) Is there a better way to do it? (2)有更好的方法吗?

I tried php's array_unique() and array_count_values() , but array_unique() does not work on multidimensional arrays, and I couldn't get array_count_values() to work correctly. 我尝试了php的array_unique()array_count_values() ,但是array_unique()在多维数组上不起作用,并且我无法使array_count_values()正常工作。

I looked at many other questions, but couldn't find one where someone was asking this particular question. 我看了许多其他问题,但找不到有人问这个特定问题的地方。 I reviewed the MYSQL manual RE subqueries, but it didn't address whether you could share the Where clause. 我查看了MYSQL手册RE子查询,但没有解决您是否可以共享Where子句的问题。

If you must have my actual code in order to answer this question (instead of the demo representation above), please let me know. 如果您必须拥有我的实际代码才能回答此问题(而不是上面的演示表示),请告诉我。 It's over two pages with all the php dynamic qualifiers, so I decided to write demo code for clarity's sake. 所有的php动态限定符超过两页,因此为了清晰起见,我决定编写演示代码。

Are you trying to get a list of all the records on the table, along with on every line a count of the number of active rows and a count of the unique active groups? 您是否要获取表中所有记录的列表,以及每一行的活动行数和唯一活动组数?

If so then something like this:- 如果是这样,那么是这样的:

SELECT Column1, Column2, Column3, Group, ActiveDistinctGroups.ActiveCnt, ActiveGroups.ActiveCnt
FROM 'table' a
CROSS JOIN (SELECT COUNT(DISTINCT Group) AS ActiveCnt FROM 'table' WHERE Active like 'YES') AS ActiveDistinctGroups
CROSS JOIN (SELECT COUNT(Group) AS ActiveCnt FROM 'table' WHERE Active like 'YES') AS ActiveGroups
WHERE Active like 'YES'

If you really need to avoid the extra WHERE clauses:- 如果您确实需要避免多余的WHERE子句:

SELECT Column1, Column2, Column3, Group, ActiveDistinctGroups.ActiveCnt, ActiveGroups.ActiveCnt
FROM 'table' a
INNER JOIN (SELECT Active, COUNT(DISTINCT Group) AS ActiveCnt FROM 'table' GROUP BY Active) AS ActiveDistinctGroups ON a.Active = ActiveDistinctGroups.Active
INNER JOIN (SELECT ActiveCOUNT(Group) AS ActiveCnt FROM 'table' GROUP BY Active) AS ActiveGroups ON a.Active = ActiveGroups.Active
WHERE a.Active like 'YES'

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

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