问题一 :表一: table1
表一字段: pid pfilename pvsum表二: table2
表二字段: tid pid tvendors
pid 为 表一 的主键 表二的外键现在我想查询出来的结果 pid pfilename pvsum tishavetishave 为自定义字段,如果表一中的主键在表二中存在记录 那么这个字段显示为 :已存在
如果表一中的主键在表二中没有记录 那么这个字段显示为 :未存在
例如:
table1中记录 pid pfilename pvsum
1 123.pdf 5
2 234.doc 7table2 中记录 tid pid tvendors
1 1 ZTE查询出结果为:
pid pfilename pvsum tishave
1 123.pdf 5 已存在
2 234.doc 7 未存在
问题二:
如果表一种字段 pfilename 结果为NULL 在查询时我想让为NULL的显示为 空 该怎么做呵呵,小弟菜鸟...突然想到这两个问题,求解。
表一字段: pid pfilename pvsum表二: table2
表二字段: tid pid tvendors
pid 为 表一 的主键 表二的外键现在我想查询出来的结果 pid pfilename pvsum tishavetishave 为自定义字段,如果表一中的主键在表二中存在记录 那么这个字段显示为 :已存在
如果表一中的主键在表二中没有记录 那么这个字段显示为 :未存在
例如:
table1中记录 pid pfilename pvsum
1 123.pdf 5
2 234.doc 7table2 中记录 tid pid tvendors
1 1 ZTE查询出结果为:
pid pfilename pvsum tishave
1 123.pdf 5 已存在
2 234.doc 7 未存在
问题二:
如果表一种字段 pfilename 结果为NULL 在查询时我想让为NULL的显示为 空 该怎么做呵呵,小弟菜鸟...突然想到这两个问题,求解。
ELSE field3 END ) as State
FROM tablename
select pid ,pfilename ,pvsum,
case when exists(select 1 from table2 where a.pid=pid) then 'exist' else 'not exist' end as tishave
from table1 a
select a.pid,isnull(a.pfilename,'空')pfilename,a.pvsum,
case when b.num=0 then '未存在' else '已存在' end as tishave
from table1 a
left join
(
select pid,count(1)num from table2
group by pid
)b on a.pid=b.pid
,(case when (t2.pid IS null) then '未存在' else '已存在'end) tishave
from table1 t1
left join table2 t2 on t1.pid=t2.pid