我有以下查询:
SELECT * FROM (
SELECT codigo, protocolo, status, nome
FROM protocolo
GROUP BY protocolo.protocolo
UNION ALL
SELECT codigo, protocolo, status, nome
FROM simulador
) tabela
返回
codigo protocolo status nome
559 2016000026 1 ALESSANDRO CAMPOS BONIFACIO
0 2016000026 0 ALESSANDRO CAMPOS BONIFACIO
0 2016000008 0 MARIA DE JESUS F. DA SILVA ***
0 2016000007 0 MARGARIDA BORGES DA SILVA
558 2016000008 1 MARIA DE JESUS F. DA SILVA ***
556 2015014035 1 MARIA DALVA DA SILVA
有两个状态相同(0,1)的相同协议(2016000008)。我只想显示重复的协议之一,状态为1
最佳答案
这是你想要的吗?
SELECT odigo, protocolo, MAX(status) as stat, nome
FROM (
SELECT codigo, protocolo, status, nome
FROM protocolo
GROUP BY protocolo.protocolo
UNION ALL
SELECT codigo, protocolo, status, nome
FROM simulador
) tabela
GROUP BY codigo, protocolo, nome ;
注意:在
GROUP BY
查询中,SELECT
中的所有列都应位于GROUP BY
或聚合函数中,除非您真的非常了解自己在做什么。