从三个表中选择具有maximo的数据<> 5

问题描述:

我试图从三个表中选择数据,表中有ID号和时间戳,表2中有ID号,数量和描述,表3中有ID号和状态。从三个表中选择具有maximo的数据<> 5

我旁边表:

TABLE ordenes 
|id_orden|fecha_registro 
| 1 |15-Oct-2016 12:00:05 
| 2 |15-Oct-2016 12:35:55 
| 3 |15-Oct-2016 12:42:20 



TABLE pre_ordenes 
|id_orden|cantidad|descripcion 
| 1 | 1 |cd adele 
| 1 | 2 |cd sia 
| 2 | 2 |cd rihana 
| 2 | 2 |cd rita 
| 2 | 2 |cd adele 
| 3 | 2 |cd sia 
| 3 | 2 |cd rihana 
| 3 | 2 |cd adele 
| 3 | 2 |cd marhia 

TABLE estado_orden/ 
|id_orden|tipo_estado 
| 1 |  1 
| 1 |  2 
| 1 |  3 
| 1 |  4 
| 1 |  5 
| 2 |  1 
| 2 |  2 
| 2 |  3 
| 2 |  4 
| 3 |  1 
| 3 |  2 
| 3 |  3 

霍伊我能获得这个?

|id_orden|fecha_registro  |cantidad|descripcion 
| 2 |15-Oct-2016 12:35:55 | 1 |cd rihana 
| 2 |15-Oct-2016 12:35:55 | 1 |cd rita 
| 2 |15-Oct-2016 12:35:55 | 1 |cd adele 
| 3 |15-Oct-2016 12:42:20 | 1 |cd sia 
| 3 |15-Oct-2016 12:42:20 | 1 |cd rihana 
| 3 |15-Oct-2016 12:42:20 | 1 |cd adele 
| 3 |15-Oct-2016 12:42:20 | 1 |cd marhia 

这是因为id_orden = 1具有tipo_estado = 5和id_orden 2和3 doesn't具有5作为tipo_estado

+2

我们不在SO:D实际上并没有为你做这些工作。如果您正在寻求帮助,请发布您迄今为止的内容,社区将为您提供帮助。 – GabrielOshiro

+0

我尝试很多句子,但没有人工作,我不想让你做我的工作,感谢您的建议,下次添加句子甚至是错误的。 –

+0

你正在使用哪些DBMS? Postgres的?甲骨文? –

select o.id_orden, o.fecha_registro, p.cantidad, p.descripcion 
    from (ordenes o 
    left join pre_ordenes p 
    on o.id_orden = p.id_orden) 
    join estado_orden e 
    on o.id_orden = e.id_orden 
    where e.tipo_estado <> 5 

或尝试以下作为替代

select o.id_orden, o.fecha_registro, p.cantidad, p.descripcion 
    from (ordenes o 
    left join pre_ordenes p 
    on o.id_orden = p.id_orden) 
    where o.id_orden in (select e.id_orden 
         from estado_orden e 
         where e.tipo_estado <> 5) 
+0

将它标记为已回答,如果它有效。我很好奇,如果第一个也适用,我添加了括号并将最后一个加入加入为加入 – Dong

+0

但我只需要ordenes,没有estado_orden = 5 ant最后一个替代方案显示所有ordenes :( –

+0

第一个为每个tipo_estado返回一个文件 –