$query="SELECT * FROM candidates INNER JOIN approvals
ON candidates.cid = approvals.cid
WHERE agency_code='$agency_code'
UNION
SELECT * FROM candidates_ans INNER JOIN approvals_ans
ON candidates_ans.cid = approvals_ans.cid
WHERE agency_code='$agency_code'";
if ($result=mysql_query($query) or die (mysql_error()));
while ($row=mysql_fetch_array($result)){
xxxxxx
}
I want to know the table name currently the while loop is fetching records
You need to add that information as a constant in the select list.
select <columnsOfInterest>,
'Candidates' as origin
from candidates
join approvals
on candidates.cid = approvals.cid
where agency_code = '$agency_code'
union all
select <columnsOfInterest>,
'Candidates_ans'
from candidates_ans
join approvals_ans
on candidates_ans.cid = approvals_ans.cid
where agency_code = '$agency_code
You can use union all which avoids the need for attempting to eliminate any duplicates in the result set.
Hai Swampboogi,
thanks for the reply, ill look at ur solution.
mean time on google i found a similler tem table solution. but know idea how to extract the table name while fetching.
$query="SELECT * FROM candidates as tbl_candidates INNER JOIN approvals
ON tbl_candidates.cid = approvals.cid
WHERE agency_code='$agency_code'
UNION
SELECT * FROM candidates_ans as tbl_candidates_ans INNER JOIN approvals_ph_ans
ON tbl_candidates_ans.cid = approvals_ph_ans.cid
WHERE agency_code='$agency_code'";
$query="SELECT 'cid' as cid1,'position' as position1,'name' as name1,'dob' as dob1,'phone' as phone1,'education' as education1,'experience' as experience1,'salary' as salary1,'date' as date1
FROM candidates INNER JOIN approvals
ON candidates.cid = approvals.cid
WHERE agency_code='$agency_code'
UNION
SELECT 'cid' as cid2,'position' as position2,'name' as name2,'dob' as dob2,'phone' as phone2,'education' as education2,'experience' as experience2,'salary' as salary2,'date' as date2
FROM candidates_ans as tbl_candidates_ans INNER JOIN approvals_ans
ON tbl_candidates_ans.cid = approvals_ans.cid
WHERE agency_code='$agency_code'";
if ($result=mysql_query($query) or die (mysql_error()));
$tot=mysql_num_rows($result);
while ($row=mysql_fetch_array($result)){
echo $row['cid1'];
echo $row['cid2'];
echo "<br>";
}
sorry, in my prvious post, the second table query after union is little messed up. here is the correct one
$query="SELECT 'cid' as cid1,'position' as position1,'name' as name1,'dob' as dob1,'phone' as phone1,'education' as education1,'experience' as experience1,'salary' as salary1,'date' as date1,'candidates' [B]as tbl1[/B]
FROM candidates INNER JOIN approvals
ON candidates.cid = approvals.cid
WHERE agency_code='$agency_code'
UNION
SELECT 'cid' as cid2,'position' as position2,'name' as name2,'dob' as dob2,'phone' as phone2,'education' as education2,'experience' as experience2,'salary' as salary2,'date' as date2,'candidates_ans' [B]as tbl2 [/B]
FROM candidates_ans INNER JOIN approvals_ph_ans
ON candidates_ans.cid = approvals_ph_ans.cid
WHERE agency_code='$agency_code'";
}
Thanks swamboogi,
now it works fine!!! great lesson!
Get rid of the quotes around the column names, or use backticks instead. Using quotes makes that ‘cid’ (for example) is considered a string instead of a column name.
The columns in query result will have the names from the first select. So it’s useless to give them other names in the select after the UNION. For example, the first column will be called cid, and not cid2 (a column can’t have two names).
@swampBoogie; in this case UNION ALL isn’t necessary, because adding the table name in a column, no rows from the two tables will be identical
SELECT 'cid' as cid1,'candidates' as tbl1
FROM candidates
INNER JOIN approvals
ON candidates.cid = approvals.cid
WHERE agency_code='$agency_code'
UNION
SELECT 'cid' as cid1,'candidates_ans' as tbl2
FROM candidates_ans
INNER JOIN approvals_ans
ON candidates_ans.cid = approvals_ans.cid
WHERE agency_code='$agency_code'";
mysql num rows shows only 1 matching record for above.This is false.
because, if i executecute the above query with only the first SELECT (without union and below that), it produces 12 results which is correct. whats wrong?
I know that there will be no duplicates in the result. The union all is intended as a hint to the optimizer that there is no need to eliminate duplicates.
i am now able to see the correct totals by fixing the following.
Ur suggestions
*remove quotes from tables name
and
*union all are helped to solve the problem.
Thank you for the great help
SELECT candidates.cid as cid1,'candidates' as tblname
FROM candidates
INNER JOIN approvals
ON candidates.cid = approvals.cid
WHERE agency_code='$agency_code'
UNION ALL
SELECT candidates_ans.cid as cid1,'candidates_ans' as tblname
FROM candidates_ans
INNER JOIN approvals_ph_ans
ON candidates_ans.cid = approvals_ph_ans.cid
WHERE agency_code='$agency_code'";
small note : with and without union it works in my case.tx.
last updated code
SELECT candidates.cid as cid1,'candidates' as tblname
FROM candidates
INNER JOIN approvals
ON candidates.cid = approvals.cid
WHERE agency_code='$agency_code'
UNION
SELECT candidates_ans.cid as cid1,'candidates_ans' as tblname
FROM candidates_ans
INNER JOIN approvals_ph_ans
ON candidates_ans.cid = approvals_ph_ans.cid
WHERE agency_code='$agency_code'";