SQL学习之组合查询(UNION)

时间:2022-12-03 06:37:53

1、大多数的SQL查询只包含从一个或多个表中返回数据的单条SELECT语句,但是,SQL也允许执行多个查询(多条SELECT语句),并将结果作为一个查询结果集返回。这些组合查询通常称为并或复合查询。

主要有两种情况需要使用组合查询:

(1)在一个查询中从不同的表返回结构数据

(2)对一个执行多个查询,按一个查询返回数据

2、使用UNION

使用UNION很简单,所要做的只是给出每条SELECT语句,然后再每条SELECT语句之间加上UNION关键字,这样所给出的SELECT结果集就能组合成一个结果集并返回。代码如下:

select * from dbo.Customers_1

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAARkAAACMCAIAAADHrkoEAAASo0lEQVR4nO1dS2/jthY+f6Rof4FXQaDmH3TRRe1tvbhykG0XxnTVTbMbxwMkQO5f8ObiTmxfYH5DFgaC5rUZ4PY1bXNX1UwbOw8HcxeSKIpviaQsMiQOAoWmxHN4zmc+zE+Ej46kl+N/BgnSZoFNYySkkDxJ8K9//ydIkCD6Yrdf+vDhg9XnG0/OKfzRTZ2rJidsDFgqJecU/uimzlWTEzbaxdL79++tPt94ck7hj27qXDU5YWPAUik5p/BHN3WumpywsXEsvT3c2Tl8K87ZXGL67O3hDuRp703zSkkSu5EBa9M2tXC9xLERWuUXu1hKkoTMajeWaIXLYflmrx1uwxO7kQEKtdvUwvWSJJBqGGihTexi6c8//ySz2o0lSuE2KcdJvEY+3Mth74ARkiQLpDd7LfiKg8fH9eruwYas109/3PyPfP7Vqyh6dZleoB46zdm0MBRG2hJSKL87zYrtDiIAgGg8P8gurvH8rOTdw3RQjEwGs6yKg0GRMx0ADOarvHA0vq7ZyHhTR68u2VWz1C4Z+PnBVfv8QrimsDc1Z3dKOwgZkl7PiibCG39197C6mw+gnKPWGvDwuF6u7m3Iev302+9/kM+/HEfR+GJ1PYognt4vV/fL6S5E4ws7OugqnGlLlCyUvziIYDBbXo4jiEaX96WLaHyB/kUl6aa4HEeQN0WRsztd3S9Xszi7qNfI99MBxFOWFYR6hNq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98nNaRvgkEe7tYGcyn+btUrc+XsCXN54cl4CzimR/jBY56JZ+1TVzU2YaNPFRIMWZKAke9ss/aJi7qbMpGwYqCuM8x2yNlz0wCR13BZ20WF3X20sbAUXfPZx7o7KWNgaPuns880NlLGwNHPaSQzCS7O2eDBHk+ErAUJIgZCVgKEsSMBCwFCWJG/g9fO6c8cPPaEQAAAABJRU5ErkJggg==" alt="" />

现在有个需求,需要检索出位于浙江和上海,或者顾客名称为Fun4All的所有顾客,下面是通过WhERE子句完成的解决代码:

select *
from dbo.Customers_1
where Province in('浙江','上海') or Company='Fun4All'

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAP8AAABiCAIAAAAQkpIAAAANBElEQVR4nO2dzU4jxxbHz4tEyRN4hVCFN8gii+BtvLhtxDYLNFllk9lN2yOB5GdgMxpoeySegQUSisFskG4yyWTCXWHI4C+MuIv+qq6P7uqP6qpqqnSEmqLc9fc5vy6Xu+tQ8GyLLS+1wLv3H6xZe5kG795/kHp53d/fSz1/5cU4wc+qNRvksfv7+7Ob88gs/WQxTvCzas0Geaxu+u/u7qSev/JinOBn1ZoN8tjd3Z2lP60YJ/hZtWaDPKYB/df7W1v71+k16gozltf7WxCW3ZP6RWUUtpMB86lMD3N6Bw09Vjf90+mUrNKbflpwEqSTXZ3C6Re2kwFi2TI9nBHiAl1LUzudTmul//b2lqzSm35KsE7iOIXn5P3d8EKV+SayQnyyq81wcXt7S9L/+LieL1YybL1++ufmf+T5J28RenvpH0Sfjn6NamMIjtQSFovf8YJmO10EAID6o15wcIXXBy0XK68bzwq6w6CLXjeu8boA3dE8bIz6VwWdjLsavb1kd82SnXiD3/YmRUOcUOJ3tOPRrou68I+HsXjcLfPFar4YdSFZI6YzkkrSv3pcz+ZLGbZeP/39+R/y/Jd9hPoX8ysXgeMtZ/PlzNsB1L+Qo6Gs4EAt0TIWf9FD0B3OLvsIkHu5TByg/kX0a9SSdsVlH0Hoirhmx5svZ/OhExwUc/LS64Ljsd4FIY+Q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n7L/e39ra3S2X16hJZiPW40Gnqt0rxE3pno3a01/PwM/Uyc9sHO5Ad3TZ/xb1rwqnNWqS2Zgr0qIkcRboa7dnIzdzhb+In25TrHcxA86qz/rop3K7hg7sePPlRQ+h3pXpmY1Rd9FmiQVJwr85MOnXcM9GRppl8k8ibQr1LjinB+HlnyVNlP4I+mbRL7Rje8mxn1iyr37PRkJzAj/iaxK/TbHeqYshpZ6+MGqkP5HbNXFRQorxmY039JVQD/0a7NlIkk1d/yJtivWOcyaWysg7rsQE6a8sqVebzMZz9v2fMiSZsmcjOavB/3tK/L9VMtoU6z2CLOtfGqZPjaoyLv10ZqNvYxc1J7NRLFtK/BukcXs2QmeA5dBgk/vMNiXoZ+LO+1XN2G8zGwVjqadp+6xXZFzXgH6b2SgQS21NW/p5JjgXkq3TZjZWEEvlZhz9mui0mY2mxlIfzaZ4LOVZr81sNCyW+mg2xWNc+m2xpfGFvh7g3fsPyi9Ka9aU2P8BfJL8XG2E388AAAAASUVORK5CYII=" alt="" />

