MySQL: Get latest message from 2 tables that are associated with eachother












1














First, thank you so much for your help.



I have 2 tables: a conversation table and a message table, and a third table assoc_message__conversation that associates the messages to a conversation.



I need to get the latest message_id and message sent for each conversation specified, along with the conversation_id it is associated with.



Here is a db-fiddle: https://www.db-fiddle.com/f/kxRQeGUYYgQ7FTwi96hbLp/0



As you can see in this example, there are two conversations with conversation_id of 1 and 2, and there are three messages associated to each conversation. Messages 1, 2, and 3 are associated to conversation 1, and messages 4, 5 and 6 are associated to conversation 2.



I need to be able to specify the conversation_id's in the assoc_message__conversation table (IDs 1 and 2), and retrieve the latest message_id, message and the associated conversation_id sent from the message table for each conversation specified.



So the rows it should pull are:



conversation_id | message_id | message
------------------------------------------------
1 | 3 | "Latest message"
------------------------------------------------
2 | 6 | "Latest message"
------------------------------------------------


Thank you so much for your help!










share|improve this question




















  • 1




    There is nothing wrong with the fiddle, but your question should stand on its own two foot directly here on Stack Overflow. Currently, it does not, because you gave no table structure, or sample input/output data. Please include these for best results.
    – Tim Biegeleisen
    Nov 18 '18 at 14:23










  • Hi Tim, thank you for your comment. The fiddle does include the CREATE TABLE statement and the INSERT statement which includes the data.
    – PHPMYSQLDEV
    Nov 18 '18 at 14:34










  • That information belongs in your question, not as an external link, at least not as an external link alone.
    – Tim Biegeleisen
    Nov 18 '18 at 14:34
















1














First, thank you so much for your help.



I have 2 tables: a conversation table and a message table, and a third table assoc_message__conversation that associates the messages to a conversation.



I need to get the latest message_id and message sent for each conversation specified, along with the conversation_id it is associated with.



Here is a db-fiddle: https://www.db-fiddle.com/f/kxRQeGUYYgQ7FTwi96hbLp/0



As you can see in this example, there are two conversations with conversation_id of 1 and 2, and there are three messages associated to each conversation. Messages 1, 2, and 3 are associated to conversation 1, and messages 4, 5 and 6 are associated to conversation 2.



I need to be able to specify the conversation_id's in the assoc_message__conversation table (IDs 1 and 2), and retrieve the latest message_id, message and the associated conversation_id sent from the message table for each conversation specified.



So the rows it should pull are:



conversation_id | message_id | message
------------------------------------------------
1 | 3 | "Latest message"
------------------------------------------------
2 | 6 | "Latest message"
------------------------------------------------


Thank you so much for your help!










share|improve this question




















  • 1




    There is nothing wrong with the fiddle, but your question should stand on its own two foot directly here on Stack Overflow. Currently, it does not, because you gave no table structure, or sample input/output data. Please include these for best results.
    – Tim Biegeleisen
    Nov 18 '18 at 14:23










  • Hi Tim, thank you for your comment. The fiddle does include the CREATE TABLE statement and the INSERT statement which includes the data.
    – PHPMYSQLDEV
    Nov 18 '18 at 14:34










  • That information belongs in your question, not as an external link, at least not as an external link alone.
    – Tim Biegeleisen
    Nov 18 '18 at 14:34














1












1








1







First, thank you so much for your help.



I have 2 tables: a conversation table and a message table, and a third table assoc_message__conversation that associates the messages to a conversation.



I need to get the latest message_id and message sent for each conversation specified, along with the conversation_id it is associated with.



Here is a db-fiddle: https://www.db-fiddle.com/f/kxRQeGUYYgQ7FTwi96hbLp/0



As you can see in this example, there are two conversations with conversation_id of 1 and 2, and there are three messages associated to each conversation. Messages 1, 2, and 3 are associated to conversation 1, and messages 4, 5 and 6 are associated to conversation 2.



I need to be able to specify the conversation_id's in the assoc_message__conversation table (IDs 1 and 2), and retrieve the latest message_id, message and the associated conversation_id sent from the message table for each conversation specified.



So the rows it should pull are:



conversation_id | message_id | message
------------------------------------------------
1 | 3 | "Latest message"
------------------------------------------------
2 | 6 | "Latest message"
------------------------------------------------


Thank you so much for your help!










share|improve this question















First, thank you so much for your help.



I have 2 tables: a conversation table and a message table, and a third table assoc_message__conversation that associates the messages to a conversation.



I need to get the latest message_id and message sent for each conversation specified, along with the conversation_id it is associated with.



Here is a db-fiddle: https://www.db-fiddle.com/f/kxRQeGUYYgQ7FTwi96hbLp/0



As you can see in this example, there are two conversations with conversation_id of 1 and 2, and there are three messages associated to each conversation. Messages 1, 2, and 3 are associated to conversation 1, and messages 4, 5 and 6 are associated to conversation 2.



I need to be able to specify the conversation_id's in the assoc_message__conversation table (IDs 1 and 2), and retrieve the latest message_id, message and the associated conversation_id sent from the message table for each conversation specified.



So the rows it should pull are:



conversation_id | message_id | message
------------------------------------------------
1 | 3 | "Latest message"
------------------------------------------------
2 | 6 | "Latest message"
------------------------------------------------


Thank you so much for your help!







mysql sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 14:28









Gordon Linoff

761k35294399




761k35294399










asked Nov 18 '18 at 14:20









PHPMYSQLDEVPHPMYSQLDEV

245




245








  • 1




    There is nothing wrong with the fiddle, but your question should stand on its own two foot directly here on Stack Overflow. Currently, it does not, because you gave no table structure, or sample input/output data. Please include these for best results.
    – Tim Biegeleisen
    Nov 18 '18 at 14:23










  • Hi Tim, thank you for your comment. The fiddle does include the CREATE TABLE statement and the INSERT statement which includes the data.
    – PHPMYSQLDEV
    Nov 18 '18 at 14:34










  • That information belongs in your question, not as an external link, at least not as an external link alone.
    – Tim Biegeleisen
    Nov 18 '18 at 14:34














  • 1




    There is nothing wrong with the fiddle, but your question should stand on its own two foot directly here on Stack Overflow. Currently, it does not, because you gave no table structure, or sample input/output data. Please include these for best results.
    – Tim Biegeleisen
    Nov 18 '18 at 14:23










  • Hi Tim, thank you for your comment. The fiddle does include the CREATE TABLE statement and the INSERT statement which includes the data.
    – PHPMYSQLDEV
    Nov 18 '18 at 14:34










  • That information belongs in your question, not as an external link, at least not as an external link alone.
    – Tim Biegeleisen
    Nov 18 '18 at 14:34








1




1




There is nothing wrong with the fiddle, but your question should stand on its own two foot directly here on Stack Overflow. Currently, it does not, because you gave no table structure, or sample input/output data. Please include these for best results.
– Tim Biegeleisen
Nov 18 '18 at 14:23




There is nothing wrong with the fiddle, but your question should stand on its own two foot directly here on Stack Overflow. Currently, it does not, because you gave no table structure, or sample input/output data. Please include these for best results.
– Tim Biegeleisen
Nov 18 '18 at 14:23












Hi Tim, thank you for your comment. The fiddle does include the CREATE TABLE statement and the INSERT statement which includes the data.
– PHPMYSQLDEV
Nov 18 '18 at 14:34




Hi Tim, thank you for your comment. The fiddle does include the CREATE TABLE statement and the INSERT statement which includes the data.
– PHPMYSQLDEV
Nov 18 '18 at 14:34












That information belongs in your question, not as an external link, at least not as an external link alone.
– Tim Biegeleisen
Nov 18 '18 at 14:34




That information belongs in your question, not as an external link, at least not as an external link alone.
– Tim Biegeleisen
Nov 18 '18 at 14:34












2 Answers
2






active

oldest

votes


















1














In older versions of MySQL (< 8.0.2), we can use Derived Tables. In a Derived table, we can get the latest send_datetime value for each conversation_id. Also, it is noteworthy that you can provide your filters for conversation_id in the WHERE clause of this subquery.



