MySQL left join return return only one row












0















I am trying to find previous flight from the historical flight table, i have only 1 historical table wit tail number of plane and scheduled date departure in it, i want to make a new table with information of previous flight from that plane, below is example of the table



Original Table :



|flight number|scheduled departure flight|
| A | 1 Jan 2018 10:00 |
| A | 1 Jan 2018 19:00 |
| B | 1 Jan 2018 11:00 |
| B | 1 Jan 2018 19:00 |
| B | 1 Jan 2018 21:00 |


Result expected :



|flight number|scheduled departure flight| previous scheduled departure |
| A | 1 Jan 2018 10:00 | NULL |
| A | 1 Jan 2018 19:00 | 1 Jan 10:00 |
| B | 1 Jan 2018 11:00 | NULL |
| B | 1 Jan 2018 19:00 | 1 Jan 2018 11:00 |
| B | 1 Jan 2018 21:00 | 1 jan 2018 19:00 |


i have been working on it for a day, appreciate any help from you










share|improve this question


















  • 4





    Please, put your MySQL code and the error or result that you are receiving in order to help you. Thanks

    – Inazense
    Nov 14 '18 at 8:17
















0















I am trying to find previous flight from the historical flight table, i have only 1 historical table wit tail number of plane and scheduled date departure in it, i want to make a new table with information of previous flight from that plane, below is example of the table



Original Table :



|flight number|scheduled departure flight|
| A | 1 Jan 2018 10:00 |
| A | 1 Jan 2018 19:00 |
| B | 1 Jan 2018 11:00 |
| B | 1 Jan 2018 19:00 |
| B | 1 Jan 2018 21:00 |


Result expected :



|flight number|scheduled departure flight| previous scheduled departure |
| A | 1 Jan 2018 10:00 | NULL |
| A | 1 Jan 2018 19:00 | 1 Jan 10:00 |
| B | 1 Jan 2018 11:00 | NULL |
| B | 1 Jan 2018 19:00 | 1 Jan 2018 11:00 |
| B | 1 Jan 2018 21:00 | 1 jan 2018 19:00 |


i have been working on it for a day, appreciate any help from you










share|improve this question


















  • 4





    Please, put your MySQL code and the error or result that you are receiving in order to help you. Thanks

    – Inazense
    Nov 14 '18 at 8:17














0












0








0


1






I am trying to find previous flight from the historical flight table, i have only 1 historical table wit tail number of plane and scheduled date departure in it, i want to make a new table with information of previous flight from that plane, below is example of the table



Original Table :



|flight number|scheduled departure flight|
| A | 1 Jan 2018 10:00 |
| A | 1 Jan 2018 19:00 |
| B | 1 Jan 2018 11:00 |
| B | 1 Jan 2018 19:00 |
| B | 1 Jan 2018 21:00 |


Result expected :



|flight number|scheduled departure flight| previous scheduled departure |
| A | 1 Jan 2018 10:00 | NULL |
| A | 1 Jan 2018 19:00 | 1 Jan 10:00 |
| B | 1 Jan 2018 11:00 | NULL |
| B | 1 Jan 2018 19:00 | 1 Jan 2018 11:00 |
| B | 1 Jan 2018 21:00 | 1 jan 2018 19:00 |


i have been working on it for a day, appreciate any help from you










share|improve this question














I am trying to find previous flight from the historical flight table, i have only 1 historical table wit tail number of plane and scheduled date departure in it, i want to make a new table with information of previous flight from that plane, below is example of the table



Original Table :



|flight number|scheduled departure flight|
| A | 1 Jan 2018 10:00 |
| A | 1 Jan 2018 19:00 |
| B | 1 Jan 2018 11:00 |
| B | 1 Jan 2018 19:00 |
| B | 1 Jan 2018 21:00 |


Result expected :



|flight number|scheduled departure flight| previous scheduled departure |
| A | 1 Jan 2018 10:00 | NULL |
| A | 1 Jan 2018 19:00 | 1 Jan 10:00 |
| B | 1 Jan 2018 11:00 | NULL |
| B | 1 Jan 2018 19:00 | 1 Jan 2018 11:00 |
| B | 1 Jan 2018 21:00 | 1 jan 2018 19:00 |


i have been working on it for a day, appreciate any help from you







mysql left-join






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 8:14









Insan RamadhanInsan Ramadhan

31




31








  • 4





    Please, put your MySQL code and the error or result that you are receiving in order to help you. Thanks

    – Inazense
    Nov 14 '18 at 8:17














  • 4





    Please, put your MySQL code and the error or result that you are receiving in order to help you. Thanks

    – Inazense
    Nov 14 '18 at 8:17








4




4





Please, put your MySQL code and the error or result that you are receiving in order to help you. Thanks

– Inazense
Nov 14 '18 at 8:17





Please, put your MySQL code and the error or result that you are receiving in order to help you. Thanks

– Inazense
Nov 14 '18 at 8:17












1 Answer
1






active

oldest

votes


















0














You are looking for LAG window function, but which support mysql version higher than 8.0



You can try to use a subquery to make a lag window function.



Schema (MySQL v5.7)



Query #1



SELECT `flight number`,
`scheduled departure flight`,
(select `scheduled departure flight`
from T tt
where t1.`scheduled departure flight` > tt.`scheduled departure flight`
AND t1.`flight number` = tt.`flight number`
order by tt.`scheduled departure flight` desc
limit 1
) 'previous scheduled departure'
FROM T t1;

| flight number | scheduled departure flight | previous scheduled departure |
| ------------- | -------------------------- | ---------------------------- |
| A | 2018-06-01 10:00:00 | |
| A | 2018-06-01 19:00:00 | 2018-06-01 10:00:00 |
| B | 2018-06-01 11:00:00 | |
| B | 2018-06-01 19:00:00 | 2018-06-01 11:00:00 |
| B | 2018-06-01 21:00:00 | 2018-06-01 19:00:00 |




View on DB Fiddle



If your mysql version support LAG you can try this.



Schema (MySQL v8.0)



CREATE TABLE T(
`flight number` varchar(5),
`scheduled departure flight` datetime
);



INSERT INTO T VALUES ('A','2018-06-01 10:00');
INSERT INTO T VALUES ('A','2018-06-01 19:00');
INSERT INTO T VALUES ('B','2018-06-01 11:00');
INSERT INTO T VALUES ('B','2018-06-01 19:00');
INSERT INTO T VALUES ('B','2018-06-01 21:00');




Query #1



SELECT `flight number`,
`scheduled departure flight`,
LAG(`scheduled departure flight`) OVER(PARTITION BY `flight number` ORDER BY `scheduled departure flight`) 'previous scheduled departure'
FROM T t1;




View on DB Fiddle






share|improve this answer


























  • thankyou, i need to extract not only the date, but also all the information from previous flight , how to add another column in the result ?

    – Insan Ramadhan
    Nov 14 '18 at 8:47











  • What do your mean information? I didn't see this column.

    – D-Shih
    Nov 14 '18 at 8:50











  • actually there is another information in the table and i forgot to put it in the question

    – Insan Ramadhan
    Nov 14 '18 at 8:53











  • i just repeat the query for another information and it work, thanks

    – Insan Ramadhan
    Nov 14 '18 at 9:16











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295640%2fmysql-left-join-return-return-only-one-row%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You are looking for LAG window function, but which support mysql version higher than 8.0



You can try to use a subquery to make a lag window function.



Schema (MySQL v5.7)



Query #1



SELECT `flight number`,
`scheduled departure flight`,
(select `scheduled departure flight`
from T tt
where t1.`scheduled departure flight` > tt.`scheduled departure flight`
AND t1.`flight number` = tt.`flight number`
order by tt.`scheduled departure flight` desc
limit 1
) 'previous scheduled departure'
FROM T t1;

| flight number | scheduled departure flight | previous scheduled departure |
| ------------- | -------------------------- | ---------------------------- |
| A | 2018-06-01 10:00:00 | |
| A | 2018-06-01 19:00:00 | 2018-06-01 10:00:00 |
| B | 2018-06-01 11:00:00 | |
| B | 2018-06-01 19:00:00 | 2018-06-01 11:00:00 |
| B | 2018-06-01 21:00:00 | 2018-06-01 19:00:00 |




View on DB Fiddle



If your mysql version support LAG you can try this.



Schema (MySQL v8.0)



CREATE TABLE T(
`flight number` varchar(5),
`scheduled departure flight` datetime
);



INSERT INTO T VALUES ('A','2018-06-01 10:00');
INSERT INTO T VALUES ('A','2018-06-01 19:00');
INSERT INTO T VALUES ('B','2018-06-01 11:00');
INSERT INTO T VALUES ('B','2018-06-01 19:00');
INSERT INTO T VALUES ('B','2018-06-01 21:00');




Query #1



SELECT `flight number`,
`scheduled departure flight`,
LAG(`scheduled departure flight`) OVER(PARTITION BY `flight number` ORDER BY `scheduled departure flight`) 'previous scheduled departure'
FROM T t1;




View on DB Fiddle






share|improve this answer


























  • thankyou, i need to extract not only the date, but also all the information from previous flight , how to add another column in the result ?

    – Insan Ramadhan
    Nov 14 '18 at 8:47











  • What do your mean information? I didn't see this column.

    – D-Shih
    Nov 14 '18 at 8:50











  • actually there is another information in the table and i forgot to put it in the question

    – Insan Ramadhan
    Nov 14 '18 at 8:53











  • i just repeat the query for another information and it work, thanks

    – Insan Ramadhan
    Nov 14 '18 at 9:16
















0














You are looking for LAG window function, but which support mysql version higher than 8.0



You can try to use a subquery to make a lag window function.



Schema (MySQL v5.7)



Query #1



SELECT `flight number`,
`scheduled departure flight`,
(select `scheduled departure flight`
from T tt
where t1.`scheduled departure flight` > tt.`scheduled departure flight`
AND t1.`flight number` = tt.`flight number`
order by tt.`scheduled departure flight` desc
limit 1
) 'previous scheduled departure'
FROM T t1;

| flight number | scheduled departure flight | previous scheduled departure |
| ------------- | -------------------------- | ---------------------------- |
| A | 2018-06-01 10:00:00 | |
| A | 2018-06-01 19:00:00 | 2018-06-01 10:00:00 |
| B | 2018-06-01 11:00:00 | |
| B | 2018-06-01 19:00:00 | 2018-06-01 11:00:00 |
| B | 2018-06-01 21:00:00 | 2018-06-01 19:00:00 |




View on DB Fiddle



If your mysql version support LAG you can try this.



Schema (MySQL v8.0)



CREATE TABLE T(
`flight number` varchar(5),
`scheduled departure flight` datetime
);



INSERT INTO T VALUES ('A','2018-06-01 10:00');
INSERT INTO T VALUES ('A','2018-06-01 19:00');
INSERT INTO T VALUES ('B','2018-06-01 11:00');
INSERT INTO T VALUES ('B','2018-06-01 19:00');
INSERT INTO T VALUES ('B','2018-06-01 21:00');




Query #1



SELECT `flight number`,
`scheduled departure flight`,
LAG(`scheduled departure flight`) OVER(PARTITION BY `flight number` ORDER BY `scheduled departure flight`) 'previous scheduled departure'
FROM T t1;




View on DB Fiddle






share|improve this answer


























  • thankyou, i need to extract not only the date, but also all the information from previous flight , how to add another column in the result ?

    – Insan Ramadhan
    Nov 14 '18 at 8:47











  • What do your mean information? I didn't see this column.

    – D-Shih
    Nov 14 '18 at 8:50











  • actually there is another information in the table and i forgot to put it in the question

    – Insan Ramadhan
    Nov 14 '18 at 8:53











  • i just repeat the query for another information and it work, thanks

    – Insan Ramadhan
    Nov 14 '18 at 9:16














0












0








0







You are looking for LAG window function, but which support mysql version higher than 8.0



You can try to use a subquery to make a lag window function.



Schema (MySQL v5.7)



Query #1



SELECT `flight number`,
`scheduled departure flight`,
(select `scheduled departure flight`
from T tt
where t1.`scheduled departure flight` > tt.`scheduled departure flight`
AND t1.`flight number` = tt.`flight number`
order by tt.`scheduled departure flight` desc
limit 1
) 'previous scheduled departure'
FROM T t1;

| flight number | scheduled departure flight | previous scheduled departure |
| ------------- | -------------------------- | ---------------------------- |
| A | 2018-06-01 10:00:00 | |
| A | 2018-06-01 19:00:00 | 2018-06-01 10:00:00 |
| B | 2018-06-01 11:00:00 | |
| B | 2018-06-01 19:00:00 | 2018-06-01 11:00:00 |
| B | 2018-06-01 21:00:00 | 2018-06-01 19:00:00 |




View on DB Fiddle



If your mysql version support LAG you can try this.



Schema (MySQL v8.0)



CREATE TABLE T(
`flight number` varchar(5),
`scheduled departure flight` datetime
);



INSERT INTO T VALUES ('A','2018-06-01 10:00');
INSERT INTO T VALUES ('A','2018-06-01 19:00');
INSERT INTO T VALUES ('B','2018-06-01 11:00');
INSERT INTO T VALUES ('B','2018-06-01 19:00');
INSERT INTO T VALUES ('B','2018-06-01 21:00');




