简体   繁体   中英

How to select a value in a table and if it doesn't exist another value?

I tried to find a solution for this, but most other answers don't work.

I have a page to translate my website, with mySQL all the texts are echo'd (in an overflow:auto of course, so that the whole page isn't filled with text), and I want it to echo all the texts which are already translated in the translation-language. All the texts from the websites which don't have a translation yet, should have "English" as the chosen language to display. I have tried all kinds of things, but I can't get it to work.

$sql=mysql_query("SELECT * FROM `website` WHERE language='$_SESSION[translate]' GROUP BY name");

is the query which I currently have. This returns all the translated values in the translation language, which is fine, but it doesn't display ALL the 'name'-values and it should display all the other 'name'-values in English, because they don't have a translation yet. But I have no idea how to do it, and JOINS only seems to work for multiple tables.

You can join a table with itself:

SELECT w1.name, IFNULL(w2.value, w1.value) AS value
FROM website AS w1
LEFT JOIN website AS w2 ON w1.name = w2.name AND w2.language = '$_SESSION[translate]'
WHERE w1.language = 'english'

The outer join result in a NULL value when there's no translation, and IFNULL then uses the English version as a default.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM