即使在数据库 mysql 中找不到结果,如何获取值

发布于 2025-01-16 19:32:56 字数 1450 浏览 4 评论 0原文

即使数据库中不存在数据,我也需要找到正确的查询来获取值。

我的表名为“prova”:

它是ende
datadata
rigarowlinie
parola

如果我查询:

SELECT en,de FROM `prova` WHERE `it` IN ("data","riga");

结果是:

ende
data
rowlinie

没关系!

但是

SELECT en,de FROM `prova` WHERE `it` IN ("data","riga","ciao","parola");

结果是:

ende
data
rowlinie

正如标准 mysql 中的预期,但对我来说是一个问题。

期望的结果是:

ende
datanot found
rowlinie
not findnot found
not findnot find

为什么我想要这个?

我需要用一个字符串数组执行查询,实际上我需要一个一个字符串查询,并检查查询是否为空。结果数组的大小和顺序应等于给定数组。

实际上我需要执行 8700 个查询,这将帮助我将查询数量减少到 8。

谢谢

I need to find the right query to get a value even if the data not exist in the db.

I have my table named "prova":

itende
datadata
rigarowlinie
parola

If I query:

SELECT en,de FROM `prova` WHERE `it` IN ("data","riga");

The resoult is:

ende
data
rowlinie

And it's ok!

BUT

SELECT en,de FROM `prova` WHERE `it` IN ("data","riga","ciao","parola");

The resoult is:

ende
data
rowlinie

Is as expected in the standard mysql but for me is a problem.

The the desired result is:

ende
datanot found
rowlinie
not foundnot found
not foundnot found

Why I want this?

I need to perform a query with an array of string, actually I need to query one by one string, and check if the query is empty. The size and the order of the resulted array should be equal to the given array.

Actually I need to perform 8700 queries, this will help me to decrease the queries number to 8.

Thanks

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

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

发布评论

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

评论(2

你丑哭了我 2025-01-23 19:32:56

您可以使用返回字符串数组的 cte,其中的数字对应于结果中字符串的顺序以及表的 LEFT 连接:

WITH cte(id, word) AS (VALUES
  ROW(1, 'data'), ROW(2, 'riga'), ROW(3, 'ciao'), ROW(4, 'parola')
)
SELECT COALESCE(p.en, 'not found') en,
       COALESCE(p.de, 'not found') de
FROM cte c LEFT JOIN prova p
ON p.it = c.word
ORDER BY c.id;

或者,使用 UNION ALL code> 对于 8.0 之前没有 cte 支持的 MySql 版本:

SELECT COALESCE(p.en, 'not found') en,
       COALESCE(p.de, 'not found') de
FROM (
  SELECT 1 id, 'data' word UNION ALL
  SELECT 2, 'riga' UNION ALL 
  SELECT 3, 'ciao' UNION ALL 
  SELECT 4, 'parola'
) t
LEFT JOIN prova p ON p.it = t.word
ORDER BY t.id;

请参阅 演示

You can use a cte that returns the array of strings with a number that corresponds to the order of the string in the results and a LEFT join of the table:

WITH cte(id, word) AS (VALUES
  ROW(1, 'data'), ROW(2, 'riga'), ROW(3, 'ciao'), ROW(4, 'parola')
)
SELECT COALESCE(p.en, 'not found') en,
       COALESCE(p.de, 'not found') de
FROM cte c LEFT JOIN prova p
ON p.it = c.word
ORDER BY c.id;

Or, with UNION ALL for versions of MySql prior to 8.0 without the cte support:

SELECT COALESCE(p.en, 'not found') en,
       COALESCE(p.de, 'not found') de
FROM (
  SELECT 1 id, 'data' word UNION ALL
  SELECT 2, 'riga' UNION ALL 
  SELECT 3, 'ciao' UNION ALL 
  SELECT 4, 'parola'
) t
LEFT JOIN prova p ON p.it = t.word
ORDER BY t.id;

See the demo.

命硬 2025-01-23 19:32:56

使用coalesce 用默认值填充空值。

select
  coalesce(en, 'not found'),
  coalesce(de, 'not found')
...

对于第二部分,如何使所有 in 值显示为行,请参阅此答案

Use coalesce to fill in a null value with a default.

select
  coalesce(en, 'not found'),
  coalesce(de, 'not found')
...

For the second part, how to make all the in values show up as rows, see this answer.

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