有表product(产品表)和imgPath(路径表)
product中字段 id,name.
imgPath中字段 id,foreignId(外键id为product中的id),imgPath
imgPath可以存储一个foreignid的多个图片.
比如数据如下
product
id name
1 特公鸡
2 细米
3 诺鸡鸭
imgPath
id foreignId imgPath
1 1 /images/001.jpg
2 1 /images/002.jpg
3 2 /images/003.jpg
现在取值的时候如何只取一张图片?
即:
1 特供机 /images/001.jpg
2 细米 /images/003.jpg
product中字段 id,name.
imgPath中字段 id,foreignId(外键id为product中的id),imgPath
imgPath可以存储一个foreignid的多个图片.
比如数据如下
product
id name
1 特公鸡
2 细米
3 诺鸡鸭
imgPath
id foreignId imgPath
1 1 /images/001.jpg
2 1 /images/002.jpg
3 2 /images/003.jpg
现在取值的时候如何只取一张图片?
即:
1 特供机 /images/001.jpg
2 细米 /images/003.jpg
解决方案 »
- 使用 Comet 时当处于长连接状态时,服务端有信息返回时不能输出给客户端
- 有那位高手用C#写过发短信软件,有原代码更好。如果有这个项目的给我一份谢谢。我的邮箱[email protected]
- 指定的参数已超出有效值的范围。参数名: index
- 用过nhibernate大佬们帮忙看一下,Unknown entity class什么错误啊~
- 关于treeview,怎么得到选中节点的nodedata值啊?
- 简单问题!在线等!
- 大家幫我看看這個SQL語句該怎么寫啊??
- 求对于单位员工的查询代码
- 请教高手:DataGrid超级链接传递参数的问题
- 如何在asp.net中打印datagrid中的数据?????
- csdn登录跳转
- mvc 往用户控件传值的问题
默认取foreignId 为1 的第一条数据 还是?
on i.foreignId=p.id and id in (select min(id) from imgPath group by foreignId)
name,
(select MIN(imgPath.imgPath) from imgPath where imgPath.foreignId=product.id)
from product
http://www.cnblogs.com/sanlang/archive/2009/03/24/1420360.html
用将图片分组 取第一个(反正就是想办法取图片的一个)
然后在join一下product(select *,row_number() over(partition by foreignId order by id asc) as Sequence from Img)
where Sequence=1要sql server 2005以前才可以~
select product.*,B.imgPath from product OUTER Apply (SELECT TOP 1 * FROM imgPath WHERE imgPath.foreignId=product.ID ORDER BY imgPath.ID) B
create table product(id int ,name varchar(10))
insert into product
select 1,'特公鸡' union
select 2,'细米' union
select 3,'诺鸡鸭' create table imgPath(id int ,foreignid int,imgpath varchar(50))
insert into imgPath
select 1,1, '/images/001.jpg'union
select 2,1,'/images/002.jpg'union
select 3,2, '/images/003.jpg'select a.id,a.name,b.foreignid,b.imgpath from product a left join imgPath b on
a.id=b.foreignid where b.id in(select min(id) from imgPath group by foreignId)--
id name foreignid imgpath
----------- ---------- ----------- --------------------------------------------------
1 特公鸡 1 /images/001.jpg
2 细米 2 /images/003.jpg
OUTER Apply 在access中应该没有 2005的
access 换成left outer应该可以
那就参考 9L的 OUTER Apply (找到匹配值则有值,没有找到匹配值则以NULL表示)
on a.id=b.foreignid and b.id in(select min(id) from imgPath group by foreignId)
b.id in(select min(id) from imgPath group by foreignId)此條件放WHERE后面肯定沒有了
select foreignId from ( select idd=row_number()over(partition by foreignId order by getdate()),* from imgPath)t where idd=1 order by orderNo desc
)
name,
(select MIN(imgPath.imgPath) from imgPath where imgPath.foreignId=product.id)
from product