0

With this query, I want a name comprised of three fields to be returned. I don't understand why the concat() function is not returning all the requested values in the table. Some records will be filled--let `s say like 1-2-3. The result should then be "1 2 3". If the 3 records are 1-0-3 than the result should be "1 3." If the records are 1-2-0 then the result should be "1 2."

The three requested fields are all VARCHAR format.

See my results here The first and second records are wrong, the third and fourth are right, then, again, some are right and some are wrong.

SELECT pt_rooms.room_id, pt_rooms.room_hotel_id ,
a_room_type.room_type_name, 
pt_rooms.room_title, a_room_type_suffix.room_type_suffix_name ,    
CONCAT(a_room_type.room_type_name,' ', pt_rooms.room_title,' ',   
a_room_type_suffix.room_type_suffix_name ) 
AS long_name FROM `pt_rooms` 
LEFT JOIN a_room_type_suffix ON   
a_room_type_suffix.room_type_suffix_id=pt_rooms.room_type_suffix_id 
JOIN a_room_type ON a_room_type.room_type_id=pt_rooms.room_type;
3
  • txs phroureo SELECT pt_rooms.room_id, pt_rooms.room_hotel_id , a_room_type.room_type_name, pt_rooms.room_title, a_room_type_suffix.room_type_suffix_name , CONCAT_WS(' - ','a_room_type.room_type_name','pt_rooms.room_title','a_room_type_suffix.room_type_suffix_name') as long_name FROM pt_rooms LEFT JOIN a_room_type_suffix ON a_room_type_suffix.room_type_suffix_id=pt_rooms.room_type_suffix_id JOIN a_room_type ON a_room_type.room_type_id=pt_rooms.room_type; Commented Nov 17, 2015 at 21:44
  • i have modifyed the query as per concat_ws documentation but stil i get wrong results Commented Nov 17, 2015 at 21:45
  • hi, ok it is solve now i had to remove backthicks in concat_ws() string Commented Nov 18, 2015 at 11:54

1 Answer 1

3

CONCAT() will return NULL if any of the values passed to it are NULL.

To get around this, you can use CONCAT_WS():

CONCAT_WS(' ', a_room_type.room_type_name, pt_rooms.room_title, a_room_type_suffix.room_type_suffix_name) AS long_name
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.