2017-01-26 22 views
1

我試圖從三個表中選擇數據,表中有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的?甲骨文? –

回答

0
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返回一個文件 –