3个表的内部连接给出重复的结果

时间:2014-04-11 04:34:15

标签: php mysql join

我有以下3个表结构(未提及allcolumns,仅需要)

user_master

  

user_id(主键)

     

FNAME

雇员

  

employee_id(主键)

     

user_id(外键)

     

地产

studexperience_master

  

exp_id(主键)

     

user_id(外键)

我有以下2个mysql查询,从3个表中获取数据,并在1个表中显示表单display(以下查询用于搜索结果)。前两个表正在正常连接但问题是加入下两个表时( ie employee和studexperience_master)数据被重复获取,如果相同的user_id比$ result结果集中的getttin 3行多3次,我应该只有1行,我试过DISTINCT ALSO FOR SECOND INNER JOIN BUT ISSUE NOT GETTING SOLVE ,HOW THE QUERY SHOULD BE?

$get_work=mysql_query("SELECT DISTINCT wm.work,wm.status FROM user_master um 
INNER JOIN employee emp ON um.user_id =emp.user_id 
INNER JOIN studexperience_master wm 
ON emp.user_id=wm.user_id 
WHERE wm.display='Y' $status_val $company_val $searchkind_val $location_val 
$search_Qualification_val");

        SELECT um.user_id,
       um.fname,
       um.lname,
       um.email,
       um.mobile,
       emp.employee_id,
       emp.ecountry,
       emp.estate,
       emp.ecity,
       emp.prefcities,
       emp.ca,
       emp.cs,
       emp.cwa,
       emp.completed,
       emp.persuing
FROM user_master um
INNER JOIN employee emp 
ON um.user_id =emp.user_id
INNER JOIN studexperience_master wm 
ON emp.user_id=wm.user_id
WHERE wm.display='Y' $status_val 
$company_val $searchkind_val $location_val 
$search_Qualification_val

和where子句值属于这样的值

WHERE wm.display='Y' AND status LIKE '%Fresher%' AND efamiliar LIKE '%Tally %' AND work LIKE 'Excise Audit' AND ecountry LIKE 'pune' OR ecity LIKE 'pune' OR estate LIKE 'pune' OR prefcities LIKE 'pune' AND ca REGEXP '%|CA CPT%' OR cs REGEXP '%|CA CPT%' OR cwa REGEXP '%|CA CPT%' OR completed REGEXP '%|CA CPT%' OR persuing REGEXP '%|CA CPT%'

I have two user in user master and there respective work in third table ,Now for monika there are 2 rows and for dinesh 3 rows (in third table) so getting total 5 rows which should be only 2 and also in last column I am getting displayed all work of "work column" in table ,which should be only respective works of particular user

user_master

  

user_id- 2

     

fname- monika

     

mobile- 2147 ......

     

email- a@a.b

员工

   >employee_id- 1

    >user_id- 2

    >ecity- Amravati

    >ca- CA IPCC Appeared

    >cs- CS Executive Appeared

    >cwa- CWA Inter Appeared   

studexperience_master

ROW1

  

exp_id-1

     

user_id- 2

     

工作 - 税务审计

ROW2

  

exp_id-2

     

user_id- 2

     

工作 - 增值税审计

3 个答案:

答案 0 :(得分:0)

只需更改下面的两个查询

不是加入emp.user_id加入um.user_id,而是加入PK并且应该拥有唯一数据。

SELECT DISTINCT wm.work,wm.status  
FROM user_master um  
INNER JOIN employee emp  
ON um.user_id =emp.user_id  
INNER JOIN studexperience_master wm  
ON um.user_id=wm.user_id and wm.display='Y' 

第二次查询,如

   SELECT um.user_id,
   um.fname,
   um.lname,
   um.email,
   um.mobile,
   emp.employee_id,
   emp.ecountry,
   emp.estate,
   emp.ecity,
   emp.prefcities,
   emp.ca,
   emp.cs,
   emp.cwa,
   emp.completed,
   emp.persuing
FROM user_master um
INNER JOIN employee emp 
ON um.user_id =emp.user_id
INNER JOIN studexperience_master wm 
ON um.user_id=wm.user_id and wm.display='Y'

虽然我无法理解where这些变量的含义是什么?你的where对我来说很奇怪。

WHERE wm.display='Y' $status_val $company_val $searchkind_val $location_val 
$search_Qualification_val

答案 1 :(得分:0)

尝试以下查询:

SELECT distinct um.user_id,
       um.fname,
       um.lname,
       um.email,
       um.mobile,
       emp.employee_id,
       emp.ecountry,
       emp.estate,
       emp.ecity,
       emp.prefcities,
       emp.ca,
       emp.cs,
       emp.cwa,
       emp.completed,
       emp.persuing
FROM user_master um
INNER JOIN employee emp 
ON um.user_id =emp.user_id
INNER JOIN 
(SELECT DISTINCT user_id FROM studexperience_master WHERE <your ALL conditions ON this TABLE>) wm 
ON emp.user_id=wm.user_id
WHERE $status_val 
$company_val $searchkind_val $location_val 
$search_Qualification_val;

答案 2 :(得分:0)

你可以试试这样的东西。如果学生体验主表有重复项,那么只需尝试获取前一条记录(根据某些条件,您可以根据需要为此记录执行过滤器)。

Select * from user_master UM
inner join employee EMP on UM.UserId = EMP.UserId
inner join studexperience_master USM on EMP.UserId = (SELECT  TOP 1 UserId FROM studexperience_master RJK WHERE USM.UserId = RJK.UserId)

如果有任何问题或需要任何帮助,请告诉我。,:))