假设我有两张桌子;表A和表格如下所示:
IL_0000: ldarg.0
IL_0001: ldfld class [System.Windows.Forms]System.Windows.Forms.Button ClassLibrary1.Test::_btn
IL_0006: ldc.i4.1
IL_0007: callvirt instance void [System.Windows.Forms]System.Windows.Forms.Control::set_Enabled(bool)
IL_000c: ldarg.0
IL_000d: ldfld class [System.Windows.Forms]System.Windows.Forms.Button ClassLibrary1.Test::_btn
IL_0012: call valuetype [System.Drawing]System.Drawing.Color [System.Drawing]System.Drawing.Color::get_Red()
IL_0017: callvirt instance void [System.Windows.Forms]System.Windows.Forms.Control::set_ForeColor(valuetype [System.Drawing]System.Drawing.Color)
IL_001c: ldarg.0
IL_001d: ldfld class [System.Windows.Forms]System.Windows.Forms.Button ClassLibrary1.Test::_btn
IL_0022: ldarg.0
IL_0023: ldfld class [System.Windows.Forms]System.Windows.Forms.Button ClassLibrary1.Test::_btn
IL_0028: callvirt instance class [System.Drawing]System.Drawing.Font [System.Windows.Forms]System.Windows.Forms.Control::get_Font()
IL_002d: callvirt instance class [System.Drawing]System.Drawing.FontFamily [System.Drawing]System.Drawing.Font::get_FontFamily()
IL_0032: ldarg.0
IL_0033: ldfld class [System.Windows.Forms]System.Windows.Forms.Button ClassLibrary1.Test::_btn
IL_0038: callvirt instance class [System.Drawing]System.Drawing.Font [System.Windows.Forms]System.Windows.Forms.Control::get_Font()
IL_003d: callvirt instance float32 [System.Drawing]System.Drawing.Font::get_Size()
IL_0042: ldc.i4.1
IL_0043: ldc.i4.2
IL_0044: newobj instance void [System.Drawing]System.Drawing.Font::.ctor(class [System.Drawing]System.Drawing.FontFamily, float32, valuetype [System.Drawing]System.Drawing.FontStyle, valuetype [System.Drawing]System.Drawing.GraphicsUnit)
IL_0049: callvirt instance void [System.Windows.Forms]System.Windows.Forms.Control::set_Font(class [System.Drawing]System.Drawing.Font)
IL_004e: ret
如果我尝试使用完全连接加入它们,结果将取决于我在select语句中使用的表。例如,以下查询将生成以下结果
A
Color ID
Blue 1
Green 2
Red 3
B
Color ID
Blue 1
Brown 2
Red 3
如果我决定在select语句中使用B.color而不是A.color,我会得到以下结果:
select A.color, count(*)
from A
full join B on a.color = B.color
group by 1
order by 1
color count
Blue 1
Green 1
Red 1
1
如何让结果集包含所有颜色值。我知道我可以使用unionall完成,并且我可以在select语句中使用case语句,当另一个为null时使用一个case语句,但是还有另一种更简洁的方法来实现它吗?
谢谢, 本
答案 0 :(得分:2)
如果值存在于一个表而不是另一个表中,请使用coalesce
从另一个表中获取值。
select coalesce(A.color,B.color) as color, count(*)
from A
full join B on a.color = B.color
group by 1
order by 1