mysql join没有响应

时间:2011-12-08 21:37:20

标签: mysql sql join

我有两个表,donaTypesfullInfo

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

我正在尝试匹配fullInfo.funddesc = donaTypes.donaType的位置,希望将donaTypes.id数字插入fullInfo表。这是我的代码,但我得到一个空白的响应(没有错误):

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 个答案:

答案 0 :(得分: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),'<<<')