内连接只使用like子句而不是两个返回1行

时间:2012-06-08 13:07:41

标签: mysql

我有下面的sql,如果我取出内连接到表并硬编码值,我得到2条记录。但是当我使用内部连接语句时,我只获得1条记录。我知道使用MYSQL,like子句不区分大小写。这是下面的sql。我不知道为什么它只是返回行。

SELECT 
      a.project_code as projectCode, 
      a.project_name as projectName, 
      a.project_creation_date as projectCreationDate, 
      a.project_end_date as projectEndDate, 
      a.project_status as projectStatus      
from projects a 
inner join tmp_rec_naren8 b 
    on a.project_name like concat("%",b.expertise_desc,"%") 
    OR a.project_description like concat("%",b.expertise_desc,"%") ;

如果我将上述内容更改为:

select *
from projects
where project_name like '%java%'
OR project_description like '%java%';

我得到2行而不是1.

tmp_rec_naren8只有一列expertise_desc并包含以下数据:

expertise_desc
--------------
XML
Python
DWH
Java

projects有很多列:..但以下是相关的

    project_code sr_user_name project_name project_descriiption ..
    -----------------------------------------------------------
    1             naren5      POS          C++, XML,Java
    2             naren7      INV          Networking
    3             naren9      CCV          Java, Unix         

第一个查询返回1行:

    project_code sr_user_name project_name project_descriiption ..
    ---------------------------------------------------------------
     1            naren5       POS         C++,XML,Java

第二个查询返回2行

    project_code sr_user_name project_name project_descriiption ..
    -----------------------------------------------------------
    1             naren5      POS          C++, XML,Java
    3             naren9      CCV          Java, Unix         

3 个答案:

答案 0 :(得分:0)

一种解释是,expertise_desc中的值是否包含尾随空格,即'Java '(不是'Java')。然后,concat值将是'%java %',而不是'%java%',这会产生影响。

如果您的某个项目名称​​以java结束,而另一个项目名称 名称,那么只有一个项目名称匹配加入,但两个将与非联接匹配。

答案 1 :(得分:0)

您可以尝试以下方法:

SELECT 
      a.project_code as projectCode, 
      a.project_name as projectName, 
      a.project_creation_date as projectCreationDate, 
      a.project_end_date as projectEndDate, 
      a.project_status as projectStatus      
from projects a, tmp_rec_naren8 b
where a.project_name like concat("%",b.expertise_desc,"%") 
    OR a.project_description like concat("%",b.expertise_desc,"%") ;

答案 2 :(得分:0)

试一试:

SELECT        
a.project_code as projectCode,        
a.project_name as projectName,        
a.project_creation_date as projectCreationDate,        
a.project_end_date as projectEndDate,        
a.project_status as projectStatus       
from projects,tmp_rec_naren8
where LOCATE(TRIM(tmp_rec_naren8.expertise_desc),projects.project_name) > 0
or
LOCATE(TRIM(tmp_rec_naren8.expertise_desc),projects.project_description) > 0