0 0

求教一条查询语句5

RT 假设有这么一张表 字段如下

t1  t2 t3

01 a1 1

01 a2 2

02 b1 1

02 b2 2

 

要求按t1 分组,对于每组提取对应t3值最小的t2列,用于作为另外的查询的条件

 

例如 t1=01 t3最小的为1那么我们要得到的就是t2

 

请问这个能用一个语句解决么 

 

谢谢!~~

 

 


问题补充:表名:wu_plan <br />ID&nbsp;&nbsp;&nbsp;&nbsp; plan&nbsp;&nbsp;&nbsp;&nbsp; model&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; corp_code&nbsp;&nbsp;&nbsp; plannum&nbsp;&nbsp;&nbsp; prixis <br />1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00001&nbsp;&nbsp;&nbsp; exx22&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nokia&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 <br />2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00002&nbsp;&nbsp;&nbsp; lc001&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sony&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 <br />表名:wu_bom <br />ID&nbsp;&nbsp;&nbsp; plan&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pact&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; amount&nbsp;&nbsp;&nbsp;&nbsp; <br />1&nbsp;&nbsp;&nbsp;&nbsp; 00001&nbsp;&nbsp;&nbsp;&nbsp; aa1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 300 <br />2&nbsp;&nbsp;&nbsp;&nbsp; 00001&nbsp;&nbsp;&nbsp;&nbsp; aa2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 200 <br />3&nbsp;&nbsp;&nbsp;&nbsp; 00002&nbsp;&nbsp;&nbsp;&nbsp; bb1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 500 <br />4&nbsp;&nbsp;&nbsp;&nbsp; 00002&nbsp;&nbsp;&nbsp;&nbsp; bb2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 800 <br />5&nbsp;&nbsp;&nbsp;&nbsp; 00002&nbsp;&nbsp;&nbsp;&nbsp; bb3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 400 <br />查询这两个表中plan唯一,每一个plan中,amount最少的,plannum大于prixis的记录 <br />结果是: <br />ID&nbsp;&nbsp;&nbsp;&nbsp; plan&nbsp;&nbsp;&nbsp;&nbsp; model&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; corp_code&nbsp;&nbsp;&nbsp; plannum&nbsp;&nbsp;&nbsp; prixis&nbsp;&nbsp;&nbsp; pact&nbsp; amount <br />1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00001&nbsp;&nbsp;&nbsp; exx22&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nokia&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 200 <br />2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 00002&nbsp;&nbsp;&nbsp; lc001&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sony&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bb3&nbsp;&nbsp;&nbsp;&nbsp; 400 <br /> <br />我要解决的问题是这个,我的想法是,首先查出每个plan中amount最少的行对应的pact值,然后链接两个表再根据这个值取出相应的值,这样行得通么?或者有什么更优化的方法么。

