sql - Ignore null values in select statement -
i'm trying retrieve list of components via computer_system, if computer system's graphics card set null (i.e. has onboard), row isn't returned select statement.
i've been trying use coalesce without results. i've tried , or in clause, returns computer system different kinds of graphic cards.
relevant code:
select computer_system.cs_id, computer_system.cs_name, motherboard.name, motherboard.price, cpu.name, cpu.price, gfx.name, gfx.price public.computer_case , public.computer_system, public.cpu, public.gfx, public.motherboard, public.ram computer_system.cs_ram = ram.ram_id , computer_system.cs_cpu = cpu.cpu_id , computer_system.cs_mb = motherboard.mb_id , computer_system.cs_case = computer_case.case_id , computer_system.cs_gfx = gfx.gfx_id; <-- ( or computer_system.cs_gfx null)
returns:
1;"computer1";"fractal design"; 721.00; "msi z87"; 982.00; "core i7 i7-4770k "; 2147.00; "crucial gamer"; 1253.00; "asus gtx780";3328.00
should use joins? there no easy way return requested row, if there's bloody null value. been struggling @ least 2 hours.
tables posted if needed.
edit: should return second row:
2;"computer2";"fractal design"; 721.00; "msi z87"; 982.00; "core i7 i7-4770k "; 2147.00; "crucial gamer"; 1253.00; "null/nothing";null/nothing
you want left outer join
.
first, clean code use ansi joins it's readable:
select computer_system.cs_id, computer_system.cs_name, motherboard.name, motherboard.price, cpu.name, cpu.price, gfx.name, gfx.price public.computer_system inner join public.computer_case on computer_system.cs_case = computer_case.case_id inner join public.cpu on computer_system.cs_cpu = cpu.cpu_id inner join public.gfx on computer_system.cs_gfx = gfx.gfx_id inner join public.motherboard on computer_system.cs_mb = motherboard.mb_id inner join public.ram on computer_system.cs_ram = ram.ram_id;
then change inner join
on public.gfx
left outer join
:
left outer join public.gfx on computer_system.cs_gfx = gfx.gfx_id
see postgresql tutorial - joins.
i recommend reading introductory tutorial sql - @ least postgresql tutorial, preferably more material well.
Comments
Post a Comment