zoukankan      html  css  js  c++  java
  • MariaDB多表联查以及导出cvs文件

    1、多表联查(内联  INNER JOIN)

     1   string productSqlStr = "SELECT * FROM " +
     2             "product_info info " +
     3             "INNER JOIN product_feed_info feed_info " +
     4             "INNER JOIN product_pcb_info pcb_info " +
     5             "INNER JOIN product_cover_info cover_info " +
     6             "INNER JOIN product_laserweld_info laserweld_info " +
     7             "INNER JOIN product_acc_info acc_info " +
     8             "INNER JOIN product_press_info press_info " +
     9             "INNER JOIN product_crash_info crash_info " +
    10             "INNER JOIN product_air_info air_info " +
    11             "INNER JOIN product_laserbar_info laserbar_info " +
    12             "INNER JOIN product_reserve1_info reserve1_info " +
    13             "INNER JOIN product_reserve2_info reserve2_info ";
    14 
    15 
    16   string tableWhereStr = "where info.processCode=feed_info.processCode " +
    17                                 "and " +
    18                                 "info.processCode=pcb_info.processCode " +
    19                                 "and " +
    20                                 "info.processCode=cover_info.processCode " +
    21                                 "and " +
    22                                 "info.processCode=laserweld_info.processCode " +
    23                                 "and " +
    24                                 "info.processCode=acc_info.processCode " +
    25                                 "and " +
    26                                 "info.processCode=press_info.processCode " +
    27                                 "and " +
    28                                 "info.processCode=crash_info.processCode " +
    29                                 "and " +
    30                                 "info.processCode=air_info.processCode " +
    31                                 "and " +
    32                                 "info.processCode=laserbar_info.processCode " +
    33                                 "and " +
    34                                 "info.processCode=reserve1_info.processCode " +
    35                                 "and " +
    36                                 "info.processCode=reserve2_info.processCode and ";
    37 
    38  string tmpProcessStr = string.Format("info.processCode='{0}';", processCodeList[i]);
    39 
    40   string tmpSqlStr = productSqlStr + tableWhereStr + tmpProcessStr;
    41 
    42  MySqlCommand tmpCommand = new MySqlCommand(tmpSqlStr, conn);
    43   MySqlDataReader tmpReader = tmpCommand.ExecuteReader();
    44 
    45   while (tmpReader.Read())
    46   {
    47       //rfid数据
    48       //product_info
    49       rfidPairStr = tmpReader.GetString("rfidIdentifier");
    50       userPairStr = tmpReader.GetString("user");
    51       pairFeedNum = tmpReader.GetInt16("feedNum");
    52       pairCarrierNum = tmpReader.GetInt16("carrierNum");
    53 
    54    }
    55 
    56   tmpReader.Close();

    2、将多表联查以后的数据导出cvs文件

     1 string colStr="测试1,测试2,测试3,测试4,测试5";
     2 string[] colStrList = colStr.Split(',');
     3 
     4 //导出csv的表头列名称 
     5 string str = string.Format("select '{0}','{1}','{2}','{3}','{4}' ",
     6                         colStrList[0], colStrList[1], colStrList[2], colStrList[3], colStrList[4]);
     7 
     8 //导出csv表的列数据 
     9 string tableStr = string.Format("UNION SELECT info.processCode,feed_info.barCode1, pcb_info.pressVal, cover_info.pressVal, laserweld_info.subDeg 
    10                           FROM product_info AS info 
    11                           INNER JOIN product_feed_info AS feed_info 
    12                           INNER JOIN product_pcb_info AS pcb_info 
    13                           INNER JOIN product_cover_info AS cover_info 
    14                           INNER JOIN product_laserweld_info AS laserweld_info 
    15                           WHERE info.processCode = feed_info.processCode 
    16                           and 
    17                           info.processCode = pcb_info.processCode 
    18                           and 
    19                           info.processCode = cover_info.processCode 
    20                           and 
    21                           info.processCode = laserweld_info.processCode  ");
    22 
    23 string sqlStr;
    24 string timeStr;
    25 string endStr = string.Format("fields terminated by "," escaped by '' optionally enclosed by '' lines terminated by '\n' ;");
    26 
    27 {//需要挑选日期
    28 timeStr = string.Format("AND info.time BETWEEN '{0}' AND '{1}' LIMIT 10000 ", startTimeStr, endTimeStr);
    29 }
    30 
    31 string outCSVStr = string.Format("INTO OUTFILE '{0}' ", filePath);
    32 
    33 sqlStr = str + tableStr + timeStr + outCSVStr + endStr;
    34 
    35 ExecuteQuery(sqlStr);

    在使用上述代码导出到cvs文件的过程中会发生错误,权限不够,这是你需要修改数据库的相关权限,参照以下连接,修改本地系统账户,停止数据库,在重新运行即可。

    MySQL导出数据到C盘目录时出现(OS errno 13 - Permission denied)异常的解决方案_Voryla的博客-CSDN博客

  • 相关阅读:
    dhtmlTree简单实例以及基本参数设置
    ewebeditor上传文件大小
    Java中集合类
    Ibatis入门基本语法
    afinal 上传文件服务端接受参数为空
    Android的快速开发框架 afinal
    学习asp.net比较完整的流程
    android中sharedPreferences的用法
    mysql怎样配置ODBC数据源
    细谈虚拟主机常见问题
  • 原文地址:https://www.cnblogs.com/wanzaiyimeng/p/15759864.html
Copyright © 2011-2022 走看看