Get those items which are ordered after they have been delivered
up vote
1
down vote
favorite
I have two tables
, namely itemOrders and itemDelivered.
itemOrders
+-------+---------+--------+
| id | orderid | itemid |
+-------+---------+--------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 2 | 2 |
| 5 | 3 | 1 |
| 6 | 3 | 2 |
+-------+---------+--------+
And
itemDelivered
+-------+-------------+--------+
| id | orderId | itemid |
+-------+-------------+--------+
| 1 | 2 | 2 |
| 2 | 3 | 2 |
| 3 | 2 | 1 |
+-------+-------------+--------+
From the above scenario I want all those distinct
items whose max
orderId in the table
itemDelivered is less than max
orderId in the table
itemOrders.
In the above example I should get itemid 1 as the result, as it's max
orderid is 2 in table
itemDelivered, which is less than its max
orderid in table
itemOrders which is 3.
I wrote the following query
but it gives me both the items, 1 and 2 as item No. 2 doesn't have orderId 1 in itemDelivered table
.
SELECT DISTINCT( itemid )
FROM itemorders
WHERE orderid NOT IN (SELECT orderid
FROM itemdelivered)
mysql
add a comment |
up vote
1
down vote
favorite
I have two tables
, namely itemOrders and itemDelivered.
itemOrders
+-------+---------+--------+
| id | orderid | itemid |
+-------+---------+--------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 2 | 2 |
| 5 | 3 | 1 |
| 6 | 3 | 2 |
+-------+---------+--------+
And
itemDelivered
+-------+-------------+--------+
| id | orderId | itemid |
+-------+-------------+--------+
| 1 | 2 | 2 |
| 2 | 3 | 2 |
| 3 | 2 | 1 |
+-------+-------------+--------+
From the above scenario I want all those distinct
items whose max
orderId in the table
itemDelivered is less than max
orderId in the table
itemOrders.
In the above example I should get itemid 1 as the result, as it's max
orderid is 2 in table
itemDelivered, which is less than its max
orderid in table
itemOrders which is 3.
I wrote the following query
but it gives me both the items, 1 and 2 as item No. 2 doesn't have orderId 1 in itemDelivered table
.
SELECT DISTINCT( itemid )
FROM itemorders
WHERE orderid NOT IN (SELECT orderid
FROM itemdelivered)
mysql
'less than its max orderid in table itemOrders which is 3.' - there is no itemid 3 in itemorders for orderid 1.
– P.Salmon
Nov 22 at 16:11
@P.Salmon,orderid
column and notitemid
column.
– abbas
Nov 22 at 16:19
1
Note that DISTINCT is not a function. It takes no arguments, so those parentheses are not doing anything.
– Strawberry
Nov 22 at 17:46
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have two tables
, namely itemOrders and itemDelivered.
itemOrders
+-------+---------+--------+
| id | orderid | itemid |
+-------+---------+--------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 2 | 2 |
| 5 | 3 | 1 |
| 6 | 3 | 2 |
+-------+---------+--------+
And
itemDelivered
+-------+-------------+--------+
| id | orderId | itemid |
+-------+-------------+--------+
| 1 | 2 | 2 |
| 2 | 3 | 2 |
| 3 | 2 | 1 |
+-------+-------------+--------+
From the above scenario I want all those distinct
items whose max
orderId in the table
itemDelivered is less than max
orderId in the table
itemOrders.
In the above example I should get itemid 1 as the result, as it's max
orderid is 2 in table
itemDelivered, which is less than its max
orderid in table
itemOrders which is 3.
I wrote the following query
but it gives me both the items, 1 and 2 as item No. 2 doesn't have orderId 1 in itemDelivered table
.
SELECT DISTINCT( itemid )
FROM itemorders
WHERE orderid NOT IN (SELECT orderid
FROM itemdelivered)
mysql
I have two tables
, namely itemOrders and itemDelivered.
itemOrders
+-------+---------+--------+
| id | orderid | itemid |
+-------+---------+--------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 2 | 2 |
| 5 | 3 | 1 |
| 6 | 3 | 2 |
+-------+---------+--------+
And
itemDelivered
+-------+-------------+--------+
| id | orderId | itemid |
+-------+-------------+--------+
| 1 | 2 | 2 |
| 2 | 3 | 2 |
| 3 | 2 | 1 |
+-------+-------------+--------+
From the above scenario I want all those distinct
items whose max
orderId in the table
itemDelivered is less than max
orderId in the table
itemOrders.
In the above example I should get itemid 1 as the result, as it's max
orderid is 2 in table
itemDelivered, which is less than its max
orderid in table
itemOrders which is 3.
I wrote the following query
but it gives me both the items, 1 and 2 as item No. 2 doesn't have orderId 1 in itemDelivered table
.
SELECT DISTINCT( itemid )
FROM itemorders
WHERE orderid NOT IN (SELECT orderid
FROM itemdelivered)
mysql
mysql
edited Nov 22 at 17:44
Strawberry
25.8k83149
25.8k83149
asked Nov 22 at 16:05
abbas
124110
124110
'less than its max orderid in table itemOrders which is 3.' - there is no itemid 3 in itemorders for orderid 1.
– P.Salmon
Nov 22 at 16:11
@P.Salmon,orderid
column and notitemid
column.
– abbas
Nov 22 at 16:19
1
Note that DISTINCT is not a function. It takes no arguments, so those parentheses are not doing anything.
– Strawberry
Nov 22 at 17:46
add a comment |
'less than its max orderid in table itemOrders which is 3.' - there is no itemid 3 in itemorders for orderid 1.
– P.Salmon
Nov 22 at 16:11
@P.Salmon,orderid
column and notitemid
column.
– abbas
Nov 22 at 16:19
1
Note that DISTINCT is not a function. It takes no arguments, so those parentheses are not doing anything.
– Strawberry
Nov 22 at 17:46
'less than its max orderid in table itemOrders which is 3.' - there is no itemid 3 in itemorders for orderid 1.
– P.Salmon
Nov 22 at 16:11
'less than its max orderid in table itemOrders which is 3.' - there is no itemid 3 in itemorders for orderid 1.
– P.Salmon
Nov 22 at 16:11
@P.Salmon,
orderid
column and not itemid
column.– abbas
Nov 22 at 16:19
@P.Salmon,
orderid
column and not itemid
column.– abbas
Nov 22 at 16:19
1
1
Note that DISTINCT is not a function. It takes no arguments, so those parentheses are not doing anything.
– Strawberry
Nov 22 at 17:46
Note that DISTINCT is not a function. It takes no arguments, so those parentheses are not doing anything.
– Strawberry
Nov 22 at 17:46
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
You can LEFT JOIN
between the two table using itemid
, and GROUP BY
on the itemid
.
Eventually use HAVING
clause to consider only those itemid
values, where MAX(itemdelivered.orderid) < MAX(itemorders.orderid)
View on DB Fiddle
SELECT io.itemid
FROM itemorders AS io
LEFT JOIN itemdelivered AS id
ON id.itemid = io.itemid
GROUP BY io.itemid
HAVING MAX(id.orderid) < MAX(io.orderid)
OR MAX(id.orderid) IS NULL
Result
| itemid |
| ------ |
| 1 |
your query works if "itemid" 1 exists in "itemdelivered"table
, if "itemid" 1 doesn't exist, then the result is blank.
– abbas
Nov 24 at 9:24
@abbas you can simply changeJOIN
toLEFT JOIN
. your problem statement did not mention about this possible case.
– Madhur Bhaiya
Nov 24 at 9:26
insert into itemdelivered(orderid, itemid) values (2,2),(3,2);
If you have this query in DB Fiddle, then the output is empty.
– abbas
Nov 24 at 11:17
@abbas please check the edited answer.
– Madhur Bhaiya
Nov 24 at 11:27
add a comment |
up vote
1
down vote
Ok, manage to write a query which gives the desired output.
SELECT io.itemid
FROM itemorders as io
LEFT JOIN itemdelivered AS id ON io.orderid = id.orderid AND io.itemid = id.itemid
WHERE id.itemid IS NULL
HAVING MAX(id.orderid) IS NULL
ORDER BY io.id
@MadhurBhaiya can you please elaborate your point.
– abbas
Nov 22 at 17:21
@Strawberry, I have edited my answer.
– abbas
Nov 23 at 3:13
@MadhurBhaiya, I have commented on your answer.
– abbas
Nov 24 at 9:24
@MadhurBhaiya; replied your answer.
– abbas
Nov 24 at 11:23
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You can LEFT JOIN
between the two table using itemid
, and GROUP BY
on the itemid
.
Eventually use HAVING
clause to consider only those itemid
values, where MAX(itemdelivered.orderid) < MAX(itemorders.orderid)
View on DB Fiddle
SELECT io.itemid
FROM itemorders AS io
LEFT JOIN itemdelivered AS id
ON id.itemid = io.itemid
GROUP BY io.itemid
HAVING MAX(id.orderid) < MAX(io.orderid)
OR MAX(id.orderid) IS NULL
Result
| itemid |
| ------ |
| 1 |
your query works if "itemid" 1 exists in "itemdelivered"table
, if "itemid" 1 doesn't exist, then the result is blank.
– abbas
Nov 24 at 9:24
@abbas you can simply changeJOIN
toLEFT JOIN
. your problem statement did not mention about this possible case.
– Madhur Bhaiya
Nov 24 at 9:26
insert into itemdelivered(orderid, itemid) values (2,2),(3,2);
If you have this query in DB Fiddle, then the output is empty.
– abbas
Nov 24 at 11:17
@abbas please check the edited answer.
– Madhur Bhaiya
Nov 24 at 11:27
add a comment |
up vote
1
down vote
accepted
You can LEFT JOIN
between the two table using itemid
, and GROUP BY
on the itemid
.
Eventually use HAVING
clause to consider only those itemid
values, where MAX(itemdelivered.orderid) < MAX(itemorders.orderid)
View on DB Fiddle
SELECT io.itemid
FROM itemorders AS io
LEFT JOIN itemdelivered AS id
ON id.itemid = io.itemid
GROUP BY io.itemid
HAVING MAX(id.orderid) < MAX(io.orderid)
OR MAX(id.orderid) IS NULL
Result
| itemid |
| ------ |
| 1 |
your query works if "itemid" 1 exists in "itemdelivered"table
, if "itemid" 1 doesn't exist, then the result is blank.
– abbas
Nov 24 at 9:24
@abbas you can simply changeJOIN
toLEFT JOIN
. your problem statement did not mention about this possible case.
– Madhur Bhaiya
Nov 24 at 9:26
insert into itemdelivered(orderid, itemid) values (2,2),(3,2);
If you have this query in DB Fiddle, then the output is empty.
– abbas
Nov 24 at 11:17
@abbas please check the edited answer.
– Madhur Bhaiya
Nov 24 at 11:27
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You can LEFT JOIN
between the two table using itemid
, and GROUP BY
on the itemid
.
Eventually use HAVING
clause to consider only those itemid
values, where MAX(itemdelivered.orderid) < MAX(itemorders.orderid)
View on DB Fiddle
SELECT io.itemid
FROM itemorders AS io
LEFT JOIN itemdelivered AS id
ON id.itemid = io.itemid
GROUP BY io.itemid
HAVING MAX(id.orderid) < MAX(io.orderid)
OR MAX(id.orderid) IS NULL
Result
| itemid |
| ------ |
| 1 |
You can LEFT JOIN
between the two table using itemid
, and GROUP BY
on the itemid
.
Eventually use HAVING
clause to consider only those itemid
values, where MAX(itemdelivered.orderid) < MAX(itemorders.orderid)
View on DB Fiddle
SELECT io.itemid
FROM itemorders AS io
LEFT JOIN itemdelivered AS id
ON id.itemid = io.itemid
GROUP BY io.itemid
HAVING MAX(id.orderid) < MAX(io.orderid)
OR MAX(id.orderid) IS NULL
Result
| itemid |
| ------ |
| 1 |
edited Nov 24 at 11:27
answered Nov 22 at 17:21
Madhur Bhaiya
19.2k62236
19.2k62236
your query works if "itemid" 1 exists in "itemdelivered"table
, if "itemid" 1 doesn't exist, then the result is blank.
– abbas
Nov 24 at 9:24
@abbas you can simply changeJOIN
toLEFT JOIN
. your problem statement did not mention about this possible case.
– Madhur Bhaiya
Nov 24 at 9:26
insert into itemdelivered(orderid, itemid) values (2,2),(3,2);
If you have this query in DB Fiddle, then the output is empty.
– abbas
Nov 24 at 11:17
@abbas please check the edited answer.
– Madhur Bhaiya
Nov 24 at 11:27
add a comment |
your query works if "itemid" 1 exists in "itemdelivered"table
, if "itemid" 1 doesn't exist, then the result is blank.
– abbas
Nov 24 at 9:24
@abbas you can simply changeJOIN
toLEFT JOIN
. your problem statement did not mention about this possible case.
– Madhur Bhaiya
Nov 24 at 9:26
insert into itemdelivered(orderid, itemid) values (2,2),(3,2);
If you have this query in DB Fiddle, then the output is empty.
– abbas
Nov 24 at 11:17
@abbas please check the edited answer.
– Madhur Bhaiya
Nov 24 at 11:27
your query works if "itemid" 1 exists in "itemdelivered"
table
, if "itemid" 1 doesn't exist, then the result is blank.– abbas
Nov 24 at 9:24
your query works if "itemid" 1 exists in "itemdelivered"
table
, if "itemid" 1 doesn't exist, then the result is blank.– abbas
Nov 24 at 9:24
@abbas you can simply change
JOIN
to LEFT JOIN
. your problem statement did not mention about this possible case.– Madhur Bhaiya
Nov 24 at 9:26
@abbas you can simply change
JOIN
to LEFT JOIN
. your problem statement did not mention about this possible case.– Madhur Bhaiya
Nov 24 at 9:26
insert into itemdelivered(orderid, itemid) values (2,2),(3,2);
If you have this query in DB Fiddle, then the output is empty.– abbas
Nov 24 at 11:17
insert into itemdelivered(orderid, itemid) values (2,2),(3,2);
If you have this query in DB Fiddle, then the output is empty.– abbas
Nov 24 at 11:17
@abbas please check the edited answer.
– Madhur Bhaiya
Nov 24 at 11:27
@abbas please check the edited answer.
– Madhur Bhaiya
Nov 24 at 11:27
add a comment |
up vote
1
down vote
Ok, manage to write a query which gives the desired output.
SELECT io.itemid
FROM itemorders as io
LEFT JOIN itemdelivered AS id ON io.orderid = id.orderid AND io.itemid = id.itemid
WHERE id.itemid IS NULL
HAVING MAX(id.orderid) IS NULL
ORDER BY io.id
@MadhurBhaiya can you please elaborate your point.
– abbas
Nov 22 at 17:21
@Strawberry, I have edited my answer.
– abbas
Nov 23 at 3:13
@MadhurBhaiya, I have commented on your answer.
– abbas
Nov 24 at 9:24
@MadhurBhaiya; replied your answer.
– abbas
Nov 24 at 11:23
add a comment |
up vote
1
down vote
Ok, manage to write a query which gives the desired output.
SELECT io.itemid
FROM itemorders as io
LEFT JOIN itemdelivered AS id ON io.orderid = id.orderid AND io.itemid = id.itemid
WHERE id.itemid IS NULL
HAVING MAX(id.orderid) IS NULL
ORDER BY io.id
@MadhurBhaiya can you please elaborate your point.
– abbas
Nov 22 at 17:21
@Strawberry, I have edited my answer.
– abbas
Nov 23 at 3:13
@MadhurBhaiya, I have commented on your answer.
– abbas
Nov 24 at 9:24
@MadhurBhaiya; replied your answer.
– abbas
Nov 24 at 11:23
add a comment |
up vote
1
down vote
up vote
1
down vote
Ok, manage to write a query which gives the desired output.
SELECT io.itemid
FROM itemorders as io
LEFT JOIN itemdelivered AS id ON io.orderid = id.orderid AND io.itemid = id.itemid
WHERE id.itemid IS NULL
HAVING MAX(id.orderid) IS NULL
ORDER BY io.id
Ok, manage to write a query which gives the desired output.
SELECT io.itemid
FROM itemorders as io
LEFT JOIN itemdelivered AS id ON io.orderid = id.orderid AND io.itemid = id.itemid
WHERE id.itemid IS NULL
HAVING MAX(id.orderid) IS NULL
ORDER BY io.id
edited Nov 23 at 3:11
answered Nov 22 at 17:18
abbas
124110
124110
@MadhurBhaiya can you please elaborate your point.
– abbas
Nov 22 at 17:21
@Strawberry, I have edited my answer.
– abbas
Nov 23 at 3:13
@MadhurBhaiya, I have commented on your answer.
– abbas
Nov 24 at 9:24
@MadhurBhaiya; replied your answer.
– abbas
Nov 24 at 11:23
add a comment |
@MadhurBhaiya can you please elaborate your point.
– abbas
Nov 22 at 17:21
@Strawberry, I have edited my answer.
– abbas
Nov 23 at 3:13
@MadhurBhaiya, I have commented on your answer.
– abbas
Nov 24 at 9:24
@MadhurBhaiya; replied your answer.
– abbas
Nov 24 at 11:23
@MadhurBhaiya can you please elaborate your point.
– abbas
Nov 22 at 17:21
@MadhurBhaiya can you please elaborate your point.
– abbas
Nov 22 at 17:21
@Strawberry, I have edited my answer.
– abbas
Nov 23 at 3:13
@Strawberry, I have edited my answer.
– abbas
Nov 23 at 3:13
@MadhurBhaiya, I have commented on your answer.
– abbas
Nov 24 at 9:24
@MadhurBhaiya, I have commented on your answer.
– abbas
Nov 24 at 9:24
@MadhurBhaiya; replied your answer.
– abbas
Nov 24 at 11:23
@MadhurBhaiya; replied your answer.
– abbas
Nov 24 at 11:23
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434698%2fget-those-items-which-are-ordered-after-they-have-been-delivered%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
'less than its max orderid in table itemOrders which is 3.' - there is no itemid 3 in itemorders for orderid 1.
– P.Salmon
Nov 22 at 16:11
@P.Salmon,
orderid
column and notitemid
column.– abbas
Nov 22 at 16:19
1
Note that DISTINCT is not a function. It takes no arguments, so those parentheses are not doing anything.
– Strawberry
Nov 22 at 17:46