我试图从我的数据库中获取变量,我在db中有一个值我有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
答案 0 :(得分:0)
为了避免将camera.name中的那个空间混淆为一个saperator,你可以用另一个char替换空格,例如_
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;