我试图从三个表中选择数据,表1中有id号和时间戳,表2中有id号,数量和描述,表3中有id号和状态。
我有下表:
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没有5作为tipo_estado
答案 0 :(得分: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)