sql-server查询所需的帮助 [英] Help Needed for a sql-server query

查看:97
本文介绍了sql-server查询所需的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我有一张桌子叫产品.它具有PID,PName,PPrice,PDescription,PCategoryID以及更多列.

现在,PCategoryID是外键.它是类别"表中的主键.类别将由Web应用程序的管理员创建.我想用每个类别的1个产品填充应用程序索引"页面上的数据列表.

例如说我们有C1,C2,C3,C4,C5之类的类别.我想用我的产品表中的每个类别中的仅一项填充数据列表.每个类别可以具有管理员输入的尽可能多的产品,并且可以具有管理员想要的多个类别.

自从早上以来,我对此查询感到困惑.我们将不胜感激.

Hi all!!

i have a table called products. it has PID ,PName , PPrice , PDescription , PCategoryID and many more columns.

Now the PCategoryID is the foreign key. It is primary key in Category tables. Category will be created by the admin of the webapplication. I want to Populate my datalist which is on the Index page of the application with 1 product of each category.

For instance say we have categories like C1, C2, C3, C4,C5. I want to populate datalist with only 1 item of each category from my products table. Each category can have as many products as admin enters and there can be as many category as admin wants.

I am puzzling over this query since morning. A little help will be much appreciated.

推荐答案

可以使用不同的方法..

一种选择是获取每个类别的最低产品ID..

there are different ways of doing this..

one option is to get the min product id for each category..

SELECT PID ,PName , PPrice , PDescription , PCategoryID  FROM products
where PID in (select min(PID) as ppid from products group by PCategoryID))



请检查以下链接以了解其他选项:

http://www .xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/ [



pl check the following link for different options:

http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/[^]


这篇关于sql-server查询所需的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