相同的存储过程,相同的表,不同的DB会产生不同的结果

时间:2018-02-18 15:59:48

标签: mysql sql

我有一个存储过程,当我在MySQL 5上运行它时,它会给我预期的结果。但是当我尝试在MariaDB 10.1.22上运行相同的过程时,它给了我不同的结果。

这是我的存储过程 -

    DELIMITER ;;
    CREATE DEFINER=`mconnect_admin`@`%` PROCEDURE `TestCumulative`(IN 
    start_date TIMESTAMP,IN end_date TIMESTAMP,IN duration TEXT,IN 
    mno_id TEXT,IN profile_type TEXT,IN timezone TEXT)

    BEGIN
    SET @provisioned = 0;
    SET @downloaded = 0;

    SET @excludeProfileFilter = FALSE;
    SET @hourlyReport = FALSE;
    SET @monthlyReport = FALSE;
    SET @sdate = start_date;

    DROP TABLE IF EXISTS tempDates;
    CREATE TEMPORARY TABLE IF NOT EXISTS tempDates(timeRange 
    VARCHAR(50));
    DELETE FROM tempDates;

    IF(profile_type = '')
       THEN
       SET profile_type = null;
       SET @excludeProfileFilter = TRUE;
    END IF;

    IF (duration = 'lastDay')
      THEN
      SET @hourlyReport=true;
    END IF;

    IF (duration = 'lastYear')
      THEN
      SET @monthlyReport = TRUE;
    END IF;

    WHILE @sdate <= end_date DO
       IF (@hourlyReport = TRUE)
         THEN
         INSERT INTO tempDates (timeRange) VALUES (HOUR(@sdate) + 1);
         SET @sdate = date_add(@sdate, INTERVAL 1 HOUR); 
       ELSE IF(@monthlyReport = TRUE)
         THEN
         INSERT INTO tempDates (timeRange) VALUES (MONTH(@sdate));
         SET @sdate = date_add(@sdate, INTERVAL 1 MONTH);
       ELSE
         INSERT INTO tempDates (timeRange) VALUES (DATE(@sdate));
         SET @sdate = date_add(@sdate, INTERVAL 1 DAY);
       END IF;
       END IF;
   END WHILE ;

   SELECT  CASE WHEN r.DateRange IS NULL THEN (@provisioned := 
   @provisioned) ELSE (@provisioned := @provisioned + r.Provisioned) 
   END AS Provisioned,
   CASE WHEN r.DateRange IS NULL THEN (@downloaded := @downloaded) ELSE 
   (@downloaded := @downloaded + r.Downloaded) END AS Downloaded, 
   CASE WHEN r.DateRange IS NULL THEN d.timeRange ELSE r.DateRange END 
   AS DateRange FROM (
   SELECT sum(result.Provisioned) as Provisioned, 
   sum(result.Downloaded) AS Downloaded, result.DateRange FROM (

   SELECT 
   1 As Provisioned, 
   0 AS Downloaded,
   CASE WHEN @hourlyReport=TRUE 
        THEN HOUR(CONVERT_TZ(s.provisioning_date,"+00:00",timezone)) 
        WHEN @monthlyReport=TRUE
        THEN MONTH(CONVERT_TZ(s.provisioning_date,"+00:00",timezone)) 
        ELSE DATE(CONVERT_TZ(s.provisioning_date,"+00:00",timezone))
    END     
    AS DateRange 

   FROM subscription s 
   INNER JOIN profile_type p ON p.id = s.profile_type 
   WHERE s.mno_id = mno_id
   AND (@excludeProfileFilter=true or p.display_name=profile_type OR p.subscription_type=profile_type)
   AND DATE(CONVERT_TZ(s.provisioning_date,"+00:00",timezone)) BETWEEN DATE(CONVERT_TZ(start_date,"+00:00",timezone)) AND DATE(CONVERT_TZ(end_date,"+00:00",timezone))

UNION ALL

SELECT 
    0 As Provisioned, 
    1 As Downloaded,

    CASE WHEN @hourlyReport=TRUE 
        THEN COALESCE(HOUR(CONVERT_TZ(r.end_download_date,"+00:00",timezone)),HOUR(CONVERT_TZ(r.last_update,"+00:00",timezone))) 
        WHEN @monthlyReport=TRUE
        THEN COALESCE(MONTH(CONVERT_TZ(r.end_download_date,"+00:00",timezone)),MONTH(CONVERT_TZ(r.last_update,"+00:00",timezone)))
        ELSE COALESCE(DATE(CONVERT_TZ(r.end_download_date,"+00:00",timezone)),DATE(CONVERT_TZ(r.last_update,"+00:00",timezone)))
    END
    AS DateRange 

   FROM subscription s 
   INNER JOIN profile_type p ON p.id = s.profile_type 
   LEFT JOIN rsp_session r ON r.profile_iccid = s.iccid 
   WHERE s.mno_id = mno_id
   AND (@excludeProfileFilter=TRUE OR p.display_name=profile_type OR p.subscription_type=profile_type)
   AND COALESCE(DATE(CONVERT_TZ(r.end_download_date,"+00:00",timezone)),DATE(CONVERT_TZ(r.last_update,"+00:00",timezone))) BETWEEN DATE(CONVERT_TZ(start_date,"+00:00",timezone)) AND DATE(CONVERT_TZ(end_date,"+00:00",timezone)) AND s.status IN('INSTALLED','ENABLED','DELETED')

) result GROUP BY result.DateRange
) r RIGHT OUTER JOIN tempDates d ON r.DateRange = d.timeRange;

   END;;
   DELIMITER ;