问题补充:<div class="quote_title">xutao5641745 写道</div><div class="quote_div">我试了一下&nbsp;&nbsp;&nbsp;&nbsp; 我用的是&nbsp;&nbsp; mysql&nbsp; : <br /> <br />&nbsp; <pre name="code" class="java">
     CREATE TABLE `wu_bom` (
  `id` bigint(20) default NULL,
  `plan` varchar(20) default NULL,
  `pact` varchar(20) default NULL,
  `amount` int(11) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

/*Data for the table `wu_bom` */

insert  into `wu_bom`(`id`,`plan`,`pact`,`amount`) values (1,'00001','aa1',300),(2,'00001','aa2',200),(3,'00002','bb1',500),(4,'00002','bb2',800),(5,'00002','bb3',400);

/*Table structure for table `wu_plan` */

DROP TABLE IF EXISTS `wu_plan`;

CREATE TABLE `wu_plan` (
  `id` bigint(20) default NULL,
  `plan` varchar(20) default NULL,
  `model` varchar(20) default NULL,
  `corp_code` varchar(20) default NULL,
  `plannum` int(11) default NULL,
  `prixis` int(11) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

/*Data for the table `wu_plan` */

insert  into `wu_plan`(`id`,`plan`,`model`,`corp_code`,`plannum`,`prixis`) values (1,'00001','exx22','nokia',2000,2001),(2,'00002','lc001','sony',3000,0);

  </pre> <br /> <br />下面是查询语句: <br /> <br />&nbsp;&nbsp; <pre name="code" class="java">
set profiling=1;   //这个先执行

select b.id,b.plan,b.pact,b.amount,p.id,p.model,p.corp_code,p.plannum,p.prixis from wu_bom b
  inner join wu_plan p on b.plan=p.plan and plannum&gt;prixis
  group by plan order by amount;


show profiles;  //这个可以用来查看上面这条SQL语句执行的时间是多少秒
   </pre></div> <br /> <br />这样行不通吧,后面用了Group by 前面的列要包含在Group by字句或者聚合函数之中的吧 <br />
2011年7月22日 10:22

7个答案 按时间排序 按投票排序

0 0

采纳的答案

select wp.*, wb.* from wu_plan wp, wu_bom wb,
    (select plan, min(amount) amount from wu_bom group by plan) t
where t.plan=wb.plan and t.amount = wb.amount and t.plan = wp.plan
and wp.plannum>wp.prixis

2011年7月22日 12:37
0 0

引用

问题补充:
xutao5641745 写道
我试了一下     我用的是   mysql  :


Java代码  收藏代码

   1.      CREATE TABLE `wu_bom` ( 
   2.   `id` bigint(20) default NULL, 
   3.   `plan` varchar(20) default NULL, 
   4.   `pact` varchar(20) default NULL, 
   5.   `amount` int(11) default NULL 
   6. ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 
   7.  
   8. /*Data for the table `wu_bom` */ 
   9.  
  10. insert  into `wu_bom`(`id`,`plan`,`pact`,`amount`) values (1,'00001','aa1',300),(2,'00001','aa2',200),(3,'00002','bb1',500),(4,'00002','bb2',800),(5,'00002','bb3',400); 
  11.  
  12. /*Table structure for table `wu_plan` */ 
  13.  
  14. DROP TABLE IF EXISTS `wu_plan`; 
  15.  
  16. CREATE TABLE `wu_plan` ( 
  17.   `id` bigint(20) default NULL, 
  18.   `plan` varchar(20) default NULL, 
  19.   `model` varchar(20) default NULL, 
  20.   `corp_code` varchar(20) default NULL, 
  21.   `plannum` int(11) default NULL, 
  22.   `prixis` int(11) default NULL 
  23. ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 
  24.  
  25. /*Data for the table `wu_plan` */ 
  26.  
  27. insert  into `wu_plan`(`id`,`plan`,`model`,`corp_code`,`plannum`,`prixis`) values (1,'00001','exx22','nokia',2000,2001),(2,'00002','lc001','sony',3000,0); 
  28.  
  29.    

     CREATE TABLE `wu_bom` (
  `id` bigint(20) default NULL,
  `plan` varchar(20) default NULL,
  `pact` varchar(20) default NULL,
  `amount` int(11) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

/*Data for the table `wu_bom` */

insert  into `wu_bom`(`id`,`plan`,`pact`,`amount`) values (1,'00001','aa1',300),(2,'00001','aa2',200),(3,'00002','bb1',500),(4,'00002','bb2',800),(5,'00002','bb3',400);

/*Table structure for table `wu_plan` */

DROP TABLE IF EXISTS `wu_plan`;

CREATE TABLE `wu_plan` (
  `id` bigint(20) default NULL,
  `plan` varchar(20) default NULL,
  `model` varchar(20) default NULL,
  `corp_code` varchar(20) default NULL,
  `plannum` int(11) default NULL,
  `prixis` int(11) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

/*Data for the table `wu_plan` */

insert  into `wu_plan`(`id`,`plan`,`model`,`corp_code`,`plannum`,`prixis`) values (1,'00001','exx22','nokia',2000,2001),(2,'00002','lc001','sony',3000,0);

 



下面是查询语句:

 
Java代码  收藏代码

   1. set profiling=1;   //这个先执行 
   2.  
   3. select b.id,b.plan,b.pact,b.amount,p.id,p.model,p.corp_code,p.plannum,p.prixis from wu_bom b  
   4.   inner join wu_plan p on b.plan=p.plan and plannum>prixis  
   5.   group by plan order by amount; 
   6.  
   7.  
   8. show profiles;  //这个可以用来查看上面这条SQL语句执行的时间是多少秒 
   9.     

set profiling=1;   //这个先执行

select b.id,b.plan,b.pact,b.amount,p.id,p.model,p.corp_code,p.plannum,p.prixis from wu_bom b
  inner join wu_plan p on b.plan=p.plan and plannum>prixis
  group by plan order by amount;


show profiles;  //这个可以用来查看上面这条SQL语句执行的时间是多少秒
  



这样行不通吧,后面用了Group by 前面的列要包含在Group by字句或者聚合函数之中的吧


  楼主,代码全贴了,,,你怎么就不测试一下再来反问我呢?我是经过测试再发上来的。。。。。

你的这种态度让我很反感。。。。。。

    我的语句中没有包含聚合函数,,,自然 group by 的时候不需要包含前面查询的所有列。。。。

2011年7月22日 14:01
0 0

select wu_plan.*, pact, amount from wu_plan
left join (
 select * from wu_bom where plan in(
	select plan, min(amount) from wu_bom
	group by plan
 )
)tmp on wu_plan.plan = tmp.plan
where plannum > prixis

2011年7月22日 12:31
0 0

我试了一下     我用的是   mysql  :

 

     CREATE TABLE `wu_bom` (
  `id` bigint(20) default NULL,
  `plan` varchar(20) default NULL,
  `pact` varchar(20) default NULL,
  `amount` int(11) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

/*Data for the table `wu_bom` */

insert  into `wu_bom`(`id`,`plan`,`pact`,`amount`) values (1,'00001','aa1',300),(2,'00001','aa2',200),(3,'00002','bb1',500),(4,'00002','bb2',800),(5,'00002','bb3',400);

/*Table structure for table `wu_plan` */

DROP TABLE IF EXISTS `wu_plan`;

CREATE TABLE `wu_plan` (
  `id` bigint(20) default NULL,
  `plan` varchar(20) default NULL,
  `model` varchar(20) default NULL,
  `corp_code` varchar(20) default NULL,
  `plannum` int(11) default NULL,
  `prixis` int(11) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

/*Data for the table `wu_plan` */

insert  into `wu_plan`(`id`,`plan`,`model`,`corp_code`,`plannum`,`prixis`) values (1,'00001','exx22','nokia',2000,2001),(2,'00002','lc001','sony',3000,0);

  


下面是查询语句:

  
set profiling=1;   //这个先执行

select b.id,b.plan,b.pact,b.amount,p.id,p.model,p.corp_code,p.plannum,p.prixis from wu_bom b 
  inner join wu_plan p on b.plan=p.plan and plannum>prixis 
  group by plan order by amount;


show profiles;  //这个可以用来查看上面这条SQL语句执行的时间是多少秒
   

2011年7月22日 11:43
0 0

select t2  from  table1 c ,(select t1 , min(t3) ts from  table1 a   group by t1) b where c.t1 = b.t1 and c.t3 = b.ts 

2011年7月22日 11:09
0 0


CREATE TABLE `test` (
  `t1` varchar(20) default NULL,
  `t2` varchar(20) default NULL,
  `t3` varchar(20) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

/*Data for the table `test` */

insert  into `test`(`t1`,`t2`,`t3`) values ('01','a1','1'),('01','a2','2'),('02','b1','1'),('02','b2','2');


select t1,t2,t3 from test group by t1 order by t3我用的是mysql


执行结果:
 
   t1         t2         t3

   01         a1         1
   02         b1         1



2011年7月22日 11:09
0 0

--oracle 不知是否满足要求
select a1.t2 from test a1 where a1.t3 in(select min(a2.t3) from test a2  group by a2.t1) and  a1.t1='01';

2011年7月22日 11:08

相关推荐

    求教C++等等一系列的问题

    #### 标题解析:“求教C++等等一系列的问题” 这一标题表明了提问者对于C++及可能的其他编程技术存在疑问。C++是一种广泛应用于系统软件开发、游戏开发等多个领域的高级编程语言。对于初学者来说,掌握C++不仅能够...

    我求教高手的VB毕业设计

    4. **命令执行**:使用Command对象的Execute方法来执行SQL语句,确保参数化查询正确,如果有的话。 5. **事务处理**:如果插入操作涉及多条记录,可能需要使用事务来保证数据的一致性。在VB中,你可以通过设置...

    求教一个asp调试的问题

    asp调试的问题

    求教电力系统仿真-039THDAT.DAT

    求教电力系统仿真-039THDAT.DAT ...现在要用matlab仿真一个39-bus的电力系统在一个故障(如其中一条母线三相短路)清除后的一小段时间,各种发电机的功角特性图,请大神告知详细步骤与操作,谢谢!节点数据见附件

    求教关于视窗中滚动条 滚动后 擦除和重绘的问题

    当窗口的一部分需要更新,比如滚动条移动导致部分区域不再可见,系统会发送这个消息给窗口处理程序。正确的响应通常是清除这一区域,以便为新的内容提供一个干净的背景。如果不进行擦除,旧内容可能会在新内容之上...

    vb-mschart查询Access数据

    标题“vb-mschart查询Access数据”涉及到的是使用Visual Basic (VB)编程语言,结合...在实际操作中,需要注意连接字符串的正确配置、SQL查询语句的编写、以及MSChart控件的属性设置,以确保数据能够正确、有效地显示。

    公式求教共4页.pdf.zip

    很抱歉,根据您提供的信息,"公式求教共4页.pdf.zip"似乎是一个包含四页公式教学内容的PDF文档的压缩文件,而"赚钱项目"可能是压缩包中的另一个文件或者是一个误输入。然而,没有实际的文件内容,我无法提供具体的IT...

    PHP推广链接的修改,求教

    PHP推广链接的修改,求教 这两个文件可以随时互换的,只是推广链接的形式不同;

    extjs文件浏览框问题求教

    标题中的“extjs文件浏览框问题求教”表明这是一个关于ExtJS框架中文件选择或浏览功能的编程问题。ExtJS是一个流行的JavaScript库,用于构建富客户端应用程序,它提供了丰富的组件库,包括文件浏览框(FileBrowser)...

    java考试题 求教

    ### 一、定义员工类 首先,我们需要创建一个员工类`Emp`来存储员工信息。该类包含四个属性:`emp_id`(员工编号)、`emp_name`(员工姓名)、`job`(职位)和`salary`(薪资)。同时为了方便后续操作,我们还需要...

    挂机软件破解求教

    请高手破解注册码,软件没有加壳,所有请告诉把注册码搞出来

    求教类似SUMIF;COUNTIF的条件函数的写法???

    求教类似SUMIF;COUNTIF的条件函数的写法???

    求教现在的主流3D网络游戏开发都使用哪些技术.doc

    求教现在的主流3D网络游戏开发都使用哪些技术.doc

    卡尔曼滤波求教-chap1_27.mdl

    卡尔曼滤波求教-chap1_27.mdl 在看关于卡尔曼滤波的东西,下面是在教程上看到的一个实例,仿真结果也没有问题,但是输出的结果都是关于变量Y(n)的,我想问一下有没有办法可以显示出X(n)的变化过程,谢谢

    卡尔曼滤波求教-chap1_27f.m

    卡尔曼滤波求教-chap1_27f.m 在看关于卡尔曼滤波的东西,下面是在教程上看到的一个实例,仿真结果也没有问题,但是输出的结果都是关于变量Y(n)的,我想问一下有没有办法可以显示出X(n)的变化过程,谢谢

    卡尔曼滤波求教-chap1_27plot.m

    卡尔曼滤波求教-chap1_27plot.m 在看关于卡尔曼滤波的东西,下面是在教程上看到的一个实例,仿真结果也没有问题,但是输出的结果都是关于变量Y(n)的,我想问一下有没有办法可以显示出X(n)的变化过程,谢谢

Global site tag (gtag.js) - Google Analytics