gpt4 book ai didi

mysql - bash中取mysql数据,函数read读取每一句作为变量

转载 作者:行者123 更新时间:2023-11-30 21:46:46 24 4
gpt4 key购买 nike

我试图从我的数据库中获取变量,我在数据库中有一个值我有 2 个单词,但下面的命令将其视为 2 个变量。

是否有任何更改以我的方式获取它?

   mysql -uroot -proot --database=symfony -se "SELECT cameras.source, cameras.name, investments.name, users.username FROM cameras JOIN investments ON investments.id = cameras.investment_id JOIN users ON users.id = investments.user_id;" | while read source camera investment username; do

mysql 结果:

| source                                                                 | name         | name       | username |
+------------------------------------------------------------------------+--------------+------------+----------+
| rtsp://vlc:vlc@test/cam/realmonitor?channel=1&subtype=0 | camera 1 | investment | user |
+------------------------------------------------------------------------+--------------+------------+----------+

狂欢:

echo "source :"$source
echo "camera :" :$camera
echo "investments: " $investments
echo "username: " $username

结果:

source :rtsp://vlc:vlc@test/cam/realmonitor?channel=1&subtype=0channel=1&subtype=0
camera : investment
investments: 1
username: investment username

最佳答案

为避免 camera.name 中的空格被混淆为分隔符,您可以将空格替换为另一个字符,例如 _

 SELECT cameras.source
, replace(cameras.name, ' ', '_')
, investments.name, users.username
FROM cameras
JOIN investments ON investments.id = cameras.investment_id
JOIN users ON users.id = investments.user_id;

关于mysql - bash中取mysql数据,函数read读取每一句作为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49083260/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com