We can then use this subquery`s result-set and join back to the main tables appropriately, to get the row corresponding to the latest message in a conversation.



Schema (MySQL v5.7)



View on DB Fiddle



Query #1



SELECT
amc.conversation_id,
m.message_id,
m.message
FROM
assoc_message__conversation AS amc
JOIN message AS m
ON m.message_id = amc.message_id
JOIN
(
SELECT
amc1.conversation_id,
MAX(m1.send_datetime) AS latest_send_datetime
FROM
assoc_message__conversation AS amc1
JOIN message AS m1
ON m1.message_id = amc1.message_id
WHERE amc1.conversation_id IN (1,2) -- Here you provide your input filters
GROUP BY amc1.conversation_id
) AS dt
ON dt.conversation_id = amc.conversation_id AND
dt.latest_send_datetime = m.send_datetime;


Result



| conversation_id | message_id | message        |
| --------------- | ---------- | -------------- |
| 1 | 3 | Latest message |
| 2 | 6 | Latest message |




In MySQL 8.0.2 and above, we can use Row_Number() functionality. Within a partition of conversation_id, we will determine Row Number for every message, sorted in descending order of send_datetime. In this subquery, you can provide your filters for conversation_id in the WHERE clause.



We will then use this result-set as a Derived Table, and consider only those rows, where Row Number value is 1 (as it will belong to latest send_datetime).



Schema (MySQL v8.0)



View on DB Fiddle



Query #2



SELECT 
dt.conversation_id,
dt.message_id,
dt.message
FROM
(
SELECT
amc.conversation_id,
m.message_id,
m.message,
ROW_NUMBER() OVER (PARTITION BY amc.conversation_id
ORDER BY m.send_datetime DESC) AS row_no
FROM
assoc_message__conversation AS amc
JOIN message AS m
ON m.message_id = amc.message_id
WHERE amc.conversation_id IN (1,2) -- Here you provide your input filters
) AS dt
WHERE dt.row_no = 1;


Result



| conversation_id | message_id | message        |
| --------------- | ---------- | -------------- |
| 1 | 3 | Latest message |
| 2 | 6 | Latest message |





share|improve this answer

















  • 1




    Thank you so much for your answer @Madhur Byaiya. I tested the query with 1.7M rows of messages and it ran in 0.01 seconds, where as the other answer took over 14 seconds to run!
    – PHPMYSQLDEV
    Nov 19 '18 at 12:59



















0














Assuming that amc_id increments for each new message, I would recommend a correlated subquery in the where clause:



select amc.*, m.message
from message m join
assoc_message__conversation amc
on amc.message_id = m.message_id
where amc.amc_id = (select max(amc.amc_id)
from assoc_message__conversation amc2
where amc2.conversation_id = amc.conversation_id
);


If you actually need to use send_datetime, then an additional join is necessary:



where m.send_datetime = (select max(m2.send_datetime)
from message m2 join
assoc_message__conversation amc2
on amc2.message_id = m2.message_id
where amc2.conversation_id = amc.conversation_id
)





share|improve this answer





















  • Thank you for your answer Gordon. This query seems to pull all of the messages. I need to be able to specify which conversations I want to retrieve the latest messages for. Example, in my db-fiddle there are 2 conversations, IDs 1 and 2. So i need to be able to specify conversations 1 and/or 2, and only retrieve the latest message for each conversation.
    – PHPMYSQLDEV
    Nov 18 '18 at 14:40










  • @PHPMYSQLDEV . . . Then you would have a where clause in the outer query specifying the conversations you want.
    – Gordon Linoff
    Nov 18 '18 at 14:56











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%2f53361878%2fmysql-get-latest-message-from-2-tables-that-are-associated-with-eachother%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









1














In older versions of MySQL (< 8.0.2), we can use Derived Tables. In a Derived table, we can get the latest send_datetime value for each conversation_id. Also, it is noteworthy that you can provide your filters for conversation_id in the WHERE clause of this subquery.



We can then use this subquery`s result-set and join back to the main tables appropriately, to get the row corresponding to the latest message in a conversation.



Schema (MySQL v5.7)



View on DB Fiddle



Query #1



