Mysql JOIN of four tables with two key tables -
i hate admit knowledge of mysql lacking when comes more complex queries. have 4 tables 2 of them contain data want return, , 2 relational tables linking data. table a present provide filler table d.aid.
+--------+ +--------+ +--------+ +-----------+ +-----------+ |table | |table b | |table c | | table d | | table e | +---+----+ +---+----+ +---+----+ +---+---+---+ +---+---+---+ |aid|name| |bid|name| |cid|name| |did|aid|bid| |eid|did|cid| +---+----+ +---+----+ +---+----+ +---+---+---+ +---+---+---+ | 1 | a_1| | 1 | b_1| | 1 | c_1| | 1 | 1 | 1 | | 1 | 1 | 1 | +---+----+ | 2 | b_2| | 2 | c_2| | 2 | 1 | 2 | | 1 | 1 | 2 | +---+----+ | 3 | c_3| +---+---+---+ +---+---+---+ +---+----+ the relationship created these tables is: table > table b > table c. data wanting belongs table b > table c relationship.
+--------+---------+--------+---------+ |tblb.bid|tblb.name|tblc.cid|tblc.name| +--------+---------+--------+---------+ | 1 | a_1 | 1 | c_1 | | 1 | a_1 | 2 | c_2 | | 2 | a_2 | null | null | +--------+---------+--------+---------+ however ensure following correct path need grab table b of table > table b relationship table c belongs to. realize making things more difficult myself allowing duplicate name values, rather have small tables , more complex queries bloated tables , simpler queries. query using is
select * `table e` left join `table d` on (`table b`.bid = `table d`.bid) right join `table e` on (`table d`.did = `table e`.did)) right join `table c` on (`table e.cid = `table c`.cid); however far has not worked. when query submitted error returned:
error 1066 (42000): not unique table/alias: 'table d' any ideas on how can work? possible?
the query submitting bears little resemblance table structure have given us! table d.national_regionid? or modx.coverage_state?
generally though don't mix left , right joins. every table used in query must either follow from or follow join. seem using table b , table c in join conditions without ever adding them query.
Comments
Post a Comment