mysql - Selecting latest conversations from table containing private messages -


i have table containing personal messages 1 user another.

here table structure:

mysql> describe pms; +---------+--------------+------+-----+---------+----------------+ | field   | type         | null | key | default |          | +---------+--------------+------+-----+---------+----------------+ | id      | int(11)      | no   | pri | null    | auto_increment | | time    | datetime     | no   |     | null    |                | |    | int(11)      | no   |     | null    |                | | from_ip | int(11)      | no   |     | null    |                | |      | int(11)      | no   |     | null    |                | | message | varchar(255) | no   |     | null    |                | | read    | tinyint(4)   | no   |     | 0       |                | +---------+--------------+------+-----+---------+----------------+ 

i creating view shows 10 latest conversations of particular user id. want find conversations, thought of using group from, to. this, however, returned duplicate rows (both user , user), , noticed ordering not work should.

in order able order results , select 10 latest conversations, groups should contain latest row of group instead of first.

here query tried:

select *   `pms`   `from` = 1 or  `to` = 1 group  `from` ,  `to`  order  `id` desc  limit 10 

which gives wrong row group, , therefore ordering id (or time) gives wrong order.

any ideas how working?

this assumes conversation defined from-to pair in either order, , latest conversation has largest id:

select least(`from`,  `to`), greatest(`from`, `to`)  `pms`   `from` = 1 or  `to` = 1 group  least(`from`,  `to`), greatest(`from`, `to`) order max( `id`) desc  limit 10 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -