Internal join of 3 tables giving duplicate results

I have the following table structure (not all columns specified, only required)

user_master

user_id (primary key)

file name

employee

employee_id (primary key)

user_id (foreign key)

The property

studexperience_master

exp_id (primary key)

user_id (foreign key)

I have 2 mysql queries, fetching data from 3 tables and displaying the form in 1 table (the following queries to search). The first two tables are joined properly, but the problem is that when the next two tables are joined (i.e. employee and studexperience_master), the data is retrieved if the same user_id is said 3 times there than getttin 3 rows in $ result set, which should be only 1 line, I triedDISTINCT 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

and where the sentence values ​​belong to values ​​like this

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

work-Tax AUDIT

ROW2

exp_id-2

user_id- 2

work - VAT AUDIT

+4
3

,

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
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;
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)

, - .,:)

0
source

Source: https://habr.com/ru/post/1536045/


All Articles