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
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
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.
add a comment |
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
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
add a comment |
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
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
mysql group-by
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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 |
+--------+-----------+---------+-----+
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
add a comment |
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
});
}
});
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%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
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 |
+--------+-----------+---------+-----+
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
add a comment |
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 |
+--------+-----------+---------+-----+
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
add a comment |
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 |
+--------+-----------+---------+-----+
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 |
+--------+-----------+---------+-----+
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
add a comment |
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
add a comment |
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.
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%2fdba.stackexchange.com%2fquestions%2f156655%2fmysql-group-by-multiple-combinations%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
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