Get those items which are ordered after they have been delivered
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 |
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 '18 at 16:11
@P.Salmon,orderid
column and notitemid
column.
– abbas
Nov 22 '18 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 '18 at 17:46
add a comment |
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 '18 at 17:44
Strawberry
26.7k83250
26.7k83250
asked Nov 22 '18 at 16:05
abbasabbas
124111
124111
'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 '18 at 16:11
@P.Salmon,orderid
column and notitemid
column.
– abbas
Nov 22 '18 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 '18 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 '18 at 16:11
@P.Salmon,orderid
column and notitemid
column.
– abbas
Nov 22 '18 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 '18 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 '18 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 '18 at 16:11
@P.Salmon,
orderid
column and not itemid
column.– abbas
Nov 22 '18 at 16:19
@P.Salmon,
orderid
column and not itemid
column.– abbas
Nov 22 '18 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 '18 at 17:46
Note that DISTINCT is not a function. It takes no arguments, so those parentheses are not doing anything.
– Strawberry
Nov 22 '18 at 17:46
add a comment |
2 Answers
2
active
oldest
votes
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 '18 at 9:24
@abbas you can simply changeJOIN
toLEFT JOIN
. your problem statement did not mention about this possible case.
– Madhur Bhaiya
Nov 24 '18 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 '18 at 11:17
@abbas please check the edited answer.
– Madhur Bhaiya
Nov 24 '18 at 11:27
add a comment |
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 '18 at 17:21
@Strawberry, I have edited my answer.
– abbas
Nov 23 '18 at 3:13
@MadhurBhaiya, I have commented on your answer.
– abbas
Nov 24 '18 at 9:24
@MadhurBhaiya; replied your answer.
– abbas
Nov 24 '18 at 11:23
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '18 at 9:24
@abbas you can simply changeJOIN
toLEFT JOIN
. your problem statement did not mention about this possible case.
– Madhur Bhaiya
Nov 24 '18 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 '18 at 11:17
@abbas please check the edited answer.
– Madhur Bhaiya
Nov 24 '18 at 11:27
add a comment |
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 '18 at 9:24
@abbas you can simply changeJOIN
toLEFT JOIN
. your problem statement did not mention about this possible case.
– Madhur Bhaiya
Nov 24 '18 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 '18 at 11:17
@abbas please check the edited answer.
– Madhur Bhaiya
Nov 24 '18 at 11:27
add a comment |
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 '18 at 11:27
answered Nov 22 '18 at 17:21
Madhur BhaiyaMadhur Bhaiya
19.6k62236
19.6k62236
your query works if "itemid" 1 exists in "itemdelivered"table
, if "itemid" 1 doesn't exist, then the result is blank.
– abbas
Nov 24 '18 at 9:24
@abbas you can simply changeJOIN
toLEFT JOIN
. your problem statement did not mention about this possible case.
– Madhur Bhaiya
Nov 24 '18 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 '18 at 11:17
@abbas please check the edited answer.
– Madhur Bhaiya
Nov 24 '18 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 '18 at 9:24
@abbas you can simply changeJOIN
toLEFT JOIN
. your problem statement did not mention about this possible case.
– Madhur Bhaiya
Nov 24 '18 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 '18 at 11:17
@abbas please check the edited answer.
– Madhur Bhaiya
Nov 24 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 11:17
@abbas please check the edited answer.
– Madhur Bhaiya
Nov 24 '18 at 11:27
@abbas please check the edited answer.
– Madhur Bhaiya
Nov 24 '18 at 11:27
add a comment |
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 '18 at 17:21
@Strawberry, I have edited my answer.
– abbas
Nov 23 '18 at 3:13
@MadhurBhaiya, I have commented on your answer.
– abbas
Nov 24 '18 at 9:24
@MadhurBhaiya; replied your answer.
– abbas
Nov 24 '18 at 11:23
add a comment |
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 '18 at 17:21
@Strawberry, I have edited my answer.
– abbas
Nov 23 '18 at 3:13
@MadhurBhaiya, I have commented on your answer.
– abbas
Nov 24 '18 at 9:24
@MadhurBhaiya; replied your answer.
– abbas
Nov 24 '18 at 11:23
add a comment |
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 '18 at 3:11
answered Nov 22 '18 at 17:18
abbasabbas
124111
124111
@MadhurBhaiya can you please elaborate your point.
– abbas
Nov 22 '18 at 17:21
@Strawberry, I have edited my answer.
– abbas
Nov 23 '18 at 3:13
@MadhurBhaiya, I have commented on your answer.
– abbas
Nov 24 '18 at 9:24
@MadhurBhaiya; replied your answer.
– abbas
Nov 24 '18 at 11:23
add a comment |
@MadhurBhaiya can you please elaborate your point.
– abbas
Nov 22 '18 at 17:21
@Strawberry, I have edited my answer.
– abbas
Nov 23 '18 at 3:13
@MadhurBhaiya, I have commented on your answer.
– abbas
Nov 24 '18 at 9:24
@MadhurBhaiya; replied your answer.
– abbas
Nov 24 '18 at 11:23
@MadhurBhaiya can you please elaborate your point.
– abbas
Nov 22 '18 at 17:21
@MadhurBhaiya can you please elaborate your point.
– abbas
Nov 22 '18 at 17:21
@Strawberry, I have edited my answer.
– abbas
Nov 23 '18 at 3:13
@Strawberry, I have edited my answer.
– abbas
Nov 23 '18 at 3:13
@MadhurBhaiya, I have commented on your answer.
– abbas
Nov 24 '18 at 9:24
@MadhurBhaiya, I have commented on your answer.
– abbas
Nov 24 '18 at 9:24
@MadhurBhaiya; replied your answer.
– abbas
Nov 24 '18 at 11:23
@MadhurBhaiya; replied your answer.
– abbas
Nov 24 '18 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.
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 '18 at 16:11
@P.Salmon,
orderid
column and notitemid
column.– abbas
Nov 22 '18 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 '18 at 17:46