多个 UNION 返回乱七八糟的结果
Multiple UNION returning messy results
我正在做作业,目前我有三张桌子。
[veiculos]
-placa (PK)
-categoria
-categoria_valor
-cor
-ano
[infracao]
-placa (PK)
-codigo
-descricao
-valor
-umt
-data
-hora
-local
[proprietario]
-placa (PK)
-nome
-endereco
我必须执行 SQL 查询才能得到结果 placa, nome, endereco, codigo, descricao, valor, umt, data, hora, local, categoria, categoria_valor, cor, ano
我正在使用这个:
SELECT placa, null AS nome, null AS endereco, codigo, descricao, data, hora, local, valor, umt FROM infracao WHERE (placa=[@PLACA])
UNION
SELECT null AS placa, nome, endereco, null, null, null, null, null AS tipo, null, null FROM proprietario WHERE (placa=[@PLACA])
UNION
SELECT null, null, null, null, null, null, categoria AS tipo, categoria_valor, cor, ano FROM veiculos WHERE (placa=[@PLACA])
;
结果很乱,它必须在单列上 return 但使用三列,如您在此处看到的:https://i.imgur.com/F1xp8x9.png
我该如何解决?你能帮助我吗?我正在使用 MS Access 2013
谢谢。
使用子查询和 max()
select
max(placa) as laca,
max(nome) as nome,
max(endereco) as endereco,
max(codigo) as codigo,
max(descricao) as descricao,
max(data) as data,
max(hora) as hora,max(local) as local,max(valor) as valor,
max(umt) as umt from
(SELECT placa, null AS nome, null AS endereco, codigo, descricao, data, hora, local, valor, umt FROM infracao WHERE (placa=[@PLACA])
UNION
SELECT null AS placa, nome, endereco, null, null, null, null, null AS tipo, null, null FROM proprietario WHERE (placa=[@PLACA])
UNION
SELECT null, null, null, null, null, null, categoria AS tipo, categoria_valor, cor, ano FROM veiculos WHERE (placa=[@PLACA])
) a
我正在做作业,目前我有三张桌子。
[veiculos]
-placa (PK)
-categoria
-categoria_valor
-cor
-ano
[infracao]
-placa (PK)
-codigo
-descricao
-valor
-umt
-data
-hora
-local
[proprietario]
-placa (PK)
-nome
-endereco
我必须执行 SQL 查询才能得到结果 placa, nome, endereco, codigo, descricao, valor, umt, data, hora, local, categoria, categoria_valor, cor, ano
我正在使用这个:
SELECT placa, null AS nome, null AS endereco, codigo, descricao, data, hora, local, valor, umt FROM infracao WHERE (placa=[@PLACA])
UNION
SELECT null AS placa, nome, endereco, null, null, null, null, null AS tipo, null, null FROM proprietario WHERE (placa=[@PLACA])
UNION
SELECT null, null, null, null, null, null, categoria AS tipo, categoria_valor, cor, ano FROM veiculos WHERE (placa=[@PLACA])
;
结果很乱,它必须在单列上 return 但使用三列,如您在此处看到的:https://i.imgur.com/F1xp8x9.png
我该如何解决?你能帮助我吗?我正在使用 MS Access 2013
谢谢。
使用子查询和 max()
select
max(placa) as laca,
max(nome) as nome,
max(endereco) as endereco,
max(codigo) as codigo,
max(descricao) as descricao,
max(data) as data,
max(hora) as hora,max(local) as local,max(valor) as valor,
max(umt) as umt from
(SELECT placa, null AS nome, null AS endereco, codigo, descricao, data, hora, local, valor, umt FROM infracao WHERE (placa=[@PLACA])
UNION
SELECT null AS placa, nome, endereco, null, null, null, null, null AS tipo, null, null FROM proprietario WHERE (placa=[@PLACA])
UNION
SELECT null, null, null, null, null, null, categoria AS tipo, categoria_valor, cor, ano FROM veiculos WHERE (placa=[@PLACA])
) a