ok,通过Where子句的OR操作符完成需求!

下面通过组合查询UNION操作符来完成需求,代码如下:

select *
from dbo.Customers_1
where Province in('浙江','上海')
select *
from dbo.Customers_1
where Company='Fun4All'

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPkAAAC4CAIAAACuKd0OAAAVeUlEQVR4nO2dvW7jRtfHz40EyRWoMoR5fAcpUsRqo+KhDLcpDKdKk+2W0gI2oGtw8+K1Kb2Ar8GFgEVky80CT7LJZuOnWq6zFvVBwW/Br+F88HvIGXEGBwI9GnH+PPPj0YicY8KLyPL09CR0/5UX5QS/NK1ZIY+B0L0r5AivKCf4pWnNCnlMLOufP38Wuv/Ki3KCX5rWrJDHNOuxopzgl6Y1K+Sx2ll/d354eP4uuaa5why5d+eHEJSTm/pFpRS2kwHzqUgPc3oHCT0mlnXbtskquVmnBcexuTmRafC8wnYyQCRbpIdThrhA18LUimX906dPZJXcrFOCZRLHKTwnn58Ep6XIg0gb4psTaYIDbLe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/Cys15HrkaGcc3KjSo5eMQ+o5lYfAaV3KZY79kMOE8fEMU6Ny8JwLD2Jy8pTH4ryA0+41clBy+2T2xSF76VpU2h3jPOxUGGHLyls146DyYqhbtMrGf6zxkl47p0OXiE5hhsrCc8MtsU651CP6GePg2Esc5+NpifjIfMxR7k4KVm3+XmRokcPJJj6mzP0qZY7xTQqW/xtisxNutzE4ExeV6unpcLE4FhqZ2DNyO3q+BGlRw8cn6CzbKi51antSnWe2DJBKdOcipmnX4OXpiGVyYBT54cPDzYV8SNejl40B9j/+4Pm5SntinBOhNu3p91xHWdg5dx5OQ0ae+bZonZtbOuc/AyjJy0Ji3rPEtmWuwcRufglRm5xk051pvSqXPwlBw5eTSr4jFb5+ApOnLyaFbFY7bOwdOlJWX2+Fa9x3lq01bM/h/nUWeXoRxWTgAAAABJRU5ErkJggg==" alt="" />

这是没有使用UNION的单独查询,一共有6条记录,如果是普通的结果集组合的话会出现6条记录,我们发现其中有两条记录是重复的aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAR0AAAA0CAIAAAA43HtmAAAIf0lEQVR4nO1dvW7bSBCeFwmSJ1AlCHt+gxQpIrVRcaThNoXgVGniLpQC2ICeQaVNqfAzuDBgRJbdGLj8XuKrYieRqB8KuYLkckkuRVLk/tEcLIT1aLXzcTifZknuWPBHhNzf3wuxu7UoB/iPaMwqeqxAASFWKU6/OdzZObzZrBEn1Ci5OdwBT/ZO+YNKELqTgfApSw/HWAeZPVagiOHV3d1dWCU3r6KAgyF6uidfoNCdDODDZunhhFO8hWmZ4iFRxPDqx48fYZXcvIoAlglcjMQ5+XDP+wpgeRBJp/h0T7ovoiIlllerlW3Nlyyaba+/3/4Xnv/qHULvJk4HrxUcjehGAYzRhpoPftd0h+3qCAAA9UZdt3NN6t2R86Wp+2skfeia6Oq+xtQB9JHlDUa96y2dTLoavZvQTdNgBw7wr+7Vtqc4gMQxtGtGXYdNOP2hD550izVfWvORDkFNOpwOVK68Wq7smbVg0Wx7/e+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R1QinwV4pWpQ+4K/My8YnY/0C04d83luM+uXB1+6PpK1nx1Tlv+lYFXoTr8Yy3wLZvmV9k31OFvPFo+z698TWH3LZSoww8+c/NvjcrEK4i5GViGdSDTOvwCXcOhKQdYOOai9lukf2trnNx5xbIOX+ApZxQlsjU5eRWXghJzUeGZCuPkzSumdfjCw66QKJG5yckr2ZoAXjGtwxfu0FJGiTyYVfEYI179DzxcGd3jwj9qAAAAAElFTkSuQmCC" alt="" />

