-
组合查询
多数sql查询只包含从一个或多个表中返回数据的单挑select语句,但是sql也允许执行多个查询,并将结果作为一个查询结果集返回。这种组合查询通常称为并或复合查询。 -
使用union
“检索所有河北省的商店”
select name from tb_store where province=‘河北省’
“检索所有江苏省的商店”
select name from tb_store where province=‘江苏省’
“检索所有河北省和江苏省的商店”使用组合查询方式
select name from tb_store where province=‘江苏省’
union
select name from tb_store where province=‘河北省’
这里使用组合查询方式或许复杂一些,但在某些情况下,组合查询更容易。
值得注意的是,使用union查询结果会自动去除重复的行。
使用union在最后一条select语句后使用order by子句。查询结果会按照使用的order by子句排序所有结果
SQL基础十三【组合查询】
最新推荐文章于 2023-07-09 15:53:42 发布