1
+------------+---------------+
| BR_NAME    | FACILITIES    |
+------------+---------------+
| headoffice | fixeddeposit  |
| headoffice | locker        |
| headoffice | mobilebanking |
| headoffice | netbanking    |
| zone1      | fixeddeposit  |
| zone1      | mobilebanking |
| zone1      | netbanking    |
| zone2      | fixeddeposit  |
| zone2      | netbanking    |
| zone3      | fixeddeposit  |
| zone3      | locker        |
| zone3      | netbanking    |
| zone4      | netbanking    |
| zone5      | locker        |
| zone6      | fixeddeposit  |
| zone6      | locker        |
| zone7      | locker        |
| zone7      | mobilebanking |
| zone8      | locker        |
| zone8      | mobilebanking |
| zone9      | locker        |
+------------+---------------+

这是我的数据库,我想找到具有相同多值属性的 br_name,例如:zone7,zone8 如何使用 mysql 查询它

4

2 回答 2

2

group_concat你可以使用(也请参见小提琴)做这样的事情

CREATE TEMPORARY TABLE tt (BR_NAME VARCHAR(50),ALLFACILITIES VARCHAR(50));

INSERT INTO tt
(SELECT
    BR_NAME,
    GROUP_CONCAT(FACILITIES)
FROM
    (SELECT * FROM t ORDER BY FACILITIES) as t1
GROUP BY
    BR_NAME);

SELECT
  GROUP_CONCAT(BR_NAME) AS BR_NAMES,
  ALLFACILITIES
FROM
  tt
GROUP BY
  ALLFACILITIES
HAVING
  COUNT(*) > 1

结果:

BR_NAMES        ALLFACILITIES

zone9,zone5     locker
zone7,zone8     mobilebanking,locker

请注意,您可以删除COUNT(*) > 1并获取每个 BR_NAME 的设施。

于 2013-04-26T05:51:00.370 回答
0

您可以使用 group by 和 have 来做到这一点。

SELECT * FROM TableName group by BR_NAME having count(*) >= 2

或者

SELECT *, COUNT(*) c FROM TableName GROUP BY BR_NAME HAVING c > 1;
于 2013-04-26T04:28:07.880 回答