Mysql group by multiple combinations The Next CEO of Stack OverflowShould I use WHERE NOT...

A question about free fall, velocity, and the height of an object.

What was Carter Burke's job for "the company" in Aliens?

Easy to read palindrome checker

Defamation due to breach of confidentiality

Why doesn't UK go for the same deal Japan has with EU to resolve Brexit?

Is it convenient to ask the journal's editor for two additional days to complete a review?

What difference does it make using sed with/without whitespaces?

What flight has the highest ratio of timezone difference to flight time?

Expressing the idea of having a very busy time

Is there an equivalent of cd - for cp or mv

Would a grinding machine be a simple and workable propulsion system for an interplanetary spacecraft?

What does "shotgun unity" refer to here in this sentence?

Does destroying a Lich's phylactery destroy the soul within it?

What happened in Rome, when the western empire "fell"?

Is it professional to write unrelated content in an almost-empty email?

Why is the US ranked as #45 in Press Freedom ratings, despite its extremely permissive free speech laws?

free fall ellipse or parabola?

Can you teleport closer to a creature you are Frightened of?

Can Sneak Attack be used when hitting with an improvised weapon?

Spaces in which all closed sets are regular closed

Small nick on power cord from an electric alarm clock, and copper wiring exposed but intact

Inexact numbers as keys in Association?

Touchpad not working on Debian 9

Is a distribution that is normal, but highly skewed, considered Gaussian?



Mysql group by multiple combinations



The Next CEO of Stack OverflowShould I use WHERE NOT EXISTS or LEFT JOIN for getting the ids of relationship table?MySQL, “columns partitioning” when multiple columns are individiually importantMysql pagination without offsetMySQL JOIN query produces wrong resultWhy did adding a secondary index not increase select performance?MySql counting results with left joins doubles the resultsTable structure and query for messaging between usersHow to show count of records from one table to another table?Select unique value pairs based on two participants that occur more than once inside a tableMysql query to fetch records which are not matched form both tables












0















I'm building a chat app in which two people will comunicate on enquiry, I want to fetch conversation between two users on enquiry, table structure is like below:



Table: enquiries



id, name, product, msg


Table: replies



id, enquiry_id(fk), from_user(fk), to_user(fk), msg

+----+--------+-----------+---------+------+
| id | enq_id | from_user | to_user | msg |
+----+--------+-----------+---------+------+
| 1 | 1 | 1 | 2 | msg1 |
+----+--------+-----------+---------+------+
| 2 | 1 | 2 | 1 | msg2 |
+----+--------+-----------+---------+------+
| 3 | 1 | 1 | 2 | msg3 |
+----+--------+-----------+---------+------+
| 4 | 1 | 2 | 1 | msg4 |
+----+--------+-----------+---------+------+
| 5 | 1 | 1 | 3 | msg1 |
+----+--------+-----------+---------+------+
| 6 | 1 | 3 | 1 | msg2 |
+----+--------+-----------+---------+------+


What I'm doing



I'm using using below query to fetch communication between two users on the basis of enquiries but it is giving two seperate records for same users & enquiry.



SELECT 
r.enquiry_id,
r.from_user,
r.to_user,
count(1) replies_count
FROM
`replies` r
GROUP BY
r.enquiry_id, r.from_user, r.to_user
ORDER BY
r.id DESC


This is the result of the above query



    +--------+-----------+---------+-----+
| enq_id | from_user | to_user | msg |
+--------+-----------+---------+-----+
| 1 | 1 | 2 | 2 |
+--------+-----------+---------+-----+
| 1 | 2 | 1 | 2 |
+--------+-----------+---------+-----+
| 1 | 1 | 3 | 1 |
+--------+-----------+---------+-----+
| 1 | 3 | 1 | 1 |
+--------+-----------+---------+-----+


And my expected result is like



+--------+--------------+--------------+-----+
| enq_id | participant1 | participant2 | msg |
+--------+--------------+--------------+-----+
| 1 | 1 | 2 | 4 |
+--------+--------------+--------------+-----+
| 1 | 1 | 3 | 2 |
+--------+--------------+--------------+-----+









share|improve this question
















bumped to the homepage by Community 12 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 1





    can be more than two distinct users in the conversation?

    – McNets
    Nov 29 '16 at 13:50











  • There will be single row for two users communicating on same enquiry, I've modified my question please check

    – Ramesh Mhetre
    Nov 30 '16 at 5:01
















0















I'm building a chat app in which two people will comunicate on enquiry, I want to fetch conversation between two users on enquiry, table structure is like below:



Table: enquiries



id, name, product, msg


Table: replies



id, enquiry_id(fk), from_user(fk), to_user(fk), msg

+----+--------+-----------+---------+------+
| id | enq_id | from_user | to_user | msg |
+----+--------+-----------+---------+------+
| 1 | 1 | 1 | 2 | msg1 |
+----+--------+-----------+---------+------+
| 2 | 1 | 2 | 1 | msg2 |
+----+--------+-----------+---------+------+
| 3 | 1 | 1 | 2 | msg3 |
+----+--------+-----------+---------+------+
| 4 | 1 | 2 | 1 | msg4 |
+----+--------+-----------+---------+------+
| 5 | 1 | 1 | 3 | msg1 |
+----+--------+-----------+---------+------+
| 6 | 1 | 3 | 1 | msg2 |
+----+--------+-----------+---------+------+


What I'm doing



I'm using using below query to fetch communication between two users on the basis of enquiries but it is giving two seperate records for same users & enquiry.



SELECT 
r.enquiry_id,
r.from_user,
r.to_user,
count(1) replies_count
FROM
`replies` r
GROUP BY
r.enquiry_id, r.from_user, r.to_user
ORDER BY
r.id DESC


This is the result of the above query



    +--------+-----------+---------+-----+
| enq_id | from_user | to_user | msg |
+--------+-----------+---------+-----+
| 1 | 1 | 2 | 2 |
+--------+-----------+---------+-----+
| 1 | 2 | 1 | 2 |
+--------+-----------+---------+-----+
| 1 | 1 | 3 | 1 |
+--------+-----------+---------+-----+
| 1 | 3 | 1 | 1 |
+--------+-----------+---------+-----+


And my expected result is like



+--------+--------------+--------------+-----+
| enq_id | participant1 | participant2 | msg |
+--------+--------------+--------------+-----+
| 1 | 1 | 2 | 4 |
+--------+--------------+--------------+-----+
| 1 | 1 | 3 | 2 |
+--------+--------------+--------------+-----+









share|improve this question
















bumped to the homepage by Community 12 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 1





    can be more than two distinct users in the conversation?

    – McNets
    Nov 29 '16 at 13:50











  • There will be single row for two users communicating on same enquiry, I've modified my question please check

    – Ramesh Mhetre
    Nov 30 '16 at 5:01














0












0








0








I'm building a chat app in which two people will comunicate on enquiry, I want to fetch conversation between two users on enquiry, table structure is like below:



Table: enquiries



id, name, product, msg


Table: replies



id, enquiry_id(fk), from_user(fk), to_user(fk), msg

+----+--------+-----------+---------+------+
| id | enq_id | from_user | to_user | msg |
+----+--------+-----------+---------+------+
| 1 | 1 | 1 | 2 | msg1 |
+----+--------+-----------+---------+------+
| 2 | 1 | 2 | 1 | msg2 |
+----+--------+-----------+---------+------+
| 3 | 1 | 1 | 2 | msg3 |
+----+--------+-----------+---------+------+
| 4 | 1 | 2 | 1 | msg4 |
+----+--------+-----------+---------+------+
| 5 | 1 | 1 | 3 | msg1 |
+----+--------+-----------+---------+------+
| 6 | 1 | 3 | 1 | msg2 |
+----+--------+-----------+---------+------+


What I'm doing



I'm using using below query to fetch communication between two users on the basis of enquiries but it is giving two seperate records for same users & enquiry.



SELECT 
r.enquiry_id,
r.from_user,
r.to_user,
count(1) replies_count
FROM
`replies` r
GROUP BY
r.enquiry_id, r.from_user, r.to_user
ORDER BY
r.id DESC


This is the result of the above query



    +--------+-----------+---------+-----+
| enq_id | from_user | to_user | msg |
+--------+-----------+---------+-----+
| 1 | 1 | 2 | 2 |
+--------+-----------+---------+-----+
| 1 | 2 | 1 | 2 |
+--------+-----------+---------+-----+
| 1 | 1 | 3 | 1 |
+--------+-----------+---------+-----+
| 1 | 3 | 1 | 1 |
+--------+-----------+---------+-----+


And my expected result is like



+--------+--------------+--------------+-----+
| enq_id | participant1 | participant2 | msg |
+--------+--------------+--------------+-----+
| 1 | 1 | 2 | 4 |
+--------+--------------+--------------+-----+
| 1 | 1 | 3 | 2 |
+--------+--------------+--------------+-----+









share|improve this question
















I'm building a chat app in which two people will comunicate on enquiry, I want to fetch conversation between two users on enquiry, table structure is like below:



Table: enquiries



id, name, product, msg


Table: replies



id, enquiry_id(fk), from_user(fk), to_user(fk), msg

+----+--------+-----------+---------+------+
| id | enq_id | from_user | to_user | msg |
+----+--------+-----------+---------+------+
| 1 | 1 | 1 | 2 | msg1 |
+----+--------+-----------+---------+------+
| 2 | 1 | 2 | 1 | msg2 |
+----+--------+-----------+---------+------+
| 3 | 1 | 1 | 2 | msg3 |
+----+--------+-----------+---------+------+
| 4 | 1 | 2 | 1 | msg4 |
+----+--------+-----------+---------+------+
| 5 | 1 | 1 | 3 | msg1 |
+----+--------+-----------+---------+------+
| 6 | 1 | 3 | 1 | msg2 |
+----+--------+-----------+---------+------+


What I'm doing



I'm using using below query to fetch communication between two users on the basis of enquiries but it is giving two seperate records for same users & enquiry.



SELECT 
r.enquiry_id,
r.from_user,
r.to_user,
count(1) replies_count
FROM
`replies` r
GROUP BY
r.enquiry_id, r.from_user, r.to_user
ORDER BY
r.id DESC


This is the result of the above query



    +--------+-----------+---------+-----+
| enq_id | from_user | to_user | msg |
+--------+-----------+---------+-----+
| 1 | 1 | 2 | 2 |
+--------+-----------+---------+-----+
| 1 | 2 | 1 | 2 |
+--------+-----------+---------+-----+
| 1 | 1 | 3 | 1 |
+--------+-----------+---------+-----+
| 1 | 3 | 1 | 1 |
+--------+-----------+---------+-----+


And my expected result is like



+--------+--------------+--------------+-----+
| enq_id | participant1 | participant2 | msg |
+--------+--------------+--------------+-----+
| 1 | 1 | 2 | 4 |
+--------+--------------+--------------+-----+
| 1 | 1 | 3 | 2 |
+--------+--------------+--------------+-----+






mysql group-by






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 30 '16 at 4:56







Ramesh Mhetre

















asked Nov 29 '16 at 13:06









Ramesh MhetreRamesh Mhetre

1012




1012





bumped to the homepage by Community 12 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 12 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.










  • 1





    can be more than two distinct users in the conversation?

    – McNets
    Nov 29 '16 at 13:50











  • There will be single row for two users communicating on same enquiry, I've modified my question please check

    – Ramesh Mhetre
    Nov 30 '16 at 5:01














  • 1





    can be more than two distinct users in the conversation?

    – McNets
    Nov 29 '16 at 13:50











  • There will be single row for two users communicating on same enquiry, I've modified my question please check

    – Ramesh Mhetre
    Nov 30 '16 at 5:01








1




1





can be more than two distinct users in the conversation?

– McNets
Nov 29 '16 at 13:50





can be more than two distinct users in the conversation?

– McNets
Nov 29 '16 at 13:50













There will be single row for two users communicating on same enquiry, I've modified my question please check

– Ramesh Mhetre
Nov 30 '16 at 5:01





There will be single row for two users communicating on same enquiry, I've modified my question please check

– Ramesh Mhetre
Nov 30 '16 at 5:01










1 Answer
1






active

oldest

votes


















0














check it here if it is what you're asking for: http://rextester.com/TVOKS20933



select enquiry_id, from_user, to_user, count(1) msg 
from replies
group by enquiry_id, from_user, to_user
order by id;

+----+--------+-----------+---------+------+
| id | enq_id | from_user | to_user | msg |
+----+--------+-----------+---------+------+
| 1 | 1 | 1 | 2 | msg1 |
+----+--------+-----------+---------+------+
| 2 | 1 | 2 | 1 | msg2 |
+----+--------+-----------+---------+------+
| 3 | 1 | 1 | 2 | msg3 |
+----+--------+-----------+---------+------+
| 4 | 1 | 2 | 1 | msg4 |
+----+--------+-----------+---------+------+
| 5 | 1 | 1 | 3 | msg1 |
+----+--------+-----------+---------+------+
| 6 | 1 | 3 | 1 | msg2 |
+----+--------+-----------+---------+------+

+--------+-----------+---------+-----+
| enq_id | from_user | to_user | msg |
+--------+-----------+---------+-----+
| 1 | 1 | 2 | 2 |
+--------+-----------+---------+-----+
| 1 | 2 | 1 | 2 |
+--------+-----------+---------+-----+
| 1 | 1 | 3 | 1 |
+--------+-----------+---------+-----+
| 1 | 3 | 1 | 1 |
+--------+-----------+---------+-----+





share|improve this answer


























  • This what is I'm doing right now, I want only one row for communication between two users. I have modified my question, please check

    – Ramesh Mhetre
    Nov 30 '16 at 5:00












Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fdba.stackexchange.com%2fquestions%2f156655%2fmysql-group-by-multiple-combinations%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














check it here if it is what you're asking for: http://rextester.com/TVOKS20933



select enquiry_id, from_user, to_user, count(1) msg 
from replies
group by enquiry_id, from_user, to_user
order by id;

+----+--------+-----------+---------+------+
| id | enq_id | from_user | to_user | msg |
+----+--------+-----------+---------+------+
| 1 | 1 | 1 | 2 | msg1 |
+----+--------+-----------+---------+------+
| 2 | 1 | 2 | 1 | msg2 |
+----+--------+-----------+---------+------+
| 3 | 1 | 1 | 2 | msg3 |
+----+--------+-----------+---------+------+
| 4 | 1 | 2 | 1 | msg4 |
+----+--------+-----------+---------+------+
| 5 | 1 | 1 | 3 | msg1 |
+----+--------+-----------+---------+------+
| 6 | 1 | 3 | 1 | msg2 |
+----+--------+-----------+---------+------+

+--------+-----------+---------+-----+
| enq_id | from_user | to_user | msg |
+--------+-----------+---------+-----+
| 1 | 1 | 2 | 2 |
+--------+-----------+---------+-----+
| 1 | 2 | 1 | 2 |
+--------+-----------+---------+-----+
| 1 | 1 | 3 | 1 |
+--------+-----------+---------+-----+
| 1 | 3 | 1 | 1 |
+--------+-----------+---------+-----+





share|improve this answer


























  • This what is I'm doing right now, I want only one row for communication between two users. I have modified my question, please check

    – Ramesh Mhetre
    Nov 30 '16 at 5:00
















0














check it here if it is what you're asking for: http://rextester.com/TVOKS20933



select enquiry_id, from_user, to_user, count(1) msg 
from replies
group by enquiry_id, from_user, to_user
order by id;

+----+--------+-----------+---------+------+
| id | enq_id | from_user | to_user | msg |
+----+--------+-----------+---------+------+
| 1 | 1 | 1 | 2 | msg1 |
+----+--------+-----------+---------+------+
| 2 | 1 | 2 | 1 | msg2 |
+----+--------+-----------+---------+------+
| 3 | 1 | 1 | 2 | msg3 |
+----+--------+-----------+---------+------+
| 4 | 1 | 2 | 1 | msg4 |
+----+--------+-----------+---------+------+
| 5 | 1 | 1 | 3 | msg1 |
+----+--------+-----------+---------+------+
| 6 | 1 | 3 | 1 | msg2 |
+----+--------+-----------+---------+------+

+--------+-----------+---------+-----+
| enq_id | from_user | to_user | msg |
+--------+-----------+---------+-----+
| 1 | 1 | 2 | 2 |
+--------+-----------+---------+-----+
| 1 | 2 | 1 | 2 |
+--------+-----------+---------+-----+
| 1 | 1 | 3 | 1 |
+--------+-----------+---------+-----+
| 1 | 3 | 1 | 1 |
+--------+-----------+---------+-----+





share|improve this answer


























  • This what is I'm doing right now, I want only one row for communication between two users. I have modified my question, please check

    – Ramesh Mhetre
    Nov 30 '16 at 5:00














0












0








0







check it here if it is what you're asking for: http://rextester.com/TVOKS20933



select enquiry_id, from_user, to_user, count(1) msg 
from replies
group by enquiry_id, from_user, to_user
order by id;

+----+--------+-----------+---------+------+
| id | enq_id | from_user | to_user | msg |
+----+--------+-----------+---------+------+
| 1 | 1 | 1 | 2 | msg1 |
+----+--------+-----------+---------+------+
| 2 | 1 | 2 | 1 | msg2 |
+----+--------+-----------+---------+------+
| 3 | 1 | 1 | 2 | msg3 |
+----+--------+-----------+---------+------+
| 4 | 1 | 2 | 1 | msg4 |
+----+--------+-----------+---------+------+
| 5 | 1 | 1 | 3 | msg1 |
+----+--------+-----------+---------+------+
| 6 | 1 | 3 | 1 | msg2 |
+----+--------+-----------+---------+------+

+--------+-----------+---------+-----+
| enq_id | from_user | to_user | msg |
+--------+-----------+---------+-----+
| 1 | 1 | 2 | 2 |
+--------+-----------+---------+-----+
| 1 | 2 | 1 | 2 |
+--------+-----------+---------+-----+
| 1 | 1 | 3 | 1 |
+--------+-----------+---------+-----+
| 1 | 3 | 1 | 1 |
+--------+-----------+---------+-----+





share|improve this answer















check it here if it is what you're asking for: http://rextester.com/TVOKS20933



select enquiry_id, from_user, to_user, count(1) msg 
from replies
group by enquiry_id, from_user, to_user
order by id;

+----+--------+-----------+---------+------+
| id | enq_id | from_user | to_user | msg |
+----+--------+-----------+---------+------+
| 1 | 1 | 1 | 2 | msg1 |
+----+--------+-----------+---------+------+
| 2 | 1 | 2 | 1 | msg2 |
+----+--------+-----------+---------+------+
| 3 | 1 | 1 | 2 | msg3 |
+----+--------+-----------+---------+------+
| 4 | 1 | 2 | 1 | msg4 |
+----+--------+-----------+---------+------+
| 5 | 1 | 1 | 3 | msg1 |
+----+--------+-----------+---------+------+
| 6 | 1 | 3 | 1 | msg2 |
+----+--------+-----------+---------+------+

+--------+-----------+---------+-----+
| enq_id | from_user | to_user | msg |
+--------+-----------+---------+-----+
| 1 | 1 | 2 | 2 |
+--------+-----------+---------+-----+
| 1 | 2 | 1 | 2 |
+--------+-----------+---------+-----+
| 1 | 1 | 3 | 1 |
+--------+-----------+---------+-----+
| 1 | 3 | 1 | 1 |
+--------+-----------+---------+-----+






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 29 '16 at 14:22

























answered Nov 29 '16 at 14:14









McNetsMcNets

16.3k42161




16.3k42161













  • This what is I'm doing right now, I want only one row for communication between two users. I have modified my question, please check

    – Ramesh Mhetre
    Nov 30 '16 at 5:00



















  • This what is I'm doing right now, I want only one row for communication between two users. I have modified my question, please check

    – Ramesh Mhetre
    Nov 30 '16 at 5:00

















This what is I'm doing right now, I want only one row for communication between two users. I have modified my question, please check

– Ramesh Mhetre
Nov 30 '16 at 5:00





This what is I'm doing right now, I want only one row for communication between two users. I have modified my question, please check

– Ramesh Mhetre
Nov 30 '16 at 5:00


















draft saved

draft discarded




















































Thanks for contributing an answer to Database Administrators Stack Exchange!


  • 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%2fdba.stackexchange.com%2fquestions%2f156655%2fmysql-group-by-multiple-combinations%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

ORA-01691 (unable to extend lob segment) even though my tablespace has AUTOEXTEND onORA-01692: unable to...

Always On Availability groups resolving state after failover - Remote harden of transaction...

Circunscripción electoral de Guipúzcoa Referencias Menú de navegaciónLas claves del sistema electoral en...