Query #1



SELECT `flight number`,
`scheduled departure flight`,
LAG(`scheduled departure flight`) OVER(PARTITION BY `flight number` ORDER BY `scheduled departure flight`) 'previous scheduled departure'
FROM T t1;




View on DB Fiddle






share|improve this answer















You are looking for LAG window function, but which support mysql version higher than 8.0



You can try to use a subquery to make a lag window function.



Schema (MySQL v5.7)



Query #1



SELECT `flight number`,
`scheduled departure flight`,
(select `scheduled departure flight`
from T tt
where t1.`scheduled departure flight` > tt.`scheduled departure flight`
AND t1.`flight number` = tt.`flight number`
order by tt.`scheduled departure flight` desc
limit 1
) 'previous scheduled departure'
FROM T t1;

| flight number | scheduled departure flight | previous scheduled departure |
| ------------- | -------------------------- | ---------------------------- |
| A | 2018-06-01 10:00:00 | |
| A | 2018-06-01 19:00:00 | 2018-06-01 10:00:00 |
| B | 2018-06-01 11:00:00 | |
| B | 2018-06-01 19:00:00 | 2018-06-01 11:00:00 |
| B | 2018-06-01 21:00:00 | 2018-06-01 19:00:00 |




View on DB Fiddle



If your mysql version support LAG you can try this.



Schema (MySQL v8.0)



CREATE TABLE T(
`flight number` varchar(5),
`scheduled departure flight` datetime
);



INSERT INTO T VALUES ('A','2018-06-01 10:00');
INSERT INTO T VALUES ('A','2018-06-01 19:00');
INSERT INTO T VALUES ('B','2018-06-01 11:00');
INSERT INTO T VALUES ('B','2018-06-01 19:00');
INSERT INTO T VALUES ('B','2018-06-01 21:00');




Query #1



SELECT `flight number`,
`scheduled departure flight`,
LAG(`scheduled departure flight`) OVER(PARTITION BY `flight number` ORDER BY `scheduled departure flight`) 'previous scheduled departure'
FROM T t1;




View on DB Fiddle







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 8:22

























answered Nov 14 '18 at 8:16









D-ShihD-Shih

25.8k61531




25.8k61531













  • thankyou, i need to extract not only the date, but also all the information from previous flight , how to add another column in the result ?

    – Insan Ramadhan
    Nov 14 '18 at 8:47











  • What do your mean information? I didn't see this column.

    – D-Shih
    Nov 14 '18 at 8:50











  • actually there is another information in the table and i forgot to put it in the question

    – Insan Ramadhan
    Nov 14 '18 at 8:53











  • i just repeat the query for another information and it work, thanks

    – Insan Ramadhan
    Nov 14 '18 at 9:16



















  • thankyou, i need to extract not only the date, but also all the information from previous flight , how to add another column in the result ?

    – Insan Ramadhan
    Nov 14 '18 at 8:47











  • What do your mean information? I didn't see this column.

    – D-Shih
    Nov 14 '18 at 8:50











  • actually there is another information in the table and i forgot to put it in the question

    – Insan Ramadhan
    Nov 14 '18 at 8:53











  • i just repeat the query for another information and it work, thanks

    – Insan Ramadhan
    Nov 14 '18 at 9:16

















thankyou, i need to extract not only the date, but also all the information from previous flight , how to add another column in the result ?

– Insan Ramadhan
Nov 14 '18 at 8:47





thankyou, i need to extract not only the date, but also all the information from previous flight , how to add another column in the result ?

– Insan Ramadhan
Nov 14 '18 at 8:47













What do your mean information? I didn't see this column.

– D-Shih
Nov 14 '18 at 8:50





What do your mean information? I didn't see this column.

– D-Shih
Nov 14 '18 at 8:50













actually there is another information in the table and i forgot to put it in the question

– Insan Ramadhan
Nov 14 '18 at 8:53





actually there is another information in the table and i forgot to put it in the question

– Insan Ramadhan
Nov 14 '18 at 8:53













i just repeat the query for another information and it work, thanks

– Insan Ramadhan
Nov 14 '18 at 9:16





i just repeat the query for another information and it work, thanks

– Insan Ramadhan
Nov 14 '18 at 9:16


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295640%2fmysql-left-join-return-return-only-one-row%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Bressuire

Vorschmack

Quarantine