我正在做作业,目前我有3张桌子。
[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])
;
结果是混乱的,它必须返回单列但使用三列,如您在此处看到的:https://i.imgur.com/F1xp8x9.png
我该如何解决?你能帮助我吗?我正在使用MS Access 2013
谢谢。
答案 0 :(得分:0)
使用子查询和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