比如:
先按照 站点分组,再按照 设备分组,
即:如果站点上配置了2个设备。就筛选出来。
查出配置了2个设备的站点
SELECT
e2.station,
e2.device
FROM
esb_config e2
GROUP BY
e2.station
HAVING
count( 1 ) > 1
SELECT
e2.station,
e2.device
FROM
esb_config e2
GROUP BY e2.station,e2.device HAVING count(*) > 1
-- 这样是错误的,这样是根据2个字段 分组。结果不会存在 > 1的情况
GROUP BY e2.station HAVING count(e2.device) > 1
-- 这样写无异议 等于 HAVING count(e2.station) > 1,也是错误的
GROUP BY e2.station
-- 核心是:这样查询后,会随机带出 一个device。
-- 所以哪怕 外层在套一个分组(因为经过一层后,已经选出了一个device了),也是错的。
CREATE TABLE `user` (
`id` int NOT NULL,
`username` varchar(22) DEFAULT NULL,
`info` varchar(22) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
1 zhangsan 1
2 lisi 22
3 lisi 33
-- 我们要查出lisi
-- 先按照姓名分组,在按照 info 分组。
-- info中的 数据>1,就查询出来
collate
英
/kəˈleɪt/
vt.
核对,校对;校勘
SELECT *
FROM `user`
GROUP BY username
-- 按照username分组后,lisi会随机选一条(第一条)
1 zhangsan 1
2 lisi 22
INSERT INTO `user`(`id`, `username`, `info`) VALUES (4, 'zhangsan', '1');
-- 此时数据的数据为
1 zhangsan 1
2 lisi 22
3 lisi 33
4 zhangsan 1
SELECT *
FROM `user`
GROUP BY username
HAVING count(username) > 1
-- 结果为:此时zhangsan是info相同的,不应该出现。count(1),或 count(其他值) 结果都一样
1 zhangsan 1
2 lisi 22
-- 此时数据的数据为
1 zhangsan 1
2 lisi 22
3 lisi 33
4 zhangsan 1
SELECT *
FROM `user`
GROUP BY username,info
-- 根据 两个字段分组,后变成3组
1 zhangsan 1
2 lisi 22
3 lisi 33
SELECT *
FROM `user`
GROUP BY username,info
HAVING count(1) > 1
-- 结果
1 zhangsan 1
业务要求了,必须是 一个站点绑定两个设备都是屏幕,才查询出来。
无用的SQL保存,验证这个真实的设备是不是 屏幕
SELECT
*
FROM
device d,
device_type dt
WHERE
d.deviceType = dt.id
AND d.id = 'e696cfeeb4568ccfcda0ae6787388760'
AND dt.attribute = '屏幕'
SELECT
*
FROM
(
SELECT
e2.station,
ROW_NUMBER() OVER ( PARTITION BY e2.station ) row_num
FROM
esb_config e2,
device d,
device_type dt
WHERE
d.deviceType = dt.id
AND dt.attribute = '屏幕'
AND e2.device = d.id
) t1
WHERE
t1.row_num > 1
-- 窗口函数 再次赋值
SELECT
*
FROM
(
SELECT
e2.station,
ROW_NUMBER() OVER ( PARTITION BY e2.station ) row_num
FROM
esb_config e2
) t1
WHERE
t1.row_num > 1
SELECT
e2.station,
e2.device
FROM
esb_config e2
GROUP BY
e2.station
HAVING
count( 1 ) > 1
SELECT
e2.station,
e2.device
FROM
esb_config e2,
device d,
device_type dt
WHERE
d.deviceType = dt.id
AND dt.attribute = '屏幕'
AND e2.device = d.id
-- 上面SQL 查出了配置表中,所有为屏幕的 设备和站点
-- 此时一个站点 如果配置了 多个屏幕,这个站点就会展示出多条
-- 然后在筛选一下,站点>1 的
GROUP BY
e2.station
HAVING
count( 1 ) > 1
思路1再关联原表
使用这个逻辑,有一个问题。怎么显示 一个站点,显示2次呢(设备不同的时候)
SELECT * from esb_config e1, (
SELECT
e2.station
FROM
esb_config e2,
device d,
device_type dt
WHERE
d.deviceType = dt.id
AND dt.attribute = '屏幕'
AND e2.device = d.id
GROUP BY
e2.station
HAVING
count( 1 ) > 1
) t1
WHERE e1.station = t1.station
-- 核心是这样的结果
SELECT * from esb_config e1, (
SELECT
e2.station -- 这里,e2.device 取了没用,只是会筛选第一个
FROM
esb_config e2
GROUP BY
e2.station
HAVING
count( e2.station ) > 1
) t1
WHERE e1.station = t1.station
-- 那这个结果加上限制对不对呢?不对的,为啥不对,这里不懂。
-- AND dt.attribute = '屏幕'
把一级分组,查询出来
把二级分组,查询出来
如果 一级分组 和 二级分组,关联上就展示
这样 查出的数据 只查出了一条(也是上面的 数据之一)
SELECT
*
FROM
(
SELECT * FROM (
SELECT
e2.station,
e2.device
FROM
esb_config e2,
device d,
device_type dt
WHERE
e2.device = d.id
AND d.deviceType = dt.id
AND dt.attribute = '屏幕'
) t2
GROUP BY t2.station HAVING count(t2.station) > 1
) t1,
(SELECT * FROM (
SELECT
e2.station,
e2.device
FROM
esb_config e2,
device d,
device_type dt
WHERE
e2.device = d.id
AND d.deviceType = dt.id
AND dt.attribute = '屏幕'
) t2
GROUP BY t2.device HAVING count(t2.device) > 1
) t3
WHERE
t1.station = t3.station and t1.device = t3.device