Y
Y
YakutD2019-02-22 15:32:01
SQL
YakutD, 2019-02-22 15:32:01

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'

If the user's language is 'en', then all is well. But if the language is Russian ('ru'), then the query displays both those products that have a Russian description and their English versions. I suppose the matter is that sampling from different tables and the reason also is covered in it. How to be?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Ruslan., 2019-03-13
@YakutD

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"

V
Vladimir, 2019-02-22
@Casufi

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

K
Konstantin Tsvetkov, 2019-02-22
@tsklab

First game_text_info.local = :local, then UNIONthe 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 version
In 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 question

Ask a Question

731 491 924 answers to any question