Повторяющиеся записи с мульти союза - PullRequest
0 голосов
/ 07 сентября 2011

У меня есть

               (SELECT  DISTINCT '1' as sb,nome,id,sobrenome,nasc,genero,escol FROM usuarios WHERE nome          REGEXP 'leidiana da'    AND tipo <> '7') 
UNION DISTINCT (SELECT  DISTINCT '2' as sb,nome,id,sobrenome,nasc,genero,escol FROM usuarios WHERE sobrenome     REGEXP 'da silva dias'  AND tipo <> '7') 
UNION DISTINCT (SELECT  DISTINCT '3' as sb,nome,id,sobrenome,nasc,genero,escol FROM usuarios WHERE nome          REGEXP 'leidiana'       AND sobrenome      REGEXP 'da silva dias'   AND tipo <> '7') 
UNION DISTINCT (SELECT  DISTINCT '4' as sb,nome,id,sobrenome,nasc,genero,escol FROM usuarios WHERE nome          REGEXP 'leidiana da'    AND sobrenome      REGEXP 'silva dias'      AND tipo <> '7') 
UNION DISTINCT (SELECT  DISTINCT '5' as sb,nome,id,sobrenome,nasc,genero,escol FROM usuarios WHERE sobrenome     REGEXP 'silva dias'     AND tipo <> '7') 
UNION DISTINCT (SELECT  DISTINCT '6' as sb,nome,id,sobrenome,nasc,genero,escol FROM usuarios WHERE nome          REGEXP 'leidiana'       AND tipo <> '7') 
UNION DISTINCT (SELECT  DISTINCT '7' as sb,nome,id,sobrenome,nasc,genero,escol FROM usuarios WHERE nome          REGEXP 'leidiana'       AND tipo <> '7') 
UNION DISTINCT (SELECT  DISTINCT '8' as sb,nome,id,sobrenome,nasc,genero,escol FROM usuarios WHERE sobrenome     REGEXP 'leidiana'       AND tipo <> '7') 
UNION DISTINCT (SELECT  DISTINCT '9' as sb,nome,id,sobrenome,nasc,genero,escol FROM usuarios WHERE sobrenome     REGEXP 'leidiana da'    AND tipo <> '7')   ORDER BY sb

Результат:

2 - Лейдиана да Силва Диас 3 - Лейдиана да Силва Диас 4 - Лейдиана да Силва Диас 5 - Лейдиана да Силва Диас 6 - Лейдиана да Силва Диас

7 - leidiana da silva dias

Как видите, я попытался решить проблему и в итоге все обрисовал. RS

1 Ответ

1 голос
/ 07 сентября 2011

Вы заставляете результаты по профсоюзам отличаться от первого столбца (sb).Возможно, вы могли бы решить проблему с помощью внешнего GROUP BY:

SELECT min(sb) AS sb, name, id,sobrenome,nasc,genero,escol
FROM (
      (SELECT  '1' as sb,nome,id,sobrenome,nasc,genero,escol FROM usuarios WHERE nome          REGEXP 'leidiana da'    AND tipo <> '7') 
UNION (SELECT  '2' as sb,nome,id,sobrenome,nasc,genero,escol FROM usuarios WHERE sobrenome     REGEXP 'da silva dias'  AND tipo <> '7') 
UNION (SELECT  '3' as sb,nome,id,sobrenome,nasc,genero,escol FROM usuarios WHERE nome          REGEXP 'leidiana'       AND sobrenome      REGEXP 'da silva dias'   AND tipo <> '7') 
UNION (SELECT  '4' as sb,nome,id,sobrenome,nasc,genero,escol FROM usuarios WHERE nome          REGEXP 'leidiana da'    AND sobrenome      REGEXP 'silva dias'      AND tipo <> '7') 
UNION (SELECT  '5' as sb,nome,id,sobrenome,nasc,genero,escol FROM usuarios WHERE sobrenome     REGEXP 'silva dias'     AND tipo <> '7') 
UNION (SELECT  '6' as sb,nome,id,sobrenome,nasc,genero,escol FROM usuarios WHERE nome          REGEXP 'leidiana'       AND tipo <> '7') 
UNION (SELECT  '7' as sb,nome,id,sobrenome,nasc,genero,escol FROM usuarios WHERE nome          REGEXP 'leidiana'       AND tipo <> '7') 
UNION (SELECT  '8' as sb,nome,id,sobrenome,nasc,genero,escol FROM usuarios WHERE sobrenome     REGEXP 'leidiana'       AND tipo <> '7') 
UNION (SELECT  '9' as sb,nome,id,sobrenome,nasc,genero,escol FROM usuarios WHERE sobrenome     REGEXP 'leidiana da'    AND tipo <> '7')) sbs
GROUP BY name, id,sobrenome,nasc,genero,escol
ORDER BY sb
...