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

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -