1

如何获取表中所有订单的列表,除了(已引用的订单和类型为-1)

订单表:

id   |   reference_id  | type
---------------------------------- 
1    |                 | 1
---------------------------------- 
2    |                 | 1
---------------------------------- 
3    |   1             | -1
----------------------------------

就像是:

list = ArrayList();

if( order.type > 0 ){
    if( order.id != other_order.reference_id )
        list.add(order)
}

如何在 MySQL 语句中执行此操作?

此语句的结果也相同,但使用 JOIN....等:

select * from orders as a
where a.type > 0 AND not exists 
(select * from orders as b where a.id = b.ref_id)

谢谢

4

2 回答 2

1

您是否正在寻找这个:

select * from orders
where not exists 
(select * from orders o where o.id = o.reference_id and type = -1)
于 2013-08-26T14:33:27.583 回答
1

这将为您提供那些被引用的记录和那些有效的记录

SELECT * 
FROM   yourtable A 
       INNER JOIN yourtable B 
               ON A.reference_id = B.order_id 
WHERE  B.reference_type > 0; 
于 2013-08-26T14:34:55.890 回答