简体   繁体   中英

group_by and order_by with codeigniter

I'm trying to figure this out, with no luck at all. I keep getting some weird results! First, I've got a MySQL database with some rows, the structure looks like this,

ID MESSAGE_ID MESSAGE TIME DATE

Now, the message_id can be the same multiple times, and this is where my problem starts. I want to select the last row of dubplicated message_ID's sorting by the ID.

So, let's say I've got a normal get request and the result looks like this,

Array
(
    [0] => Array
        (
            [id] => 3
            [message_id] => 1
            [from_user_id] => 3
            [to_user_id] => 1
            [message] => last
            [time_sent] => 1331874924
            [date_sent] => 16/03/2012
            [opened] => 0
            [ip] => ::1
            [deleted] => 0
            [reported] => 0
        )

    [1] => Array
        (
            [id] => 2
            [message_id] => 1
            [from_user_id] => 3
            [to_user_id] => 1
            [message] => middle
            [time_sent] => 1331874920
            [date_sent] => 16/03/2012
            [opened] => 0
            [ip] => ::1
            [deleted] => 0
            [reported] => 0
        )

    [2] => Array
        (
            [id] => 1
            [message_id] => 1
            [from_user_id] => 3
            [to_user_id] => 1
            [message] => first
            [time_sent] => 1331874916
            [date_sent] => 16/03/2012
            [opened] => 0
            [ip] => ::1
            [deleted] => 0
            [reported] => 0
        )

    [3] => Array
        (
            [id] => 4
            [message_id] => 2
            [from_user_id] => 3
            [to_user_id] => 1
            [message] => test
            [time_sent] => 1331874916
            [date_sent] => 16/03/2012
            [opened] => 0
            [ip] => ::1
            [deleted] => 0
            [reported] => 0
        )

) 

Now, what i want, is to remove the dubplicated message_id's and only get this (notice how it should be ordering by ID)

Array
(
    [0] => Array
        (
            [id] => 3
            [message_id] => 1
            [from_user_id] => 3
            [to_user_id] => 1
            [message] => last
            [time_sent] => 1331874924
            [date_sent] => 16/03/2012
            [opened] => 0
            [ip] => ::1
            [deleted] => 0
            [reported] => 0
        )

    [1] => Array
        (
            [id] => 4
            [message_id] => 2
            [from_user_id] => 3
            [to_user_id] => 1
            [message] => test
            [time_sent] => 1331874916
            [date_sent] => 16/03/2012
            [opened] => 0
            [ip] => ::1
            [deleted] => 0
            [reported] => 0
        )

)

What I've been trying doing before is,

$this->db->order_by(‘id’, ‘DESC’);
$this->db->group_by(‘message_id’);

and

$this->db->group_by(‘message_id’);
$this->db->order_by(‘id’, ‘DESC’);

but i don't get the latest results from the message_id, only the first (looks like it's not soring before removing or something?)

Any help would be great!

I succeeded that with this on my tables:

select * from 
    (select * from da_user_messages order by id desc) t 
where receiver=1 group by msg_session

in your case msg_session is message_id, receiver is to_user_id and da_user_messages is your table name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM