4

让我们考虑这张表,它指定了一个人购买了多少次房产。

+--------+----------+
|  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 |
+----------+---+---+
  • 一辆车买了多少次?彼得两,约翰两。
  • 一辆车被买了多少次?,对于同一个人。
  • 房子买了多少次?阿曼达两次,约翰一次。
  • 房子被买了多少次?只有一次,对于阿曼达

是否可以仅使用 SQL 查询来做到这一点?

  • 我不在乎性能或骇人听闻的方式。
  • 有两个以上的频率。
  • 购买房产的时间是固定的 (5),因此在查询中手动指定列是没有问题的。我的意思是做类似的事情没有问题:

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

7 回答 7

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`
于 2013-02-27T01:18:04.603 回答
1

SQL小提琴

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

结果

| PROPERTY | TOTAL |     C1 | C2 |     C3 |
-------------------------------------------
|      car |     4 | (null) |  2 | (null) |
|    house |     3 |      1 |  1 | (null) |
于 2013-02-27T01:19:49.460 回答
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;

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

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

小提琴链接就在这里

PS: 60 次尝试 O_O

于 2013-02-27T00:52:35.730 回答
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 中进行了类似的测试

于 2013-02-27T01:25:03.893 回答
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

演示 SQL 小提琴在这里

于 2013-02-27T00:24:28.410 回答
0

I hope this can help u.....let us create one table first:

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

insert into prop values('john','car'),insert into prop values('john','car'), insert into prop values('john','house'),insert into prop values('peter','car'),

insert into prop values('peter','car'),insert into prop values('amanda','house'), insert into prop values('amanda','house')

1)how many times car was bought?

ANS: select count(property) from prop where property = 'car' (4)

2)How many times a car was bought twice?

ANS: select user,COUNT(property) from prop where property = 'car' group by user having COUNT(property) = 2

2-john 2-peter

3)How many times a house was bought?

ANS: select COUNT(property) from prop where property = 'house' (3) 4)How many times a house was bought twice? ANS: select user,COUNT(property) from prop where property='house' group by user having COUNT(property)< =2 2-amanda 1-john

于 2013-02-27T06:35:31.307 回答
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;

它与您说明的格式不完全一样,但它返回数据

于 2013-02-27T00:35:22.930 回答