SELECT
amc.conversation_id,
m.message_id,
m.message
FROM
assoc_message__conversation AS amc
JOIN message AS m
ON m.message_id = amc.message_id
JOIN
(
SELECT
amc1.conversation_id,
MAX(m1.send_datetime) AS latest_send_datetime
FROM
assoc_message__conversation AS amc1
JOIN message AS m1
ON m1.message_id = amc1.message_id
WHERE amc1.conversation_id IN (1,2) -- Here you provide your input filters
GROUP BY amc1.conversation_id
) AS dt
ON dt.conversation_id = amc.conversation_id AND
dt.latest_send_datetime = m.send_datetime;


Result



| conversation_id | message_id | message        |
| --------------- | ---------- | -------------- |
| 1 | 3 | Latest message |
| 2 | 6 | Latest message |




In MySQL 8.0.2 and above, we can use Row_Number() functionality. Within a partition of conversation_id, we will determine Row Number for every message, sorted in descending order of send_datetime. In this subquery, you can provide your filters for conversation_id in the WHERE clause.



We will then use this result-set as a Derived Table, and consider only those rows, where Row Number value is 1 (as it will belong to latest send_datetime).



Schema (MySQL v8.0)



View on DB Fiddle



Query #2



SELECT 
dt.conversation_id,
dt.message_id,
dt.message
FROM
(
SELECT
amc.conversation_id,
m.message_id,
m.message,
ROW_NUMBER() OVER (PARTITION BY amc.conversation_id
ORDER BY m.send_datetime DESC) AS row_no
FROM
assoc_message__conversation AS amc
JOIN message AS m
ON m.message_id = amc.message_id
WHERE amc.conversation_id IN (1,2) -- Here you provide your input filters
) AS dt
WHERE dt.row_no = 1;


Result



| conversation_id | message_id | message        |
| --------------- | ---------- | -------------- |
| 1 | 3 | Latest message |
| 2 | 6 | Latest message |





share|improve this answer

















  • 1




    Thank you so much for your answer @Madhur Byaiya. I tested the query with 1.7M rows of messages and it ran in 0.01 seconds, where as the other answer took over 14 seconds to run!
    – PHPMYSQLDEV
    Nov 19 '18 at 12:59
















1














In older versions of MySQL (< 8.0.2), we can use Derived Tables. In a Derived table, we can get the latest send_datetime value for each conversation_id. Also, it is noteworthy that you can provide your filters for conversation_id in the WHERE clause of this subquery.



We can then use this subquery`s result-set and join back to the main tables appropriately, to get the row corresponding to the latest message in a conversation.



Schema (MySQL v5.7)



View on DB Fiddle



Query #1



SELECT
amc.conversation_id,
m.message_id,
m.message
FROM
assoc_message__conversation AS amc
JOIN message AS m
ON m.message_id = amc.message_id
JOIN
(
SELECT
amc1.conversation_id,
MAX(m1.send_datetime) AS latest_send_datetime
FROM
assoc_message__conversation AS amc1
JOIN message AS m1
ON m1.message_id = amc1.message_id
WHERE amc1.conversation_id IN (1,2) -- Here you provide your input filters
GROUP BY amc1.conversation_id
) AS dt
ON dt.conversation_id = amc.conversation_id AND
dt.latest_send_datetime = m.send_datetime;


Result



| conversation_id | message_id | message        |
| --------------- | ---------- | -------------- |
| 1 | 3 | Latest message |
| 2 | 6 | Latest message |




In MySQL 8.0.2 and above, we can use Row_Number() functionality. Within a partition of conversation_id, we will determine Row Number for every message, sorted in descending order of send_datetime. In this subquery, you can provide your filters for conversation_id in the WHERE clause.



We will then use this result-set as a Derived Table, and consider only those rows, where Row Number value is 1 (as it will belong to latest send_datetime).



Schema (MySQL v8.0)



View on DB Fiddle



Query #2



SELECT 
dt.conversation_id,
dt.message_id,
dt.message
FROM
(
SELECT
amc.conversation_id,
m.message_id,
m.message,
ROW_NUMBER() OVER (PARTITION BY amc.conversation_id
ORDER BY m.send_datetime DESC) AS row_no
FROM
assoc_message__conversation AS amc
JOIN message AS m
ON m.message_id = amc.message_id
WHERE amc.conversation_id IN (1,2) -- Here you provide your input filters
) AS dt
WHERE dt.row_no = 1;


Result



| conversation_id | message_id | message        |
| --------------- | ---------- | -------------- |
| 1 | 3 | Latest message |
| 2 | 6 | Latest message |





share|improve this answer

















  • 1




    Thank you so much for your answer @Madhur Byaiya. I tested the query with 1.7M rows of messages and it ran in 0.01 seconds, where as the other answer took over 14 seconds to run!
    – PHPMYSQLDEV
    Nov 19 '18 at 12:59














1












1








1






In older versions of MySQL (< 8.0.2), we can use Derived Tables. In a Derived table, we can get the latest send_datetime value for each conversation_id. Also, it is noteworthy that you can provide your filters for conversation_id in the WHERE clause of this subquery.



We can then use this subquery`s result-set and join back to the main tables appropriately, to get the row corresponding to the latest message in a conversation.



