Question

I am facing a strange comparision error for a varchar field in MySQL. I have two tables as below:

CREATE TABLE IF NOT EXISTS tmpMntYrTbl(
  sfr_value INTEGER,                    
  sfr_month VARCHAR(25)
);

CREATE TEMPORARY TABLE IF NOT EXISTS tmpTbl(                           
  sfr_responsive_avg INTEGER,                    
  sfr_month VARCHAR(25)
);

The content of these are as follows:

sfr_value   sfr_month
0   Jan`12
0   Feb`12
0   Mar`12
0   Apr`12
0   May`12
0   Jun`12
0   Jul`12
0   Aug`12
0   Sep`12
0   Oct`12
0   Nov`12
0   Dec`12
0   Jan`13
0   Feb`13
0   Mar`13
0   Apr`13
0   May`13
0   Jun`13
0   Jul`13
0   Aug`13
0   Sep`13
0   Oct`13
0   Nov`13
0   Jan`12
0   Feb`12
0   Mar`12
0   Apr`12
0   May`12
0   Jun`12
0   Jul`12
0   Aug`12
0   Sep`12
0   Oct`12
0   Nov`12
0   Dec`12
0   Jan`13
0   Feb`13
0   Mar`13
0   Apr`13
0   May`13
0   Jun`13
0   Jul`13
0   Aug`13
0   Sep`13
0   Oct`13
0   Nov`13

and

sfr_responsive_avg  sfr_month
20  Aug`12
11  Oct`12
11  Nov`12
11  Dec`12
19  Jan`13
18  Mar`13
12  Apr`13
16  May`13
5   Jun`13
6   Jul`13

Now I am trying to copy the sfr_responsive_avg values of temporary table tmpTbl to tmpMntYrTbl table so I am doing

UPDATE tmpMntYrTbl SET sfr_value= (SELECT sfr_responsive_avg FROM tmpTbl)
                   WHERE tmpTbl.sfr_month = tmpMntYrTbl.sfr_month;

but it did not work, the content are not copied.

Also i tried something like below.., but that too didnt work.

 SELECT COUNT(sfr_month) AS rowval, sfr_month AS Mnth, sfr_responsive_avg AS val, MONTH(sfr_month) AS MnthNo, YEAR(sfr_month) AS YrNo 
FROM tmpTbl 
WHERE sfr_month = mth_str;
SELECT sfr_month AS Mnth, sfr_responsive_avg AS val, MONTH(sfr_month) AS MnthNo, YEAR(sfr_month) AS YrNo 
FROM tmpTbl 
WHERE sfr_month = mth_str LIMIT 1;

SELECT  * FROM tmpMntYrTbl;

 IF  rowval THEN
INSERT INTO tmpMntYrTbl VALUES(val,mth_str);
 ELSE
    INSERT INTO tmpMntYrTbl VALUES(rowval,mth_str);
 END IF;

So it appears that the issue is in varchar column , where it compare and fails. Why? Any clue?

Was it helpful?

Solution

Use multiple table update:

UPDATE tmpMntYrTbl AS t1,tmpTbl AS t2
SET t1.sfr_value=t2.sfr_responsive_avg
WHERE t1.sfr_month=t2.sfr_month;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top