MYsql want to fetch title_en if title_es is empty
I have a table in mysql having two fileds title_en,title_es If language is ENGLISH then title_en is required and title_es is optional. If langauge is SPANISH then title_Es is required and title_en is optional while entring form data. NOTE: This is for data entry.
Most likely one filed will be empty.
In PHP i write dynamic query
$sql='Select title_'.$lang.' from product';
If Language is English then it w开发者_如何学Pythonill be "Select title_en from product". If Language is Spanish then it will be "Select title_es from product".
For Listing page IF LANGUAGE is SPANISH, i have an issue that for some records(Entered when language was English) title_es is empty.
I know it can be handled by fetching both and using PHP function that will check if title_es is empty then title_en whould be shown.
But i want to handle it in Mysql as all site is complete and at this point client make 1 title required and other optional, which in start was both required.
Please suggest only for mysql solution.
Thanks for reading this.
SELECT
case when title_es is null then title_en
else title_es
end as title
FROM product
select case title_en
when null then title_es
else title_en end as title
from product
If you are not allowing nulls in the fields, then just replace the null with ""
Hope this helps!!
精彩评论