简体   繁体   English

使用不同的语言从数据库中选择类别

[英]SELECT categories FROM database using different languages

I hope someone will give me an idea how to proceed, because I'm losing my mind right now. 我希望有人能给我一个思路,因为我现在正在失去理智。

I just made two tables in my database. 我刚刚在数据库中制作了两个表。 First one looks like this: 第一个看起来像这样:

categories
categories_id
cat_group (categories group)
cat_name (category name)
cat_lang (category language)

Second one: 第二个:

users
users_id
username
category

You'll probably ask yourself why am I using the "categories group". 您可能会问自己,为什么我要使用“类别组”。 Well I need sometimes to pull all categories from the database for a certain group. 好吧,有时我需要从数据库中为特定组提取所有类别。 Let's see an example: 让我们来看一个例子:

cat_group = teacher
cat_name = teacher
cat_lang = 1 (en)

cat_group = teacher
cat_name = leraar
cat_lang = 2 (dutch)

cat_group = teacher
cat_name = professeur
cat_lang = 3 (french)

When I try to SELECT all teachers now from both tables, it works just fine. 当我现在尝试从两个表中选择所有教师时,它工作正常。 But sometimes the category names are the same in different languages. 但是有时类别名称在不同语言中是相同的。

For example: 例如:

Category name: student (english) category name: student (dutch) category name: étudiant (french) 类别名称:学生(英语)类别名称:学生(荷兰语)类别名称:étudiant(法语)

When I try the same query: 当我尝试相同的查询时:

SELECT users.username, users.category, categories.cat_group 
FROM users
LEFT JOIN categories ON users.category = categories.category_name
WHERE categories.cat_group = 'student'

I end up with the following result: 我得到以下结果:

student adam student
student chris student
étudiant brian student
student adam student
student chris student

I don't know what I'm doing wrong here. 我不知道我在做什么错。 I just need something like: category_name: student username: adam category_group : student 我只需要类似以下内容:category_name:学生用户名:adam category_group:学生

Am I doing anything wrong in my query? 我在查询中做错什么了吗?

Thanks in advance. 提前致谢。

I do not understand how any of the answers actually fixes your problem. 我不了解任何答案如何真正解决您的问题。 For instance, how are you supposed to determine which language to use ? 例如,您应该如何确定使用哪种语言?

I just need something like ... Am I doing anything wrong in my query? 我只需要...在查询中做错什么了吗?

We need to go back to first principles. 我们需要回到第一原则。 The Data model is not ready for coding. 数据模型尚未准备好进行编码。 When it is, the code will be easy. 如果是这样,代码将很容易。

Multiple language support requires a bit more work. 多语言支持需要更多的工作。 The issue is worked around but not dealt with explicitly, and it needs to be. 该问题已解决,但未明确解决,需要解决。 With those two serious issues not being resolved, it is no wonder that you are mentally stressed. 由于这两个严重问题尚未解决,因此您的精神压力也就不足为奇了。 And that's assuming you are reasonably qualified for the task, otherwise you have a third cause of stress. 这是假设您有足够的资格胜任这项工作,否则您会感到压力的第三个原因。

You need something like this. 您需要这样的东西。

▶Normalised Data Model◀ (inline links do not work on some browsers/versions.) ▶标准化数据模型Data (内联链接在某些浏览器/版本上不起作用。)

Readers who are not familiar with the Relational Modelling Standard may find the ▶IDEF1X Notation◀ useful. 不熟悉关系建模标准的读者可能会发现▶IDEF1X Notation◀有用。

Your examples show a single category per User, so I have provided that; 您的示例为每个用户显示了一个类别,因此我提供了该类别; if an User can belong to multiple Categories, you need a different model, let me know. 如果一个用户可以属于多个类别,那么您需要一个不同的模型,请告诉我。

Code

Now your queries are easy. 现在,您的查询很简单。 I realise your query is an example, but there is no context, and in a language-specific environment, language is always fairly high up in the order, always part of the context. 我知道您的查询只是一个示例,但是没有上下文,并且在特定于语言的环境中,语言始终是顺序最高的语言,始终是上下文的一部分。 Here is a query), similar to your example, for all users with Category in their language: 这是一个查询),类似于您的示例,针对使用语言的所有类别的所有用户:

SELECT  FirstName,
        LastName,
        Name AS Category
    FROM User              U,
         CategoryLanguage  CL
    WHERE U.CategoryId   = CL.CategoryId
    AND   U.LanguageCode = CL.LanguageCode
Notice there is no need to join with Category . 注意,没有必要加入Category This is because I have used the Identifiers that exist, and migrated them to the child tables, as per the Standard, and not stuck an Id column on everything that moves (in the case of Id columns all over the place, yes, you would have to join with Category ; that is eliminated). 这是因为我已经使用了存在的标识符 ,并且按照标准将其迁移到了子表中,并且没有在移动的所有对象上都添加一个Id列(对于整个地方的Id列,是的,必须加入Category ;这已被淘汰)。 CategoryId and LanguageCode are exactly that, wherever they show up. 无论它们出现在什么地方, CategoryIdLanguageCode都是这样。 No Nulls, no outer joins. 没有Null,没有外部联接。

Here is the same query in your language (let's assume English): 以下是您所用语言的相同查询(假设使用英语):

 SELECT FirstName, LastName, Name AS Category FROM User U, CategoryLanguage CL WHERE U.CategoryId = CL.CategoryId AND U.LanguageCode = "en" -- or ${language} from the app layer AND U.CategoryId = ( SELECT CategoryId FROM CategoryLanguage WHERE LanguageCode = "en" -- or ${language} from the app layer AND Name = "Student" -- or ${category} from the app layer ) 

Joining on strings is expensive and risky. 连接字符串很昂贵且有风险。 What happens when a category_name changes in Categories? 当category_name在Categories中更改时会发生什么? You have to cascade the update to all of your Users records. 您必须将更新级联到所有“用户”记录。 A few suggestions: 一些建议:

  1. Use categories_id as your link from users to categories instead of the actual string description. 使用Categories_id作为用户到类别的链接,而不是实际的字符串描述。
  2. Create a Categories_Group table with ID and Description columns. 创建具有ID和Description列的Categories_Group表。 As above, use that ID as the link from Categories to Categories_Group instead of storing the descriptive group text in the Categories table. 如上所述,使用该ID作为从Categories到Categories_Group的链接,而不是将描述性的组文本存储在Categories表中。
  3. Finally, use the users.categories_id in your join condition and use the categories.cat_group_id in your where clause to eliminate the issues of duplicate names. 最后,在联接条件中使用users.categories_id,并在where子句中使用Categories.cat_group_id来消除名称重复的问题。

Despite the normalization, and natural key issues ... your query should be like 尽管存在标准化和自然键问题,但您的查询应该像

LEFT JOIN 
  categories ON users.category = categories.category_name and
  categories.cat_name='student' <-- make sure cat_name is student (dutch,english)
  and categories.cat_group='student'
  and cat_lang=1 <-- if is english

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

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