二表结构如下
 
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!

解决方案 »

  1.   

    select m.* , n.* 
    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 ...  )
      

  2.   


    这样查也只是得到一条数据,并不能详细的知道到底是哪个字段吧.
    和我这样其实一样吧  (取出的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'
      

  3.   

    如果想详细知道是哪个字段,需要使用union allselect m.* , 'image1' image from product m, productsku n where m.id = n.id and m.defaultImage = n.image1
    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
      

  4.   

    不知道楼住是否是这个意思:到productsku表中找到image1~n中,如果imagex的值和product表中的defaultImage值相同,则返回productsku表中对应的该记录给楼住个思路,手头上没有数据库测试环境1 首先构造一个结果集(用cte或者临时表都可以),假设该结果集为tempTable
    列如下:(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找对应的信息
      

  5.   

    select m.* , 'image1' image from product m, productsku n where m.id = n.id and m.defaultImage = n.image1
    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
      

  6.   


    --这里假设 为图片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 ')
      

  7.   


    这个确实能查出来.  不过我在请教一下.
    这查出来的结果集是这样的
     defaultImage  无列名
         图片a      image1
         图片b      image2
    是这样相匹对,恕我笨我这样写  查出了结果,但感觉绕了个天大的弯路查出结果在( select top 1 参数 from productsku )
    而且无列名的数据我也不知道怎么取,加列名不知道从何加
      

  8.   

    你最好能提供详细的数据和结果,参考如下的发帖方式,以免耽搁你的时间.最好给出完整的表结构,测试数据,计算方法和正确结果.否则耽搁的是你宝贵的时间。
    如果有多表,表之间如何关联?
    发帖注意事项
    http://topic.csdn.net/u/20091130/21/fb718680-98ff-4afb-98d8-cff2f8293ed5.html?24281