用于确定表中是否存在的SQL查询 [英] SQL query for determining presence in table
本文介绍了用于确定表中是否存在的SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个学生ID和我的数据库中的多个表为学生的信息,给予任何id,它只存在一个表。每个表都有属性学生id。
I have a student id and multiple tables in my database all for info of students such that given any id, it is present only in one table. Each table has attribute Student id.
给定学生id,如何使用SQL查询确定表有学生的信息与该学生id?
Given a student id, how to use SQL query to determine table has the info of student with that student id?
推荐答案
IF EXISTS(SELECT TOP 1 1 FROM table WHERE studentID = @input)
BEGIN
-- do stuff
END
b
$ b
or slightly less fancy
IF EXISTS(SELECT TOP 1 studentID FROM table WHERE studentID = @input)
BEGIN
-- do stuff
END
您还可以创建查询视图/查询。像这样:
you could also make a lookup view / query. something like this:
SELECT sudentID, 'Table1' as Location
FROM 'Table1'
UNION ALL
SELECT sudentID, 'Table2' as Location
FROM 'Table2'
UNION ALL
SELECT sudentID, 'Table3' as Location
FROM 'Table3'
UNION ALL
SELECT sudentID, 'Table4' as Location
FROM 'Table4'
然后查询此视图的位置。如果您需要加入
Then query this view for location. This can be useful if you need to joins
这篇关于用于确定表中是否存在的SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文