在线等关于sql查询的问题~~~在线等~~~
第一张:table1
typeid name
1 张三
2 李四
第二章:table2
id typeid gameid value
1 1 1 test1
2 2 1 test2
3 -1 1 test3
问题是这样的,现在我要查table2的全部并且和table的typeid做关联,但是table2里的typeid中有一列的值是-1。
如果这样写:select * from table1 a,table2 b where b.gameid=1 and a.typeid=b.typeid
这样写就会把table2的一条数据丢掉了,怎样写才可以保证table2的数据全在,如果table2不符合条件的也不要把table2的-1那条数据丢掉???
在线等~~
作者: lht918 发布时间: 2011-06-15
select a.*,b.name from table2 a left join table1 b on a.typeid=b.typeid where b.gameid=1
作者: qianjin036a 发布时间: 2011-06-15
作者: qianjin036a 发布时间: 2011-06-15
select * from table2 b left join table1 a on a.typeid=b.typeid where b.gameid=1
作者: FlySQL 发布时间: 2011-06-15
作者: mad_13810376563 发布时间: 2011-06-15
作者: yibey 发布时间: 2011-06-15
select a.*,b.name from table2 a left join table1 b on a.typeid=b.typeid where b.gameid=1
作者: fredrickhu 发布时间: 2011-06-15
select a.*,b.name from table2 a left join table1 b on a.typeid=b.typeid
作者: chuanzhang5687 发布时间: 2011-06-15
create table table1 (typeid int,name char(20)) create table table2 (id int,typeid int,gameid int,value char(10)) insert into table1 select 1,'张三' union all select 2,'李四' insert into table2 select 1,1,1,'test1' union all select 2,2,1,'test2' union all select 3,-1,1,'test3' select * from table1 typeid name ----------- -------------------- 1 张三 2 李四 select * from table2 id typeid gameid value ----------- ----------- ----------- ---------- 1 1 1 test1 2 2 1 test2 3 -1 1 test3 select a.*,b.* from table2 a left join table1 b on a.typeid=b.typeid id typeid gameid value typeid name ----------- ----------- ----------- ---------- ----------- -------- 1 1 1 test1 1 张三 2 2 1 test2 2 李四 3 -1 1 test3 NULL NULL
作者: ap0405140 发布时间: 2011-06-15
作者: PPGGLL 发布时间: 2011-06-15
作者: aspwebchh 发布时间: 2011-06-15
作者: bmcl00 发布时间: 2011-06-15
SQL code
create table table1 (typeid int,name char(20))
create table table2 (id int,typeid int,gameid int,value char(10))
insert into table1
select 1,'张三' union all select 2,'李四'
insert into tab……
我现在是这样的:
SQL code
Set @Sql = 'SELECT * FROM table2 a , table1 b WHERE ('+CASE @uid WHEN '-1' THEN '1=1' ELSE '_a.uid = '+CAST(@uid AS VARCHAR(20)) END+') AND ('+CASE @typeid WHEN '-1' THEN '1=1' ELSE ' a.typeid = '+CAST(@typeid AS VARCHAR(20)) END+') AND a.typeid=b.typeid
我是拼的sql 多条件查询,就是说如果typeid为-1的话我就忽略不差,如果是别的话我就联查了,就是不知道默认怎么写,如果不传参的话按照这样的拼法貌似就出现了如题的问题。
求解~~
作者: lht918 发布时间: 2011-06-15
作者: lht918 发布时间: 2011-06-15
作者: leetiannet 发布时间: 2011-06-15
还有就是多个left join怎么使用?例如table2要left join两个表??
SQL code
select [字段列表] from [表1] left join [表2] on [表1].[字段]=[表2].[字段] left join [表3] on [表1].[字段]=[表3].[字段] ...
作者: ap0405140 发布时间: 2011-06-15
SQL code
declare @Sql varchar(max) Set @Sql='select a.*,b.* from table2 a left join table1 b on a.typeid=b.typeid' exec(@Sql)
作者: ap0405140 发布时间: 2011-06-15
作者: yanqinyanfang 发布时间: 2011-06-15