如何选择所有已完成项目的订单?

时间:2018-03-19 13:33:36

标签: mysql sql database

我有以下表格:

CREATE TABLE `orders` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(10) unsigned NOT NULL,
  `billing_profile_id` int(10) unsigned NOT NULL,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `currency` varchar(3) COLLATE utf8_unicode_ci NOT NULL,
  `valid_until` timestamp NULL DEFAULT NULL
)

CREATE TABLE `items` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `order_id` int(10) unsigned NOT NULL,
  `status` enum('pending','processing','completed','on-hold','cancelled'),
  `code` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `quantity` int(10) unsigned NOT NULL DEFAULT '1',
  `term` int(10) unsigned NOT NULL,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `discounts` int(10) unsigned NOT NULL,
  `total` int(10) unsigned NOT NULL
)

如何仅选择状态为orders的项目全部completed

我试过这个:

SELECT o.* FROM 
FROM orders o INNER JOIN items i on o.id=i.order_id
GROUP BY o.id, i.status
HAVING i.status = 'completed'

但这仅返回某些completed而不是所有的订单。

编辑:没有order的{​​{1}}也是有效的结果。

3 个答案:

答案 0 :(得分:4)

试试这个:

SELECT *
FROM   orders
WHERE  EXISTS (SELECT *
               FROM   items
               WHERE  orders.id = items.order_id
                 AND  items.status = 'completed')
  AND  NOT EXISTS (SELECT *
                   FROM   items
                   WHERE  orders.id = items.order_id
                     AND  items.status <> 'completed')

答案 1 :(得分:1)

易:

select * from orders
  where id not in (
    select order_id from items where status <> 'completed'
  )

答案 2 :(得分:1)

DROP TABLE IF EXISTS orders;
CREATE TABLE orders (
  `id` SERIAL PRIMARY KEY
  );

INSERT INTO orders VALUES
(100),(101);

DROP TABLE IF EXISTS items;

CREATE TABLE `items` (
  `id` SERIAL PRIMARY KEY,
  `order_id` int(10) unsigned NOT NULL,
  `status` enum('pending','processing','completed','on-hold','cancelled')
);

INSERT INTO items VALUES
(1,100,'completed'),
(2,100,'completed'),
(3,100,'completed'),
(4,101,'completed'),
(5,101,'processing');


SELECT DISTINCT o.* 
           FROM orders o
           LEFT 
           JOIN items i 
             ON i.order_id = o.id 
            AND i.status <> 'completed' 
          WHERE i.id IS NULL;

+-----+
| id  |
+-----+
| 100 |
+-----+

...或...

SELECT DISTINCT o.* 
           FROM orders o
          WHERE NOT EXISTS (SELECT 1 
                              FROM items i 
                             WHERE i.order_id = o.id 
                               AND i.status <> 'completed');