Answer the question
In order to leave comments, you need to log in
Why does OR check both conditions when at least one is true?
There are product cards with localization stored in a separate database table. On the store page, the user is shown those products whose description is localized in the language selected by the user, if there is none, then the product card is shown in English (set by default when adding a product as the first language).
To work with queries to the database I use redbeanphp
Here is my query:
SELECT game_text_info.title, game_text_info.descr, games.*
FROM game_text_info,games
WHERE game_text_info.game_id = games.id
AND (game_text_info.local = :local OR game_text_info.local = "en")
AND game_text_info.title LIKE :search'
Answer the question
In order to leave comments, you need to log in
If the English version is always available, and others are optional, you can try this:
SELECT ISNULL(i2.title, i1.title) as title, ISNULL(i2.descr, i1.descr) as descr, gg.*
FROM game_text_info as i1
INNER JOIN games as gg
ON gg.id = i1.game_id
LEFT JOIN game_text_info as i2
ON i2.game_id = i1.game_id
AND i2.game_text_info.local = :local
WHERE i1.title LIKE ':search'
and i1.game_text_info.local = "en"
Here you explicitly indicate that local can be equal to what is in :local or 'en'
what were you waiting for?
you have a one-to-many join in your query, on one row from the games table you hook all the rows from game_text_info that match the condition in WHERE
I don’t know if MySQL has HAVING, but it would help here
Fill in an example on sqlfiddle.com
First game_text_info.local = :local
, then UNION
the rest game_text_info.local = "en"
. There will be no repeat.
the product description may not be available in the user's language, so the user is requested to read the English versionIn the subquery, find the product you need, associate descriptions with it twice (local and English versions), if the first one is missing, show the second one.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question