Schema (MySQL v5.7)



View on DB Fiddle



Query #1



SELECT
amc.conversation_id,
m.message_id,
m.message
FROM
assoc_message__conversation AS amc
JOIN message AS m
ON m.message_id = amc.message_id
JOIN
(
SELECT
amc1.conversation_id,
MAX(m1.send_datetime) AS latest_send_datetime
FROM
assoc_message__conversation AS amc1
JOIN message AS m1
ON m1.message_id = amc1.message_id
WHERE amc1.conversation_id IN (1,2) -- Here you provide your input filters
GROUP BY amc1.conversation_id
) AS dt
ON dt.conversation_id = amc.conversation_id AND
dt.latest_send_datetime = m.send_datetime;


Result



| conversation_id | message_id | message        |
| --------------- | ---------- | -------------- |
| 1 | 3 | Latest message |
| 2 | 6 | Latest message |




In MySQL 8.0.2 and above, we can use Row_Number() functionality. Within a partition of conversation_id, we will determine Row Number for every message, sorted in descending order of send_datetime. In this subquery, you can provide your filters for conversation_id in the WHERE clause.



We will then use this result-set as a Derived Table, and consider only those rows, where Row Number value is 1 (as it will belong to latest send_datetime).



Schema (MySQL v8.0)



View on DB Fiddle



Query #2



SELECT 
dt.conversation_id,
dt.message_id,
dt.message
FROM
(
SELECT
amc.conversation_id,
m.message_id,
m.message,
ROW_NUMBER() OVER (PARTITION BY amc.conversation_id
ORDER BY m.send_datetime DESC) AS row_no
FROM
assoc_message__conversation AS amc
JOIN message AS m
ON m.message_id = amc.message_id
WHERE amc.conversation_id IN (1,2) -- Here you provide your input filters
) AS dt
WHERE dt.row_no = 1;


Result



| conversation_id | message_id | message        |
| --------------- | ---------- | -------------- |
| 1 | 3 | Latest message |
| 2 | 6 | Latest message |





share|improve this answer












In older versions of MySQL (< 8.0.2), we can use Derived Tables. In a Derived table, we can get the latest send_datetime value for each conversation_id. Also, it is noteworthy that you can provide your filters for conversation_id in the WHERE clause of this subquery.



