0

Before all, sorry my very bad english level. Here's my problem.

If i have two tables: 'Products'

|id | product |
|---|---------|
|1  | "Fork"  |
|2  | "Spoon" |
|3  | "Knife" |

and 'taxes'

|id | id_prod |   tax   |
|---|---------|---------|
|1  |    1    |   21    |
|2  |    2    |   11,5  |

If i execute the following command:

SELECT product.*, taxes.tax FROM products LEFT JOIN taxes ON taxes.id_prod = products.id

I will obtain this result.

|id | product |   tax   |
|---|---------|---------|
|1  |  "Fork" |   21    |
|2  | "Spoon" |   11,5  |
|3  | "Knife" |   NULL  |

My question is: How i can give a default value when the product is not included in tax?

I want to receive a result like this: If the default value is "21"

|id | product |   tax   |
|---|---------|---------|
|1  |  "Fork" |   21    |
|2  | "Spoon" |   11,5  |
|3  | "Knife" |   21    |

How i can do that?

Thanks in advance...

1 Answer 1

1
SELECT product.*, IFNULL(taxes.tax,21) AS tax
FROM products
LEFT JOIN taxes ON taxes.id_prod = products.id;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.