MySQL: Select unique field and return all other columns as well + if specific column is not 0 - return that -


i can't figure out how done. making message system, have table: enter image description here

basically trying print out threads, every distinct msgfrom new thread. thread should say:

[(sender name) (last message (order id desc)) ([if @ least 1 message has msgread=0 have unread messages])

i've tried far:

select id, msgfrom, message, date,  if(msgread='0', 'unread', 'read') msr  pmessages msgto=$userid group msgfrom 

but returns:

message from: username (this msgfrom 1) read message from: username (this msgfrom 2) unread 

even though msgfrom1 has 1 unread message, says read (i assume takes first result). in basic:

select unique msgrom order id desc if @ least 1 msgread = 0    return * + set msgread=0 (unread) 

it's bit hard explain, sorry if it's bit unclear.

you can't reliably group data attempted in first sql.

if understand correctly, want differentiate posts read , unread?

select id      , msgfrom      , message      , date      , case when msgread = 0 'unread' else 'read' msgread   pmessages  msgfrom in ( select distinct msgfrom                       pmessages                      msgto = ?                   )  order msgfrom asc      , date desc 

please make sure use placeholders in code (assuming php) rather passing variable in directly query, prevent sql injection.

let me know how on.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -