计算多个值的不同行数

时间:2013-02-27 00:15:27

标签: mysql sql distinct

让我们考虑一下这个表格,指明一个人购买房产的次数。

+--------+----------+
|  user  | property |
+--------+----------+
| john   | car      |
| john   | car      |
| john   | house    |
| peter  | car      |
| peter  | car      |
| amanda | house    |
| amanda | house    |
+--------+----------+

我需要知道一次购买汽车的次数,一次购买房屋的次数,等等。这样的事情:

+----------+---+---+
| property | 1 | 2 |
+----------+---+---+
| cars     | 4 | 2 |
| house    | 3 | 1 |
+----------+---+---+
  • 购买汽车多少次? ,两个用于 peter ,两个用于 john
  • 两次购买汽车多少次? 两个,对于同样的人。
  • 买房子多少次? 三个,两个用于 amanda ,一个用于 john
  • 两次买房子的次数是多少次?对于 amanda
  • ,只有一次

仅使用SQL查询才能实现此目的吗?

  • 我不关心表演或黑客行为。
  • 频率超过两个。
  • 一个人可以购买房产的固定时间(5),因此在查询中手动指定列并不成问题。我的意思是做出类似的事情没有问题:

    SELECT /* ... */ AS 1, /* ... */ AS 2, /* ... */, AS 3 /* ... */
    

7 个答案:

答案 0 :(得分:1)

SELECT DISTINCT @pr := prop,
    (SELECT COUNT(1) FROM tbl WHERE prop = @pr LIMIT 1),
    (SELECT COUNT(1) FROM 
        (SELECT *, COUNT(*) cnt
        FROM tbl
        GROUP BY usr, prop
        HAVING cnt = 2) as tmp
        WHERE `tmp`.prop = @pr LIMIT 1)
FROM tbl;

是的,这不是最好的方法;但是,嘿,你可以得到你想要的答案。

此外,它还会为您表格中的任何类型的属性生成结果。

小提琴链接lies here

P.S。:60次尝试O_O

答案 1 :(得分:1)

我在这里发布了这个问题。好的... ... 这是一种完全按照你的要求去做的方法,只有组和数量 诀窍是我连接用户和属性列,为每个列生成一个唯一的“id”,如果我们可以调用它。它应该独立于购买计数。

SELECT C.`property`, COUNT(C.`property`), D.`pcount` from `purchases` C
LEFT JOIN(
  SELECT A.`property`, B.`pcount` FROM `purchases` A
  LEFT JOIN (
    SELECT `property`,
           CONCAT(`user`, `property`) as conc,
           COUNT(CONCAT(`user`, `property`)) as pcount
    FROM `purchases` GROUP BY CONCAT(`user`, `property`)
  ) B
  ON A.`property` = B.`property`
  GROUP BY B.pcount
) D
ON C.`property` = D.`property`
GROUP BY C.`property`

答案 2 :(得分:1)

SQL Fiddle

MySQL 5.5.30架构设置

CREATE TABLE Table1
    (`user` varchar(6), `property` varchar(5))
;

INSERT INTO Table1
    (`user`, `property`)
VALUES
    ('john', 'car'),
    ('john', 'car'),
    ('john', 'house'),
    ('peter', 'car'),
    ('peter', 'car'),
    ('amanda', 'house'),
    ('amanda', 'house')
;

查询1

select t.property, t.total, c1.cnt as c1, c2.cnt as c2, c3.cnt as c3
from
  (select 
    t.property , 
    count(t.property) as total
  from Table1 t
  group by t.property
  ) as t
  left join (
      select property, count(*) as cnt
      from (
          select 
            property, user, count(*) as cnt
          from table1
          group by property, user
          having count(*) = 1
        ) as i1
      group by property
  ) as c1 on t.property = c1.property
  left join (
      select property, count(*) as cnt
      from (
          select 
            property, user, count(*) as cnt
          from table1
          group by property, user
          having count(*) = 2
        ) as i2
      group by property
  ) as c2 on t.property = c2.property
  left join (
      select property, count(*) as cnt
      from (
          select 
            property, user, count(*) as cnt
          from table1
          group by property, user
          having count(*) = 3
        ) as i3
      group by property
  ) as c3 on t.property = c3.property

<强> Results

| PROPERTY | TOTAL |     C1 | C2 |     C3 |
-------------------------------------------
|      car |     4 | (null) |  2 | (null) |
|    house |     3 |      1 |  1 | (null) |

答案 3 :(得分:1)

您可以尝试跟随。

SELECT COUNT(TABLE1.PROPERTY) AS COUNT, PROPERTY.USER FROM TABLE1
INNER JOIN (SELECT DISTINCT PROPERTY, USER FROM TABLE1) AS PROPERTY
ON PROPERTY.PROPERTY = TABLE1.PROPERTY
AND PROPERTY.USER = TABLE1.USER
GROUP BY TABLE1.USER, PROPERTY.PROPERTRY

在MySQL中测试类似

答案 4 :(得分:0)

试试这个

    SELECT property , count(property) as bought_total , count(distinct(user)) bought_per_user
    FROM Table1
    GROUP BY property

输出就像那样

  PROPERTY  |   BOUGHT_TOTAL     |  BOUGHT_PER_USER
  ________________________________________________________
    car     |            4       |       2
    house   |            3       |       2

DEMO SQL FIDDLE HERE

答案 5 :(得分:0)

你应该能够通过子选择来做到这一点。

SELECT property, user, COUNT(*) FROM purchases GROUP BY property, user;

将返回您想要的完整分组数据集。然后,您需要查看不同的频率:

SELECT property, freq, COUNT(*) FROM (SELECT property, user, COUNT(*) freq FROM purchases GROUP BY property, user) AS foo GROUP BY property, freq;

它并不完全符合您所说明的格式,但会返回数据

答案 6 :(得分:0)

我希望这可以帮助你......让我们先创建一个表:

create table prop(user varchar(max),property varchar(max))

插入道具值('john','car'),插入道具值('john','car'), 插入道具值('john','house'),插入道具值('peter','car'),

插入道具值('peter','car'),插入道具值('amanda','house'), 插入道具值('amanda','house')

1)买了多少次汽车?

ANS:从道具中选择count(属性),其中property ='car' (4)

2)两次购买汽车的次数是多少次?

ANS:从prop中选择用户,COUNT(属性),其中property ='car'按用户分组  有COUNT(财产)= 2

2 - 约翰 2-彼得

3)房子买了多少次?

ANS:从prop中选择COUNT(属性),其中property ='house' (3) 4)两次买房子的次数是多少次? ANS:从prop中选择用户,COUNT(属性),其中property ='house'按用户分组 有COUNT(财产)&lt; = 2 2-阿曼达 1-约翰