简体   繁体   English

使用 while read 将 MySQL 查询中的空变量设置为 false

[英]Set empty variable from a MySQL Query to false using while read

MySQL-Version: mysql Ver 15.1 Distrib 10.2.31-MariaDB, for Linux (x86_64) using readline 5.1 Bash-Version: GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu) MySQL 版本:mysql Ver 15.1 Distrib 10.2.31-MariaDB,用于 Linux (x86_64) 使用 readline 5.1 Bash 版本:GNU bash,版本 4.2.46(2)-release (x86_64-redhat-linux-gnu)

I'm using the following code with Bash to do a MySQL query.我在 Bash 中使用以下代码来执行 MySQL 查询。

The value of "ssl" in the MySQL Database is empty (I check that with the condition SELECT * FROM domains WHERE ssl=') MySQL 数据库中“ssl”的值为空(我使用条件 SELECT * FROM domain WHERE ssl=' 进行检查)

while read ssl certificate_id; do

echo $ssl
echo $certificate_id

done < <(mysql -NB -utheuser -pthemysqlpass psa -e  "SELECT ssl, certificate_id FROM domains WHERE id='26';")

If ssl is empty in the database, the read command will ignore or skip this variable and set the value of certificate_id to ssl.如果数据库中的 ssl 为空,则读取命令将忽略或跳过此变量并将 certificate_id 的值设置为 ssl。

I tried a lot of variants using IFS= but it didn't work.我使用 IFS= 尝试了很多变体,但没有用。

I want to set the value of "ssl" to "false" if "ssl" is empty.如果“ssl”为空,我想将“ssl”的值设置为“false”。

Do you have an idea how to do that?你知道怎么做吗?

Thanks!谢谢!

I think that I found an alternative using the IF function of MySQL:我想我找到了一个使用 MySQL 的 IF 函数的替代方法:

SELECT IF(ssl = '', 'false', ssl) ssl, certificate_id FROM domains WHERE id='26';" SELECT IF(ssl = '', 'false', ssl) ssl, certificate_id FROM domain WHERE id='26';"

I have to do more tests using bash but the first tests using PHPMyAdmin gave the correct result.我必须使用 bash 进行更多测试,但使用 PHPMyAdmin 的第一次测试给出了正确的结果。

Maybe it will help others!也许它会帮助别人!

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

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