根据字段内容仅更改一行上的选择的 WHERE

发布于 2024-07-15 10:37:15 字数 560 浏览 2 评论 0原文

好吧,这可能听起来有点奇怪,但我需要做的很简单。 我有一份公司列表,每个公司都有一个描述。 该网站有 4 种语言版本。 当我用英语列出一家公司但负责用法语翻译的人仍然没有进行翻译时,我的问题就出现了,所以我有一个简单的:

SELECT TOP(6)
    company.name,
    company.description,
    company.address
WHERE
    langid=1
ORDER BY
    company.id DESC

真正的查询更复杂,但我认为它会有了这个就更容易理解这个问题了。 我需要做的是,当 langid 2 中的描述为空时,请显示 langid 1,因为 langid 1 始终是第一个添加的描述。 我不想将描述字段显示为空。

想象一下,第 1 行、第 2 行、第 3 行和第 5 行有 langid 2 的描述,但没有第 4 行和第 6 行。我需要为包含它的行显示 langid 2,但为没有描述的行显示 langid 1。

我知道我可能可以用 @i 字段递增并将结果一一插入到临时表中,但是有没有更好的方法呢?

Ok, this may sound a little weird, but what I need to do is simple. I have a list of companies and each one has a description. The site is in 4 languages. My problem comes when I have listed a company in English but the person who is in charge of translating it in French still didn't make the translation, so I have a simple:

SELECT TOP(6)
    company.name,
    company.description,
    company.address
WHERE
    langid=1
ORDER BY
    company.id DESC

The real query is more complex, but I think it will be easier with this one to understand the problem. What I need to do is, when the description is empty in langid 2 then show me the langid 1 as langid 1 is always the first one that will be added. I don't want to show the description field empty.

Imagine row 1, row 2, row 3 and row 5 have a description for langid 2 but not row 4 and row 6. I need to show langid 2 for the rows containing it but langid 1 for the rows with no description.

I know that I could probably do a while with a @i field incrementing and inserting the results one by one in a temp table, but is there any better way of doing it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

够钟 2024-07-22 10:37:15

除非缺少 lng1.name 等(即英语/后备变体),否则这将起作用。

SELECT TOP(6) 
  COALESCE(lng2.name, lng1.name)               [name], 
  COALESCE(lng2.description, lng1.description) description, 
  COALESCE(lng2.address , lng1.address )       address
FROM 
  company lng1
  LEFT JOIN company lng2 ON 
    lng1.id     = lng2.id AND
    lng1.langid = 1 AND
    lng2.langid = 2  /* your "foreign language" id, e.g. a parameter */
WHERE
  lng1.id IN (1,2,3,4,5,6)  /* or whatever */
ORDER BY 
  lng1.id DESC

如果仅缺少部分本地化变体,它也不会用英语/后备版本替换整个地址。 仅替换缺失的部分。

This will work unless there is lng1.name etc. (i.e. the English language/fallback variant) missing.

SELECT TOP(6) 
  COALESCE(lng2.name, lng1.name)               [name], 
  COALESCE(lng2.description, lng1.description) description, 
  COALESCE(lng2.address , lng1.address )       address
FROM 
  company lng1
  LEFT JOIN company lng2 ON 
    lng1.id     = lng2.id AND
    lng1.langid = 1 AND
    lng2.langid = 2  /* your "foreign language" id, e.g. a parameter */
WHERE
  lng1.id IN (1,2,3,4,5,6)  /* or whatever */
ORDER BY 
  lng1.id DESC

It will also not replace the entire address with the English/fallback version if only part of the localized variant is missing. Only the missing parts will be substituted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文