Forum Moderators: open
$query = "Select * from pmstep1,pmstep2,pmstep3,pmstep4,pmstep5,pmusa,pmca
where pmstep1.pmid = pmstep2.pmid = pmstep3.pmid = pmstep4.pmid = pmstep5.pmid = pmca.pmid = pmusa.pmid ='shasha'";
but when i execute it on database using phpmyadmin. or command line to check my system hangs and it start to behave like 386 computers..
Please guide me that how can i select data based on this common coloumn from 7 tables. i never used JOINS and am not sure whether it has to do with joins or not.
thanks in advance
SELECT *
FROM pmstep1,pmstep2,pmstep3,pmstep4,pmstep5,pmusa,pmca
WHERE pmstep1.pmid = 'shasha'
AND pmstep2.pmid = 'shasha'
AND pmstep3.pmid = 'shasha'
AND pmstep4.pmid = 'shasha'
AND pmstep5.pmid = 'shasha'
AND pmca.pmid = 'shasha'
AND pmusa.pmid = 'shasha'; Is how I'd do it. If the query is running slowly you may need to add indexes on the pmid columns.
I'm surprised the database accepted
where pmstep1.pmid = pmstep2.pmid = pmstep3.pmid = pmstep4.pmid = pmstep5.pmid = pmca.pmid = pmusa.pmid ='shasha'
Select *
from pmstep1
INNER JOIN pmstep2
ON pmstep1.pmid = pmstep2.pmid
INNER JOIN pmstep3
ON pmstep2.pmid = pmstep3.pmid
INNER JOIN pmstep4
ON pmstep3.pmid = pmstep4.pmid
INNER JOIN pmstep5
ON pmstep4.pmid = pmstep5.pmid
INNER JOIN pmusa
ON pmstep5.pmid = pmusa.pmid
INNER JOIN pmca
ON pmusa.pmid = pmca.pmid
AND pmusa.pmid ='shasha'
it is working for me with one problem, it selects PMID col from all the tables againa n again, i used GROUP BY PMID but it gives error.
anyway idea?
for instance a name table for id's.
i.e.
select
(select name from table1 where pid=t1.gid) as Group_name,
(select name from table2 where pid=t1.id) as item_name
item2,
item3
from table1 t1 , table2 t2, table3 t3
where t1.id=t2.t1id and t1.id=t3.t1id;
Note, that if you have a single driving table you only need to join fields to that table and not crossreference to every single table you are selecting from..
Regards
** edit **
Moving one-to-one joins to the select is a very nice way to optimize your query. since it removes extra analysis from the rdbms engine.