mysql——sql语句

            sql_module_base = f"""
                            SELECT
                                COUNT(distinct id,date_format(creat_time, ‘%Y-%m_%d %H:%M‘)) as count,
                                shift,
                                MIN( creat_time ) as creat_time,
                                YEAR ( creat_time ) AS year,
                                MONTH ( creat_time ) AS month,
                                DAY ( creat_time ) AS day,
                                (HOUR ( creat_time ) DIV 4) AS hour 
                            FROM
                                Module_{station_id} 
                                """
            sql_module_ng_base = f"""
                                SELECT
                                    COUNT( distinct id,date_format(creat_time, ‘%Y-%m_%d %H:%M‘) ) as count,
                                    shift,
                                    YEAR ( creat_time ) AS year,
                                    MONTH ( creat_time ) AS month,
                                    DAY ( creat_time ) AS day,
                                    HOUR ( creat_time ) DIV 4 AS hour 
                                FROM
                                    Module_{station_id}
                                """
            sql_module_groupby = f" GROUP BY YEAR ( creat_time ),MONTH ( creat_time ),DAY ( creat_time ),HOUR ( creat_time ) DIV {p},shift "
            sql_defect_base = f"""
                            SELECT
                                Module_{station_id}.id,
                                Module_{station_id}.serial_id,
                                Module_{station_id}.shift,
                                Module_{station_id}.creat_time,
                                defect_ai_{station_id}.ROW,
                                defect_ai_{station_id}.col,
                                defect_ai_{station_id}.NAME,
                                YEAR ( Module_{station_id}.creat_time ) as year,
                                MONTH ( Module_{station_id}.creat_time ) as month,
                                DAY ( Module_{station_id}.creat_time ) as day,
                                HOUR ( Module_{station_id}.creat_time ) as hour 
                            FROM
                                defect_ai_{station_id}
                                LEFT JOIN Module_{station_id} ON defect_ai_{station_id}.module_id = Module_{station_id}.id 
                         """
            # sql_defect_groupby = f"GROUP BY Module_{station_id}.id,Module_{station_id}.creat_time"
            sql_ng_where = " AND is_ng_ai = 1 "

            sql_shift_where = f" AND Module_{station_id}.shift LIKE ‘%{shift_q[0]}‘"
            if start_time and end_time:
                sql_where_defect = f" WHERE defect_ai_{station_id}.creat_time >= ‘{start_time}‘ AND defect_ai_{station_id}.creat_time < ‘{end_time}‘ AND is_ng_ai = 1 "
                sql_where_module = f" WHERE creat_time >= ‘{start_time}‘ AND creat_time < ‘{end_time}‘"
            else:
                sql_where_defect = ‘‘
                sql_where_module = ‘‘

            sql_defect = sql_defect_base + sql_where_defect
            sql_module = sql_module_base + sql_where_module + sql_module_groupby
            sql_module_ng = sql_module_ng_base + sql_where_module + sql_ng_where + sql_module_groupby

            f_defect = pd.read_sql(sql_defect, con=connections[‘default‘])
            f_module = pd.read_sql(sql_module, con=connections[‘default‘])
            f_module_ng = pd.read_sql(sql_module_ng, con=connections[‘default‘])

mysql——sql语句

上一篇:转-squid介绍及其简单配置


下一篇:什么是NoSQL