获取平均值并上传到新的db表mysql

时间:2018-04-16 15:59:56

标签: mysql sql-insert

我正在尝试获取表格中的列的平均值,然后将平均值插入第二个表格,我有超过30列,所以如果可能的话,我宁愿不必单独完成这些列

 command.CommandText = " INSERT INTO  FaceAverages(`rightEyeRightUpper`),(`rightEyeLeftUpper`),(`rightEyeRightLower`),(`rightEyeLeftLower`),(`leftEyeRightUpper`)... FROM(SELECT AVG (rightEyeRightUpper),(rightEyeLeftUpper),(rightEyeRightLower),(`rightEyeLeftLower`)... FROM 'FaceDistancesHappy')";

1 个答案:

答案 0 :(得分:0)

假设您的表FaceAverages包含coulmns

 `rightEyeRightUpper`,`rightEyeLeftUpper`,`rightEyeRightLower`,
      `rightEyeLeftLower`,`leftEyeRightUpper`...

然后你可以使用插入选择作为

command.CommandText = " INSERT INTO  FaceAverages(`rightEyeRightUpper`,`rightEyeLeftUpper`,
    `rightEyeRightLower`,  `rightEyeLeftLower`,`leftEyeRightUpper`... )
  SELECT AVG(rightEyeRightUpper), AVG(rightEyeLeftUpper), 
       AVG(rightEyeRightLower), AVG(`rightEyeLeftLower`), ... 
  FROM 'FaceDistancesHappy'";

声明您想要的所有列插入对应于您想要选择的所有列。并为select

中的每一列使用AVG()函数