我在谷歌上找不到答案,因为我不知道关键字...这就是我的问题
SELECT col1
from table1
WHERE col2 in (select col2 from table2 where user_id=@ID)我想知道是否可以把if条件语句..。如果table2为null,那么我将取而代之
select col1 from table1 <no condition>就像如果是select col2 from table2 where user_id=@id is null,它就会取出所有的数据
有人能帮我吗?
发布于 2013-01-18 16:44:16
SELECT table1.col1
FROM table1 LEFT OUTER JOIN table2 ON table1.col2 = table1.col2 AND table2.user_id = @ID发布于 2013-01-18 16:55:11
DECLARE @sql VARCHAR(500)
SET @sql = 'SELECT col1 from table1'
IF((select col2 from table2 where user_id=@ID )IS NOT NULL)
BEGIN
SET @sql = @sql + ' WHERE col2 in (select col2 from table2 where user_id= ' + CAST(@ID AS VARCHAR) + ' )'
END
EXEC (@sql)对于这种情况,您可以根据需要使用动态查询
https://stackoverflow.com/questions/14395127
复制相似问题