I have the following tables:
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
)
And:
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
)
How do I SELECT only the orders
that have all of its items with status completed
?
I tried with this:
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'
But that returns only the orders that have some items completed
, not all.
EDIT: An order
with no items
is also a valid result.