即使在数据库 mysql 中找不到结果,如何获取值
即使数据库中不存在数据,我也需要找到正确的查询来获取值。
我的表名为“prova”:
它是 | en | de |
---|---|---|
data | data | |
riga | row | linie |
parola |
如果我查询:
SELECT en,de FROM `prova` WHERE `it` IN ("data","riga");
结果是:
en | de |
---|---|
data | |
row | linie |
没关系!
但是
SELECT en,de FROM `prova` WHERE `it` IN ("data","riga","ciao","parola");
结果是:
en | de |
---|---|
data | |
row | linie |
正如标准 mysql 中的预期,但对我来说是一个问题。
期望的结果是:
en | de |
---|---|
data | not found |
row | linie |
not find | not found |
not find | not 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":
it | en | de |
---|---|---|
data | data | |
riga | row | linie |
parola |
If I query:
SELECT en,de FROM `prova` WHERE `it` IN ("data","riga");
The resoult is:
en | de |
---|---|
data | |
row | linie |
And it's ok!
BUT
SELECT en,de FROM `prova` WHERE `it` IN ("data","riga","ciao","parola");
The resoult is:
en | de |
---|---|
data | |
row | linie |
Is as expected in the standard mysql but for me is a problem.
The the desired result is:
en | de |
---|---|
data | not found |
row | linie |
not found | not found |
not found | not 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用返回字符串数组的 cte,其中的数字对应于结果中字符串的顺序以及表的
LEFT
连接:或者,使用
UNION ALL
code> 对于 8.0 之前没有 cte 支持的 MySql 版本:请参阅 演示。
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:Or, with
UNION ALL
for versions of MySql prior to 8.0 without the cte support:See the demo.
使用
coalesce
用默认值填充空值。对于第二部分,如何使所有
in
值显示为行,请参阅此答案。Use
coalesce
to fill in a null value with a default.For the second part, how to make all the
in
values show up as rows, see this answer.