Getting duplicate result while joining tables
When im using Select statement for displaying data from Mysql Db. Im getting Duplicate Values. How to Solve this issue? My sql Query is this:
$sql="SELECT r.hosteladmissionno,r.student_name,r.semester,
m.billmonth,m.billyear,m.wastagecharge,b.exp_amount
FROM registration r, messexp m, blockexp开发者_C百科 b
WHERE r.mess_type = '".$q."' AND r.mess_type=m.messtype AND r.status_flag=1";
You should use inner join in you query. Else each result of registration will be combined with each result of messexp ...
$sql="SELECT registration.hosteladmissionno,
registration.student_name,
registration.semester,
messexp.billmonth,
messexp.billyear,
messexp.wastagecharge,
blockexp.exp_amount
FROM registration
INNER JOIN messexp ON (messexp.id_registration = registration.id)
INNER JOIN blockexp ON (blockexp.id_messexp = messexp.id)
WHERE
registration.mess_type = '".$q."' AND status_flag=1";
Please note that ON (...) will change depending on you schema
You have cross-joined the tables, so you get every combination of results. Connect the tables in the where clause:
select tab1.column1, table2.column1
from tab1, tab2
where tab1.fkColumn = tab2.idColumn
What are the columns that connect the tables to each other?
精彩评论