We can then use this subquery`s result-set and join back to the main tables appropriately, to get the row corresponding to the latest message in a conversation.



Schema (MySQL v5.7)



View on DB Fiddle



Query #1



SELECT
amc.conversation_id,
m.message_id,
m.message
FROM
assoc_message__conversation AS amc
JOIN message AS m
ON m.message_id = amc.message_id
JOIN
(
SELECT
amc1.conversation_id,
MAX(m1.send_datetime) AS latest_send_datetime
FROM
assoc_message__conversation AS amc1
JOIN message AS m1
ON m1.message_id = amc1.message_id
WHERE amc1.conversation_id IN (1,2) -- Here you provide your input filters
GROUP BY amc1.conversation_id
) AS dt
ON dt.conversation_id = amc.conversation_id AND
dt.latest_send_datetime = m.send_datetime;


Result



| conversation_id | message_id | message        |
| --------------- | ---------- | -------------- |
| 1 | 3 | Latest message |
| 2 | 6 | Latest message |




In MySQL 8.0.2 and above, we can use Row_Number() functionality. Within a partition of conversation_id, we will determine Row Number for every message, sorted in descending order of send_datetime. In this subquery, you can provide your filters for conversation_id in the WHERE clause.



We will then use this result-set as a Derived Table, and consider only those rows, where Row Number value is 1 (as it will belong to latest send_datetime).



Schema (MySQL v8.0)



View on DB Fiddle



Query #2



SELECT 
dt.conversation_id,
dt.message_id,
dt.message
FROM
(
SELECT
amc.conversation_id,
m.message_id,
m.message,
ROW_NUMBER() OVER (PARTITION BY amc.conversation_id
ORDER BY m.send_datetime DESC) AS row_no
FROM
assoc_message__conversation AS amc
JOIN message AS m
ON m.message_id = amc.message_id
WHERE amc.conversation_id IN (1,2) -- Here you provide your input filters
) AS dt
WHERE dt.row_no = 1;


Result



| conversation_id | message_id | message        |
| --------------- | ---------- | -------------- |
| 1 | 3 | Latest message |
| 2 | 6 | Latest message |






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 18 '18 at 15:35









Madhur BhaiyaMadhur Bhaiya

19.5k62236




19.5k62236








  • 1




    Thank you so much for your answer @Madhur Byaiya. I tested the query with 1.7M rows of messages and it ran in 0.01 seconds, where as the other answer took over 14 seconds to run!
    – PHPMYSQLDEV
    Nov 19 '18 at 12:59














  • 1




    Thank you so much for your answer @Madhur Byaiya. I tested the query with 1.7M rows of messages and it ran in 0.01 seconds, where as the other answer took over 14 seconds to run!
    – PHPMYSQLDEV
    Nov 19 '18 at 12:59








1




1




Thank you so much for your answer @Madhur Byaiya. I tested the query with 1.7M rows of messages and it ran in 0.01 seconds, where as the other answer took over 14 seconds to run!
– PHPMYSQLDEV
Nov 19 '18 at 12:59




Thank you so much for your answer @Madhur Byaiya. I tested the query with 1.7M rows of messages and it ran in 0.01 seconds, where as the other answer took over 14 seconds to run!
– PHPMYSQLDEV
Nov 19 '18 at 12:59













0














Assuming that amc_id increments for each new message, I would recommend a correlated subquery in the where clause:



select amc.*, m.message
from message m join
assoc_message__conversation amc
on amc.message_id = m.message_id
where amc.amc_id = (select max(amc.amc_id)
from assoc_message__conversation amc2
where amc2.conversation_id = amc.conversation_id
);


If you actually need to use send_datetime, then an additional join is necessary:



where m.send_datetime = (select max(m2.send_datetime)
from message m2 join
assoc_message__conversation amc2
on amc2.message_id = m2.message_id
where amc2.conversation_id = amc.conversation_id
)





share|improve this answer





















  • Thank you for your answer Gordon. This query seems to pull all of the messages. I need to be able to specify which conversations I want to retrieve the latest messages for. Example, in my db-fiddle there are 2 conversations, IDs 1 and 2. So i need to be able to specify conversations 1 and/or 2, and only retrieve the latest message for each conversation.
    – PHPMYSQLDEV
    Nov 18 '18 at 14:40










  • @PHPMYSQLDEV . . . Then you would have a where clause in the outer query specifying the conversations you want.
    – Gordon Linoff
    Nov 18 '18 at 14:56
















0














Assuming that amc_id increments for each new message, I would recommend a correlated subquery in the where clause:



select amc.*, m.message
from message m join
assoc_message__conversation amc
on amc.message_id = m.message_id
where amc.amc_id = (select max(amc.amc_id)
from assoc_message__conversation amc2
where amc2.conversation_id = amc.conversation_id
);


If you actually need to use send_datetime, then an additional join is necessary:



where m.send_datetime = (select max(m2.send_datetime)
from message m2 join
assoc_message__conversation amc2
on amc2.message_id = m2.message_id
where amc2.conversation_id = amc.conversation_id
)





share|improve this answer





















  • Thank you for your answer Gordon. This query seems to pull all of the messages. I need to be able to specify which conversations I want to retrieve the latest messages for. Example, in my db-fiddle there are 2 conversations, IDs 1 and 2. So i need to be able to specify conversations 1 and/or 2, and only retrieve the latest message for each conversation.
    – PHPMYSQLDEV
    Nov 18 '18 at 14:40










  • @PHPMYSQLDEV . . . Then you would have a where clause in the outer query specifying the conversations you want.
    – Gordon Linoff
    Nov 18 '18 at 14:56














0












0








0






Assuming that amc_id increments for each new message, I would recommend a correlated subquery in the where clause:



select amc.*, m.message
from message m join
assoc_message__conversation amc
on amc.message_id = m.message_id
where amc.amc_id = (select max(amc.amc_id)
from assoc_message__conversation amc2
where amc2.conversation_id = amc.conversation_id
);


If you actually need to use send_datetime, then an additional join is necessary:



where m.send_datetime = (select max(m2.send_datetime)
from message m2 join
assoc_message__conversation amc2
on amc2.message_id = m2.message_id
where amc2.conversation_id = amc.conversation_id
)





share|improve this answer












Assuming that amc_id increments for each new message, I would recommend a correlated subquery in the where clause:



select amc.*, m.message
from message m join
assoc_message__conversation amc
on amc.message_id = m.message_id
where amc.amc_id = (select max(amc.amc_id)
from assoc_message__conversation amc2
where amc2.conversation_id = amc.conversation_id
);


If you actually need to use send_datetime, then an additional join is necessary:



where m.send_datetime = (select max(m2.send_datetime)
from message m2 join
assoc_message__conversation amc2
on amc2.message_id = m2.message_id
where amc2.conversation_id = amc.conversation_id
)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 18 '18 at 14:27









Gordon LinoffGordon Linoff

761k35294399




761k35294399












  • Thank you for your answer Gordon. This query seems to pull all of the messages. I need to be able to specify which conversations I want to retrieve the latest messages for. Example, in my db-fiddle there are 2 conversations, IDs 1 and 2. So i need to be able to specify conversations 1 and/or 2, and only retrieve the latest message for each conversation.
    – PHPMYSQLDEV
    Nov 18 '18 at 14:40










  • @PHPMYSQLDEV . . . Then you would have a where clause in the outer query specifying the conversations you want.
    – Gordon Linoff
    Nov 18 '18 at 14:56


















  • Thank you for your answer Gordon. This query seems to pull all of the messages. I need to be able to specify which conversations I want to retrieve the latest messages for. Example, in my db-fiddle there are 2 conversations, IDs 1 and 2. So i need to be able to specify conversations 1 and/or 2, and only retrieve the latest message for each conversation.
    – PHPMYSQLDEV
    Nov 18 '18 at 14:40










  • @PHPMYSQLDEV . . . Then you would have a where clause in the outer query specifying the conversations you want.
    – Gordon Linoff
    Nov 18 '18 at 14:56
















Thank you for your answer Gordon. This query seems to pull all of the messages. I need to be able to specify which conversations I want to retrieve the latest messages for. Example, in my db-fiddle there are 2 conversations, IDs 1 and 2. So i need to be able to specify conversations 1 and/or 2, and only retrieve the latest message for each conversation.
– PHPMYSQLDEV
Nov 18 '18 at 14:40




Thank you for your answer Gordon. This query seems to pull all of the messages. I need to be able to specify which conversations I want to retrieve the latest messages for. Example, in my db-fiddle there are 2 conversations, IDs 1 and 2. So i need to be able to specify conversations 1 and/or 2, and only retrieve the latest message for each conversation.
– PHPMYSQLDEV
Nov 18 '18 at 14:40












@PHPMYSQLDEV . . . Then you would have a where clause in the outer query specifying the conversations you want.
– Gordon Linoff
Nov 18 '18 at 14:56




@PHPMYSQLDEV . . . Then you would have a where clause in the outer query specifying the conversations you want.
– Gordon Linoff
Nov 18 '18 at 14:56


















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.





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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53361878%2fmysql-get-latest-message-from-2-tables-that-are-associated-with-eachother%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

mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?