我在MaroiaDB 10.1.22中得到的结果是不正确的 -

2   0   2017-11-02
5   10  2017-11-03
32  16  2017-11-06
51  34  2017-11-07
64  42  2017-11-08
79  47  2017-11-09
79  48  2017-11-10
102 61  2017-11-13
116 61  2017-11-14
128 68  2017-11-15
145 71  2017-11-16
157 82  2017-11-17
196 95  2017-11-20
254 111 2017-11-21
273 118 2017-11-22
313 134 2017-11-23
323 144 2017-11-24
363 149 2017-11-27
368 152 2017-11-28
371 152 2017-11-29
403 160 2017-11-30
403 160 2017-11-01
403 160 2017-11-04
403 160 2017-11-05
403 160 2017-11-11
403 160 2017-11-12
403 160 2017-11-18
403 160 2017-11-19
403 160 2017-11-25
403 160 2017-11-26

它应该是有序的。

谁能说出错是什么意思?还是它的数据库问题?

提前致谢。

1 个答案:

答案 0 :(得分:1)

您的任何查询都没有for i in range (4513,5001): url="https://...{pagenum}.....xml".format(pagenum=i) downloaded = False while(not downloaded): response=requests.get(url, verify=False) soup=BeautifulSoup(response.text) g_data=soup.find_all("td",{"class":"detail_1"}) if not g_data: print("List is empty") continue else: downloaded = True results=[] print (i) for item in g_data: results.append(item.text) df=pd.DataFrame(np.array(results).reshape(20,7),columns=list("abcdefg")) excel_reader=pd.ExcelFile('test6.xlsx') to_update={"Sheet1":df} excel_writer=pd.ExcelWriter('test6.xlsx') for sheet in excel_reader.sheet_names: sheet_df=excel_reader.parse(sheet) append_df=to_update.get(sheet) if append_df is not None: sheet_df=pd.concat([sheet_df,df]).drop_duplicates() sheet_df.to_excel(excel_writer,sheet,index=False) excel_writer.save() 。结果集可以是任何顺序,因为没有order by的结果集(如表)是无序集。

因此,数据库是正确的。您的理解缺少关于SQL的这一重要事实。

添加所需的order by,并在您使用的任何版本的数据库中正确排序结果集。