简体   繁体   English

sql 依赖于其他表的每个值的查询

[英]sql query that depends on each value of other table

I'm not used to query sentences and this one is a bit tricky, I don't know even if it is possible (at least with just a single sql query) or if I should change the question name to something more accurated, if so let me know.我不习惯查询句子,这个有点棘手,我不知道是否可能(至少只有一个 sql 查询)或者我是否应该将问题名称更改为更准确的名称,如果所以让我知道。

I have two tables, I will make them simple.我有两张桌子,我会让它们变得简单。 The first one that is about games has a name and a details_id , the other on that consists on the game details has details_id (foreign key), locale and description .第一个关于游戏的有一个名称和一个details_id ,另一个关于游戏细节的有details_id (外键), localedescription

Games游戏
name名称
details_id详细信息_id
Details细节
details_id详细信息_id
locale语言环境
description描述

A game can have multiple details associated as the details_id is not unique.一个游戏可以关联多个详细信息,因为details_id不是唯一的。 So I can have 3 Details tables with same id associated to a Game but there is no Game with the same details_id as other.所以我可以有 3 个具有相同 id 的详细信息表与一个游戏相关联,但是没有与其他游戏具有相同details_id的游戏。 I will make an example:我举个例子:

GAMES游戏

name名称 details_id详细信息_id
Mario Kart马里奥卡丁车 007 007

DETAILS细节

details_id详细信息_id locale语言环境 description描述
007 007 en-GB英文 A nice racing game.一款不错的赛车游戏。
details_id详细信息_id locale语言环境 Description描述
007 007 es-ES es-ES Un buen juego de carreras. Un buen juego de carreras。

I show all the Games and their english descriptions in a view, they are now sorted by description as follows:我在视图中显示了所有游戏及其英文描述,它们现在按描述排序如下:

SELECT Games.name, Details.description 
FROM Games 
  INNER JOIN Details ON Details.locale='en-GB'

The trick is that I want it to depend on the locale, they might be games not translated to the same locale as others.诀窍是我希望它取决于语言环境,它们可能是未翻译成与其他语言环境相同的游戏。

The goal is a select that through a join, can select all the Game names and Detail descriptions ordered by description where locale is es-ES , and if the detail does not exist, it will take the en-GB one still ordering alphabetically among spanish descriptions.目标是一个 select ,通过连接,可以 select 所有游戏名称和详细描述按description排序,其中语言环境为es-ES ,如果详细信息不存在,它将采用en-GB仍然按字母顺序在西班牙语中排序说明。

Here I have added a second game which only has an english description.在这里我添加了第二个只有英文描述的游戏。 We join onto the details table twice, once to extract the english descriptions and once to extract the spanish descriptions.我们两次连接到详细信息表,一次提取英语描述,一次提取西班牙语描述。 We use the function COALESCE which returns the first value which is not null, ie.我们使用 function COALESCE,它返回第一个不是 null 的值,即。 the spanish if there is and the english if there is no spanish.如果有西班牙文,如果没有西班牙文,则英文。
Please see the dbFiddle link at the bottom for the schema and to test further.请查看底部的 dbFiddle 链接以了解架构并进一步测试。

 select * from games; select * from details;
 name |姓名 | details_id:----------- | details_id:------------ | ---------: Mario Kart | --------:马里奥卡丁车 | 7 Ninja Battle | 7 忍者之战 | 5 details_id | 5 详细信息_id | locale |语言环境 | description ---------: |:----- |:------------------------- 7 |描述 ----------: |:----- |:------------------------ 7 | en-GB | zh-CN | A nice racing game.一款不错的赛车游戏。 7 | 7 | es-ES | es-ES | Un buen juego de carreras. Un buen juego de carreras。 5 | 5 | en-GB | zh-CN | A street fighting game.街头格斗游戏。
 select name, coalesce(esp.description, eng.description) Description from games g left join (select details_id, description from details where locale = 'en-GB') eng on g.details_id = eng.details_id left join (select details_id, description from details where locale = 'es-ES') esp on g.details_id = esp.details_id
 name |姓名 | description:----------- |:------------------------- Mario Kart |描述:------------ |:------------------------ 马里奥卡丁车 | Un buen juego de carreras. Un buen juego de carreras。 Ninja Battle |忍者之战 | A street fighting game.街头格斗游戏。

db<>fiddle here db<> 在这里摆弄

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

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