經過多個位置變量處理數據的小方法!

cat dd.txt
mysql

courseId  startTime  endTime
sql

107 1548471600  1548475200
337 1550455200  1550458800
337 1553306400  1553310000
bash


如上所述,如今有一批數據,須要經過courseId、startTime、endTime三個維度(三個變量)惟一肯定一條數據,而後以courseId分組進行數據統計。筆者記得有位置變量能夠實現,不過很遺憾的是忘記了,只能對而求其次,換了一種方法實現,若是有大神路過,還請留言指教ide


cat dd.sh
#!/bin/bash

username=account
passwd=mima
mysql -u$username -p$passwd -Bse "truncate table test.caidan"

while [ "1" = "1" ]
do
    id=`cat dd.txt | head -1 | awk '{print $1}'`
    start=`cat dd.txt | head -1 | awk '{print $2}'`
    end=`cat dd.txt | head -1 | awk '{print $3}'`
    num1=`mysql -u$username -p$passwd edusoho_e -Bse "SELECT COUNT(*) FROM course_member WHERE courseId=$id AND createdTime BETWEEN $start AND $end"`
    mysql -u$username -p$passwd -Bse "insert into test.caidan select $id,$num1"
    sed -i '1d' dd.txt
    num=`cat dd.txt | wc -l`
    if [ $num -eq 0 ];then
        exit
    fi
done
spa


分組統計結果it

SELECT id,SUM(num1) FROM `test`.`caidan` GROUP BY id ORDER BY NULL
table

相關文章
相關標籤/搜索