MySQL condition (display username if exist row in phonebook) -
i have phonebook table contains user contacts number
, name
. have messages table (text
, number
, date
etc.)
i need contact name phonebook if there exists record or number messages table.
i tried this:
select owner, sender, left(text, 28) text, date, status, if((select name phonebook number = sender), name, sender) messages order id desc limit 30
but not work. (1054 - unknown column 'name' in 'field list')
sorry bad english.
you left join
. if there no phonebook record name null
, coalesce
returns second argument, i.e. sender
.
select owner, sender, left(text, 28) text, date, status, coalesce(name, sender) messages left join phonebook on number = sender order id desc limit 30
Comments
Post a Comment