Note that there are some explanatory texts on larger screens.

plurals
  1. POGroup by an advanced way
    primarykey
    data
    text
    <p>I ran into a bit of a problem when trying to do a query to group messages into conversations. So that messages with same sender and receiver will be in the same conversation. As well there will be group conversations, which means two and two persons talk together as four.</p> <p>My database table looks like this:</p> <p>Table: <code>messages</code></p> <pre><code>+----+------+----+---------+---------+ | id | from | to | group_1 | group_2 | +----+------+----+---------+---------+ | 1 | 1 | 2 | 0 | 0 | | 2 | 2 | 1 | 0 | 0 | | 3 | 1 | 0 | 1 | 2 | | 4 | 2 | 0 | 2 | 1 | | 5 | 3 | 0 | 1 | 2 | | 6 | 4 | 0 | 2 | 1 | | 7 | 4 | 1 | 0 | 0 | | 8 | 1 | 4 | 0 | 0 | +----+------+----+---------+---------+ </code></pre> <p>I have tried with the following, but then I get all the messages out, so I need to make a group by, to make them stick into one row instead of having multiple rows for same conversation.</p> <p>SELECT * FROM <code>messages</code></p> <p>So basically what is needed to be done, is to group by the following:<br> - Group rows conversations where <code>from</code> and <code>to</code> is equals with other rows, while <code>group_1</code> and <code>group_2</code> = 0. But also it should group those rows where <code>to</code> is equal with other rows <code>from</code>, and the other way around, but still while <code>group_1</code> and <code>group_2</code> = 0. (Like the 2 first rows in the table above)<br> - Group rows conversations where <code>group_1</code> and <code>group_2</code> is equals with other rows, while <code>to</code> = 0, and as well where <code>group_1</code> is equal with other rows <code>group_2</code>, and the other way around, but still while <code>to</code> = 0. (Like the rows with id 3 and 4)</p> <p>So basically it should group all messages where groups are talking with each other, and where users are talking with each other.</p> <p>EDIT</p> <h1>Better explaining of <code>group_1</code> and <code>group_2</code></h1> <p><code>group_1</code> and <code>group_2</code> is made so that I can put a user inside a group, and make a group able to talk with another group.</p> <p>So instead of using <code>to</code> and <code>from</code>, then they use <code>group_1</code> and <code>group_2</code>. It should GROUP the group conversations just like the user conversations (where <code>from</code> and <code>to</code> are defined).</p> <p>You can see that a row is a group, when <code>to = 0</code>. When <code>to != 0</code>, then it is not a group.</p> <p>It should GROUP <code>group_1</code> and <code>group_2</code>, just like users.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload