2011-12-08 24 views
0

我有兩個表,donaTypesfullInfomysql連接得不到迴應

fullInfo

id   funddesc  giftamt  giftdate 
001  annual fund  50.00   2010-03-09 
223  alumni fund  25.00   2009-03-06 
334  capital exp  100.00   2011-09-27 
...   ...   ...    ... 

donaTypes

id   donaType 
    1   annual fund 
    2   capital exp 
    3   alumni fund 

我試圖用插入donaTypes.id數到fullInfo表的希望相匹配,其中fullInfo.funddesc = donaTypes.donaType。這裏是我的代碼,但我只是得到了一個空白響應(沒有錯誤):

SELECT st1.funddesc, st2.donatype 
FROM 

(select t1.funddesc 
from fullInfo as t1) st1 

inner join 

(select t2.donatype 
from donatTypes as t2) st2 

on trim(st1.funddesc) like trim(st2.donaType) 
; 

我也試過:

SELECT t1.funddesc, t2.donatype 
FROM fullInfo as t1, 
donatTypes as t2 
where trim(t1.funddesc) = trim(t2.donatype); 

理想情況下,我想fullInfo看起來像這樣:

fullInfo

id   funddesc  giftamt  giftdate 
001    1   50.00   2010-03-09 
223    3   25.00   2009-03-06 
334    2   100.00   2011-09-27 
...   ...   ...    ... 
+1

您是否驗證過funddesc和donatype之間的數據類型是否相同? –

+1

應該工作,數據類型和相同。是否可能存在一些不可見字符(例如CR/LF)。我建議從每個表格中獲取兩個字段的長度(),以確保文本包含您的想法。 – Sparky

+0

是的,它們都是varchar。 – screechOwl

回答

2

保持它更簡單直到你得到調試。你不需要嵌套查詢。而且,LIKE對於連接並不是很好,因爲它可能有點混雜。

SELECT fi.funddesc, dt.donaType 
    FROM fullinfo fi 
    JOIN donatTypes dt on trim(fi.funddesc) = trim(dt.donaType) 

你也可能想在你的兩張桌子上做這種事情,只是爲了弄清楚你實際上在你的連接列中有什麼樣的東西。

SELECT COUNT(*), concat('>>>',TRIM(funddesc),'<<<') 
    FROM fullinfo 
GROUP BY concat('>>>',TRIM(funddesc),'<<<')