简体   繁体   English

MySQL-使用条件执行JOIN

[英]MySql - executing JOIN with condition

I have 2. tables: videos and videos_lang. 我有2个表格:videos和videos_lang。 I join these tables with LEFT JOIN when I want to get information about a video in selected language. 当我想获取有关所选语言的视频的信息时,我将这些表与LEFT JOIN一起使用。 And here is the problem: if the translation in selected language doesn't exist, I want to join translation in default language (which always exists). 这就是问题所在:如果不存在所选语言的翻译,我想加入默认语言(始终存在)的翻译。 How can I do that? 我怎样才能做到这一点? I use that sql query: 我使用该sql查询:

SELECT vl.\*, v.* 
FROM videos v 
LEFT JOIN videos_lang vl 
ON vl.sig = 'de' AND v.idVideo = vl.idVideo

How to get translation where vl.sig = 'en' (en is the defualt language)? 如何在vl.sig ='en'(en是默认语言)的情况下获取翻译?

Try this query - 试试这个查询-

SELECT
  IFNULL(vl.idVideo, vl_en.idVideo),
  v.*
FROM videos v
LEFT JOIN (SELECT * FROM videos_lang WHERE sig = 'de') vl
  ON v.idVideo = vl.idVideo
LEFT JOIN (SELECT * FROM videos_lang WHERE sig = 'en') vl_en
  ON v.idVideo = vl_en.idVideo

You can try using OR condition inside your join query like this 您可以像这样在联接查询中尝试使用OR条件

SELECT vl.*, v.* FROM videos v LEFT JOIN videos_lang vl 
ON (vl.sig = 'de' OR vl.sig = 'en') AND v.idVideo = vl.idVideo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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