MySQL,如果另一个字段为空,如何检索一个字段?

发布于 2024-11-30 22:48:44 字数 240 浏览 2 评论 0原文

我需要检索一个字段,仅当不为空时:

我有一个表,其中包含:计数器(主要)、id(这对于同一页面是相同的)、文本、语言和其他字段:我有身份证和语言。

我需要搜索该语言的文本,如果它是空的,我需要英文文本。 例子: - 0,1,你好,eng - 1,1,再见,伊塔 在本例中,我需要检索“ciao”,但如果它为空,我需要“he​​llo”。

我该怎么办?

I need to retrieve a field, only if is not empty:

I have a table with: counter(primary),id (this is the same for the same page), text, language and other fields: i have the id and the language.

I need to search the text in that language, and if it's empty I need the text in english.
example:
- 0,1,hello,eng
- 1,1,ciao,ita

In this case I need to retrieve "ciao", but if it's empty I need "hello".

How can I?

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

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

发布评论

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

评论(2

狼性发作 2024-12-07 22:48:44

好吧,我想您可以获取意大利语(如果不为空)英语,并以意大利语优先(如果有)排序:(

SELECT
    text
FROM
    <table>
WHERE
    id = <page-id>
  AND (
      (
        language = '<preferred-language-to-retrieve>'
          AND
        text IS NOT NULL
      )
    OR
      language = '<fallback-language>'
  )
ORDER BY
    language = '<preferred-language-to-retrieve>' DESC
LIMIT 1

未经测试)

使用意大利语的替代值和页面 ID = 的英语后备75(假设表名为l10n):

SELECT
    text
FROM
    l10n
WHERE
    id = 75
  AND (
      (
        language = 'ita'
          AND
        text IS NOT NULL
      )
    OR
      language = 'eng'
  )
ORDER BY
    language = 'ita' DESC
LIMIT 1

Well, I guess you could just get the Italian (if not empty) and the English and order with Italian first (if any):

SELECT
    text
FROM
    <table>
WHERE
    id = <page-id>
  AND (
      (
        language = '<preferred-language-to-retrieve>'
          AND
        text IS NOT NULL
      )
    OR
      language = '<fallback-language>'
  )
ORDER BY
    language = '<preferred-language-to-retrieve>' DESC
LIMIT 1

(untested)

With substituted values for Italian with English fallback for page ID = 75 (assuming table is named l10n):

SELECT
    text
FROM
    l10n
WHERE
    id = 75
  AND (
      (
        language = 'ita'
          AND
        text IS NOT NULL
      )
    OR
      language = 'eng'
  )
ORDER BY
    language = 'ita' DESC
LIMIT 1
残月升风 2024-12-07 22:48:44

试试这个:

SELECT text 
FROM table lcl
WHERE id=1 AND language='ita'
UNION ALL
SELECT text 
FROM table eng
WHERE id=1 AND language='eng'
LIMIT 1

第一个选择应该始终是特定于语言的检查,第二个选择将返回英语版本,如果您使用限制 1,您将只获得第一行。
如果缺少语言特定版本,您最终将在第二个选择中得到值,英语

SELECT 
    CASE WHEN 
    lcl.text IS NOT NULL THEN 
        lcl.text
    ELSE
        eng.text END AS text
FROM table eng
LEFT JOIN table lcl ON lcl.id=1 AND lcl.language='ita' 
WHERE eng.id=1 AND eng.language='eng'

这将获得英语版本(因此假设它始终存在),如果存在,则获得选定的语言版本。
在选择中,我们只需显示本地语言版本(如果不为空)或英语版本(如果为空)。

try this :

SELECT text 
FROM table lcl
WHERE id=1 AND language='ita'
UNION ALL
SELECT text 
FROM table eng
WHERE id=1 AND language='eng'
LIMIT 1

The first select should always be the language specific check, the 2nd select will return the english version, and if you use limit 1 you will only ever get the first row.
If the language specific version is missing you will end up with the value in the 2nd select, English

OR

SELECT 
    CASE WHEN 
    lcl.text IS NOT NULL THEN 
        lcl.text
    ELSE
        eng.text END AS text
FROM table eng
LEFT JOIN table lcl ON lcl.id=1 AND lcl.language='ita' 
WHERE eng.id=1 AND eng.language='eng'

This will get the english version (so assumes it is always there) and if it exists gets the selected langange version.
In the select we simply have to display the local language version if it not empty or the english version if it is.

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