php - Permutating Result Set of My Query, Something is wrong -
i have 4 tables:
- persons
- jobs
- titles
- departments
persons table
id | title_id | person | phone | email | dept_id | job_id ---+----------+--------+-------+-------+---------+------- 1 | 1 | some1 | 12345 | s@a.c | 2 | 3 ...
other tables' structures same following
id | ******** ---+---------
assume there 5 records in table titles, 5 records in table departments , 5 job records in table jobs.
and let's suitable records defined $search string = 6 in table persons;
when execute query below, result set has 5 x 5 x 5 x 6 = 750
records , of them repetitive. results doing permutation.
$query = sprintf( "select * persons p, titles t, departments d, jobs j p.person %s or p.phone %s or p.email %s , p.id=t.id , p.id=j.id , p.id=d.id", $search, $search, $search );
then have tried changed conditions following:
$query = sprintf( "select * persons p, titles t, departments d, jobs j p.person %s or p.phone %s or p.email %s , p.title_id=t.id , p.job_id=j.id , p.dept_id=d.id", $search, $search, $search );
this returns same result. want title's instead of title_id, job's instead of job_id , department's name instead of dept_id in result set, @ once.
what doing wrong?
try using join
:
$query = sprintf( "select * persons p join titles t on p.title_id=t.id join departments d on p.dept_id=d.id join jobs j on p.job_id=j.id p.person %s or p.phone %s or p.email %s ", $search, $search, $search );
Comments
Post a Comment