SQL多条件查询Sql语句

DECLARE @startIndex INT,  --用来判断的变量
@ordertype NVARCHAR(500), --条件语句
@SqlBase  NVARCHAR(500)   --最终的sql语句
SELECT @startIndex=3
SELECT @ordertype=CASE --根据条件组合sql语句
 WHEN @startIndex=1 THEN
    'ORDER BY  CM.GeneralID DESC '
 WHEN @startIndex=2 THEN
    'ORDER BY  CM.GeneralID ASC '
 WHEN @startIndex=3 THEN
    'ORDER BY  CM.DefaultPicUrl DESC '
 END 

--拼凑最终的sql语句
SELECT @SqlBase='SELECT TOP 1 GeneralID  FROM PE_CommonModel CM WHERE ISNULL(CM.DefaultPicUrl , '''')!='''' '+'and CM.NodeID IN('''+CAST(34 AS NVARCHAR(10))+''')'+@ordertype
SELECT @SqlBase --查看sql语句
EXEC (@SqlBase) --执行sql语句
时间: 2024-09-10 19:07:31

SQL多条件查询Sql语句的相关文章

sql多条件查询,如何高效组合多个条件

问题描述 sql多条件查询,如何高效组合多个条件 我想查询数据库,是条件查询,但是这个条件有可能比较多,比如有几百个条件,如何写一条sql语句能高效查询数据库中满足这些条件的数据呢? 例如,我想查询userid字段为a,b,c,d--等人的数据,这样写sql感觉效率很低 select * from TABLE where USERID=a or USERID= b or USERID=c-- 有什么方法能比较好的满足查询要求呢? 解决方案 用in不能实现吗?比如 select * from TA

SQL 多条件查询几种实现方法详细介绍

SQL 多条件查询 以后我们做多条件查询,一种是排列结合,另一种是动态拼接SQL 如:我们要有两个条件,一个日期@addDate,一个是@name 第一种写法是 if (@addDate is not null) and (@name <> '') select * from table where addDate = @addDate and name = @name else if (@addDate is not null) and (@name ='') select * from t

SQL 多条件查询几种实现方法详细介绍_MsSql

SQL 多条件查询 以后我们做多条件查询,一种是排列结合,另一种是动态拼接SQL 如:我们要有两个条件,一个日期@addDate,一个是@name 第一种写法是 if (@addDate is not null) and (@name <> '') select * from table where addDate = @addDate and name = @name else if (@addDate is not null) and (@name ='') select * from t

sql 多条件查询的一种简单的方法

sql 多条件查询的一种简单的方法以后我们做多条件查询,一种是排列结合,另一种是动态拼接SQL如:我们要有两个条件,一个日期@addDate,一个是@name第一种写法是if (@addDate is not null) and (@name <> '')select * from table where addDate = @addDate and name = @nameelse if (@addDate is not null) and (@name ='')select * from 

[技术点]SQL 多条件查询

网上有不少人提出过类似的问题:"看到有人写了WHERE 1=1这样的SQL,到底是什么意思?".其实使用这种用法的开发人员一般都是在使用动态组装的SQL.让我们想像如下的场景:用户要求提供一个灵活的查询界面来根据各种复杂的条件来查询员工信息,界面如下图: 界面中列出了四个查询条件,包括按工号查询.按姓名查询.按年龄查询以及按工资查询,每个查询条件前都有一个复选框,如果复选框被选中,则表示将其做为一个过滤条件.比如上图就表示"检索工号介于DEV001和DEV008之间.姓名中含

SQL多条件查询,模糊查询,模糊多条件查询

临近毕业答辩,最近老有同学问多条件查询,模糊查询其实没那么复杂.别想的复杂了. 在企业应用程序开发中经常遇到,查询数据库的时候,查询的where条件可能不止一个,可能没有条件,也有可能至少一个或者多个条件,遇到这种情况,今天看到论坛上有人用C#的 if 语句拼接,这样解决可以是可以,不过显得太过累赘也没有必要,其实,一句固定的 sql 语句句型即可解决:  关于多条件查询 select * from t where (a = @a or @a is null) and (b = @b or @b

sql多条件查询语句

如上图:三个文本可选项,那sql语句怎么写呢? 1.首先获取三个文本的值分别为Name,Age,Sex. 2.string sql="select * from 表 where 1=1"; 3.if(Name!="") { sql=sql+"and userName like '%" + Name + "%'"; } if(Age!="") { sql=sql+"and Age like '%&q

C# SQL多条件查询拼接技巧

本文转载:http://blog.csdn.net/limlimlim/article/details/8638080 #region 多条件搜索时,使用List集合来拼接条件(拼接Sql) StringBuilder sql = new StringBuilder("select * from PhoneNum"); List<string> wheres = new List<string>(); if (cboGroup.SelectedIndex !=

sql 多表查询的语句集合

今天我们来讲一下关于如何多表查询的sql语句,先是最常见也是最突然的效绿也不怎么样的多表查询方法一 select * from A join B ON A.uid=B.uid join C ON C.uid=A.uid where uid=1 二.四个表查询 select * form A,B,C,D where A.uid=B.uid and B.uid=C.uid and C.uid=D.uid where uid=1 三.用别名查询 select * from table1 as a,ta