Sql 中的关联与合并查询
Sql查询中经常用到多表关联查询或多表合并查询,在本文家介绍关联查询中常用到的几个Sql语句。多表主键关联查询与数据合并查询。
INNER JOIN 多表关联查询
当多表存在关联主键,同时进行查询时,通过INNER JOIN来完成查询,表与表之间必须存在关联依赖关系。一般出现层级数据表分类,例如城市或省份与子级县市分类关联查询。下面采用“WPF TreeView动态指定选取节点“文中的数据源做示例。这里数据库中三个表CityData、CityItemData、CityChildItem分别为城市的层级关系,为CityData为主城市,CityItemData为主城市的分区,CityChildItem为分区的地点。下图可以看到红色和绿色两组关联主键。
Sql关联查询,可以看到三个数据表的关联结果查询。
SELECT CityItemData.*, CityData.*, CityChildItem.* FROM CityItemData INNER JOIN CityData ON CityItemData.CityId = CityData.CityId INNER JOIN CityChildItem ON CityItemData.CityItemId = CityChildItem.CityItemId
查询结果
在INNER JOIN后面加Where条件来筛选查询数据,如果需要筛选某个小条件的查询直接加上WHERE条件即可。在主城市CityData中只有输入了两个城市,一个是"北京/Beijing"对应的CityId为1,一个是“上海/Shanghai”对应的CityId为2。下面就通过筛选城市Id来完成查询。加入“WHERE (CityItemData.CityId = 1) ”、WHERE (CityItemData.CityId = 1) AND (CityItemData.CityItemId = 2)、WHERE (CityItemData.CityId = 2)可看到查看数据筛选结果。
/*筛选城市Id为 1 的 WHERE (CityItemData.CityId = 1)*/ SELECT CityItemData.CityItemId, CityItemData.CityItemName, CityItemData.CityId, CityData.CityId AS Expr1, CityData.CityName, CityChildItem.CityChildItemId, CityChildItem.CityChildItemName, CityChildItem.CityItemId AS Expr2 FROM CityItemData INNER JOIN CityData ON CityItemData.CityId = CityData.CityId INNER JOIN CityChildItem ON CityItemData.CityItemId = CityChildItem.CityItemId WHERE (CityItemData.CityId = 1) /*筛选城市Id为 1并且CityItemId为2的 WHERE (CityItemData.CityId = 1) AND (CityItemData.CityItemId = 2)*/ SELECT CityItemData.CityItemId, CityItemData.CityItemName, CityItemData.CityId, CityData.CityId AS Expr1, CityData.CityName, CityChildItem.CityChildItemId, CityChildItem.CityChildItemName, CityChildItem.CityItemId AS Expr2 FROM CityItemData INNER JOIN CityData ON CityItemData.CityId = CityData.CityId INNER JOIN CityChildItem ON CityItemData.CityItemId = CityChildItem.CityItemId WHERE (CityItemData.CityId = 1) AND (CityItemData.CityItemId = 2) /*筛选城市Id为 1 的 WHERE (CityItemData.CityId = 2)*/ SELECT CityItemData.CityItemId, CityItemData.CityItemName, CityItemData.CityId, CityData.CityId AS Expr1, CityData.CityName, CityChildItem.CityChildItemId, CityChildItem.CityChildItemName, CityChildItem.CityItemId AS Expr2 FROM CityItemData INNER JOIN CityData ON CityItemData.CityId = CityData.CityId INNER JOIN CityChildItem ON CityItemData.CityItemId = CityChildItem.CityItemId WHERE (CityItemData.CityId = 2)
加入条件分别执行效果显示
UNION多表合并查询
UNION将多个表数据很并到一个垂直表中显示,不需要存在主键关联,要求查询标的列数属性相同,同时也支持WHERE条件查询下面合并CityData、CityItemData、CityChildItem三个表。
SELECT CityId, CityName FROM CityData UNION SELECT CityChildItemId, CityChildItemName FROM CityChildItem UNION SELECT CityItemId, CityItemName FROM CityItemData
查询显示结果
进一步深入,将News_Info和Blogs_Info合并查询,News_Info表由NewsId, NewsCapiton, NewsContent, NewsTime组成,Blogs_Info表由 BlogsId, BlogsName, BlogsContent, BlogsTime组成。下面将这两个表合并查询并且加入模糊查询条件,将包含“平板电脑”的关键字查询出来。
SELECT NewsId, NewsCapiton, NewsContent, NewsTime FROM News_Info WHERE (NewsContent LIKE '%平板电脑%') UNION SELECT BlogsId, BlogsName, BlogsContent, BlogsTime FROM Blogs_Info WHERE (BlogsContent LIKE '%平板电脑%')
下面是查询结果显示