Aram Alvarez
Aram Alvarez

Reputation: 566

MySQL syntax for conditional CONCAT depending on a field content?

I have inherited a simple table from a client, that stores two addresses (shipping address and business address) in just two fields shipping_address and business_address. There is a field called ship_to with enum('shipping','business'), and a field for a full name. The client wants an output as:

full name: address

Where "address" column should be picked by the ship_to value. If ship_to = shipping then use shipping_address column. If ship_to = business then use business_address column.

For instance:

Joe Smith: Roach Street #10, Rats Village

How I can use CONCAT with IF condition? I'm guessing something like this, which obviusly is just an idea:

SELECT CONCAT( full_name, ':', IF (ship_to=='shipping', shipping_address, business_address ) AS contact FROM `table`

But, what's the right syntax for doing this?

Upvotes: 2

Views: 7147

Answers (1)

Edper
Edper

Reputation: 9322

Lacking the closing )

SELECT 
CONCAT( full_name, ':', IF (ship_to='shipping', shipping_address, business_address )) as    contact
FROM TableName

Upvotes: 2

Related Questions