Lets say my database looks like this:
Table: messages
+----+------+----+----------+----------+
| id | from | to | double_1 | double_2 |
+----+------+----+----------+----------+
| 1 | 1 | 2 | 0 | 0 |
| 2 | 1 | 0 | 1 | 2 |
| 3 | 2 | 0 | 2 | 1 |
| 4 | 2 | 1 | 0 | 0 |
| 5 | 2 | 3 | 0 | 0 |
| 6 | 2 | 0 | 1 | 3 |
+----+------+----+----------+----------+
Then in my selection from the table, I want to GROUP BY the following:
- group rows where
double_1anddouble_2is the same, whileto= 0 - group rows where
fromis the same, whileto= $id anddouble_1= 0 anddouble_2= 0 - group rows where
tois the same, whilefrom= $id anddouble_1= 0 anddouble_2= 0
Anyone have any ideas how to do that?
EDIT: As it seems that i did make some people misunderstanding this. I will now explain better.
As I say the ways that I would like to GROUP BY, then when saying group rows where double_1 and double_2 is the same..., then its grouping the rows where double_1 and double_2 are the same. Like I have those two rows:
+----+------+----+----------+----------+
| id | from | to | double_1 | double_2 |
+----+------+----+----------+----------+
| 1 | 1 | 0 | 1 | 1 |
| 2 | 1 | 0 | 2 | 2 |
+----+------+----+----------+----------+
They should not be grouped, as ID 1's double_1 AND double_2 is not the same as ID 2's double_1 AND double_2. While this would group:
+----+------+----+----------+----------+
| id | from | to | double_1 | double_2 |
+----+------+----+----------+----------+
| 1 | 1 | 0 | 1 | 2 |
| 2 | 1 | 0 | 1 | 2 |
+----+------+----+----------+----------+
Im very sorry for the misunderstanding, hope that someone here are still willing to help me out.
`You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'to, double_1 , double_2 , case when double_1 = double_2 ' at line 4
CREATE OR REPLACE VIEW vw_messages AS SELECT id , 'from' , to, double_1 , double_2 , case when double_1 = double_2 and to = 0 end flag_1 , case when double_1 = 0 and double_2 = 0 and to = 38 end flag_2 , case when double_1 = 0 and double_2 = 0 and 'from' = 38 end flag_3 FROM messages`
– Mark Topper Apr 24 '13 at 11:50CASEclause, it seems useful. But sorry for the misunderstanding, as I actually wanted to do something else. I tried to explain it better in the question by editing it. Sorry and thanks a lot for your time. – Mark Topper Apr 24 '13 at 20:31