K
K
kir_cipher2017-09-12 17:09:25
MySQL
kir_cipher, 2017-09-12 17:09:25

How to select from a MySQL database data with a specific column value or (in case of mismatch) data with a specified column value?

Good afternoon, ladies and gentlemen! I think the title does not explain the essence of the issue much, so I'll try to describe it here in normal language xD
I have a table with news in the database. One of the columns is responsible for the language (lang = 'ru'/'en'/etc). I want to make a query that would select only the English versions of the news from the database or (if there is no translated one) - in Russian.
Here is what we have at the moment, but it does not work correctly for obvious reasons.

select news.id,news.nid,news.uid,news.show_author,news.title,news.tid,news.html,news.image,news.smallimage,news.views,news.created 
from news 
where lang='en' or (((select count(*) from news where news.nid=news.nid and lang='en') = 0) and (news.lang='ru'));

The nid column is responsible for the ID of the news itself (that is, the same news in different languages ​​has the same nid). Please help organize the request

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kir_cipher, 2017-09-12
@kir_cipher

The toaster is magical. While writing the question, I came up with an answer. I gave the table an alias and pulled nid into a subquery through it. Final working result:

select newsData.id,newsData.nid,newsData.uid,newsData.show_author,newsData.title,newsData.tid,newsData.html,newsData.image,newsData.smallimage,newsData.views,newsData.created from news newsData where newsData.lang='en' or (((select count(*) from news where nid=newsData.nid and lang='en') = 0) and (newsData.lang='ru'));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question