二表结构如下
product
id defaultImage
1 图片a productsku
id Color image1 image2 image3 image4 (很多) productid
1 red 图片a 图片b 图片c 图片d 图片..
根据defaultimage查出 另一张表的跟他对应的字段 怎么查我要得到 image1 或者 image2 或者 image3 或者 image4 或者其它
因为是动态可以设置的 如果image2记录跟他相等,我就要image2,如果image1记录跟他相等,我就要image1,thank you!
product
id defaultImage
1 图片a productsku
id Color image1 image2 image3 image4 (很多) productid
1 red 图片a 图片b 图片c 图片d 图片..
根据defaultimage查出 另一张表的跟他对应的字段 怎么查我要得到 image1 或者 image2 或者 image3 或者 image4 或者其它
因为是动态可以设置的 如果image2记录跟他相等,我就要image2,如果image1记录跟他相等,我就要image1,thank you!
from product m, productsku n
where m.id = n.id and (m.defaultImage = n.image1 or m.defaultImage = n.image2 or m.defaultImage = n.image3 or m.defaultImage = n.image4 ... )
这样查也只是得到一条数据,并不能详细的知道到底是哪个字段吧.
和我这样其实一样吧 (取出的defaultimage值 )
select *from Productsku
where image1='images/Business/p1.gif'
or image2='images/Business/p1.gif'
or image3='images/Business/p1.gif'
or image4='images/Business/p1.gif'
union all
select m.* , 'image2' image from product m, productsku n where m.id = n.id and m.defaultImage = n.image2
...
select m.* , 'imagen' image from product m, productsku n where m.id = n.id and m.defaultImage = n.imagen
列如下:(id,color,imagetotal)
imagetotal:是所有的image的内容想加,最好每个字段后用一个特殊符号分割开2 从临时表中,通过 imagetotal字段找到包含指定id的product 表中对应行的defaultImage 的那一行,然后返回该行对应的id信息
select * from tempTable where imagetotal like (select '%'+defaultImage +'%' from product where id=x)3 通过第2步找到的id,然后到productsku找对应的信息
union all
select m.* , 'image2' image from product m, productsku n where m.id = n.id and m.defaultImage = n.image2
...
select m.* , 'imagen' image from product m, productsku n where m.id = n.id and m.defaultImage = n.imagen
--这里假设 为图片A
declare @sql varchar(8000)
select @sql = isnull(@sql + ' union all ' , '' ) + ' select id , [images] = ' + quotename(Name , '''') + ' from tb where '+quotename(name)+'=''图片a'''
from syscolumns
where name like N'%image%' and ID = object_id('tb') --表名tb,不包含列名为姓名的其它列
order by colid asc
print @sql
exec(@sql + ' order by id ')
这个确实能查出来. 不过我在请教一下.
这查出来的结果集是这样的
defaultImage 无列名
图片a image1
图片b image2
是这样相匹对,恕我笨我这样写 查出了结果,但感觉绕了个天大的弯路查出结果在( select top 1 参数 from productsku )
而且无列名的数据我也不知道怎么取,加列名不知道从何加
如果有多表,表之间如何关联?
发帖注意事项
http://topic.csdn.net/u/20091130/21/fb718680-98ff-4afb-98d8-cff2f8293ed5.html?24281