在看使用了UNION组合查询关键字的查询解决代码:

select *
from dbo.Customers_1
where Province in('浙江','上海')
UNION
select *
from dbo.Customers_1
where Company='Fun4All'

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPoAAABiCAIAAAD2u1lEAAAM0klEQVR4nO2dzW7jthbHz4sU7RN4FRhs3qCLLhpv68WVA2+7CNJVN53dyB4gAfwM2QRJZAfIM2RhIKgTZxPgttNOp7mr8aQTf0VB7kJfFD8k6oMS6ZAgAoWmxb/P+emElnhCOD45N9XUV1Lh+OT8RWZ5eHiQev7Si3aCX+rWrJHFDO5k0U7wS92aNbKYdNw/f/4s9fylF+0Ev9StWSOLGdzJop3gl7o1a2SxOnC/O9jePrhLbqmvMJ13d7ANQeleVC8qpbCNDJhNZVqYMzooaDHpuM9mM7JJbdxpwXFyLroq+c8rbCMDRLJlWjjFxTmGlqZWOu6fPn0im9TGnRKskjhO4Rn5oBtcmTI/RJqLL7rKxAc4Pjl/enIXy7WM6rrP/9z/jzz/9B1C7268g/APntdSd2UIDtUSNRK/6/jddjsIAAD1Rz3/4BZv93su104n+kPfGfpD9DpRi9MB6IwWQWfUv81pZNzU6N0Ne2iW7NgH/LY3zevimBJvoF2HNl04hHc8jMTjZlks14vlqAPxFjGdnlQ4PjlfP7nzxUpGdd3nvz/+Q57/po9Q/3pxayOwnNV8sZo7u4D613I0FBXsqyV6RuKvewg6w/lNHwGyb1axA9S/Dn8Ne9KmuOkjCEwRtew6i9V8MbT8g3xGXjkdsBzWpyDkEbIXtzb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3hLnRGN/1vUf82d+aeIsl7mVwrig5nwbpyO+9xUzf4i9rpPvlGF6vAWSMpEXcqm2lowa6zWF33EOrd6p68Fw4XbnmXEx189s/EXcGd9xiZhPGXRPrkGl1wXg7CiyVl4R5Svlm4C+2dXTC6E0vY6995j9Ac4434qsPvk290iv6EdvpKkIl7LJtpaqPY2Non7zHQrwZ3BXbeI1GmLniRPvlGp5hOfYl3XErl4V5aoqoyyXtX7Ls0RdDRZec9cqKC/5OP6F+ApPTJN3pQkyFOne2UjzudvOfViY02J3lPLD9I/GufdjvvQXuAJZFgE/TUPgVwZ/LN+7Wi6G6S9wSdp2ZV9qmqSOSuA3eTvCfgPGWrsrjzquD0pnSdJnmvBOfVXrXDvS6dJnlPV+epo1kXi81M8p6+zlNHsy4Wm5nkPVNeTxnfX8HxyXntl52pplZTDe6mvqL6f4aFsbc2QvDUAAAAAElFTkSuQmCC" alt="" />

ok,完成需求,通过和上面没有使用UNION关键的分开查询相比,我们发现UNION(组合查询)从结果集中去除了重复的行。

这里我们可以使用UNION ALL,告诉DBMS不取消重复的行。那么效果就和上面没加UNION的一样

3、总结

分析上面完成需求的连个例子,我们发现对于上面的简单的例子使用UNION似乎比WHERE子句更加的复杂。但是这里你需要知道,如果对于较复杂的过滤条件,或者从多个表(而不是一个表)中检索数据的情形,使用UNION可能会使处理更加的简单。

4、使用UNION必须知道的规则(使用注意事项)

(1)使用UNION必须有两条或者两条以上的SELECT语句组成,语句之间用UNION关键字分割

(2)使用UNION关联的每个子查询必须包含相同的检索列、表达式或这聚集函数(次序可以不一样)

(3)列数据类型必须兼容;类型不必完全相同,但必须是DBMS可以隐含转换的类型(不同的数值类型或者不同的日期类型)

(4)UNION几乎总是完成与多个WHERE条件相同的工作,UNION ALL是UNION的一种形式,它完成WHERE子句完成不了的工作,因为他将返回每个条件的匹配行(包括重复行)

(5)使用组合查询,当需要对结果进行排序是,只能指定一条Order By语句,这条语句只能放在最后一天SELECT语句的后面.