php - Combining Three Tables Output using another to cross-reference -
background
being mysql n00b am, best query can come match 3 tables, run comparison between 2 tables, output variable, , use variable select final output cross reference table. after that, run yet query output third table...
now know there way select rows needed 1 single statement, life of me cannot piece together. can please me structure query need correctly?
i must output result set in php using information 3 tables, , use separate 1 actual link between 2 table's ids. thank you!!
tables
name: table_one ----------------------------------------------------------- id | user_id | o_id | num | likes | dislikes | .... | ... | ----------------------------------------------------------- 1 | 765 | 1 | 100 |android| cats | 2 | 765 | 2 | 100 | birds | mysql queries | 3 | 765 | 3 | 100 | php | iphones | 4 | 765 | 2 | 2 |oranges| bananas | ----------------------------------------------------------- name: table_two ------------------------------------------------------------| id |first_name| location | num_times | diploma | why | ------------------------------------------------------------| 1 | abc | here | 0 | none | because | 2 | bcd | there | 5 | bs | no reason | 3 | sally | | 194384 | dr | no reason | 4 | jack | overthere| 3 | none | failure | 5 | bob | mars | 0 | random | in training | ------------------------------------------------------------- name: table_agency | ---------------------------| id | name | address | ---------------------------| 1 | | 123x | 2 | b | 234y | 3 | c | 456z | ---------------------------- name: table_link ----------------------------- rel_a | rel_b --------------------------------- 1 | 1 | 1 | 4 | 1 | 5 | 2 | 1 | 2 | 4 | 2 | 5 | 3 | 2 | 3 | 3 | 4 | 3 | --------------------------------- output/php
$results = $class->runquery($query); //basically fetchall foreach ($results $result) { echo id_table_one ($result['id']); echo $result['name']; echo $result['num']; echo $result['likes']; echo other_rows...basically table_one.* echo all_first_names&num_times correspond in table link; } //actual printout(echo doesn't have ,'s): //here should output: ------------------------------------------------- 1 | | 100 | android | abc-0/jack-3/bob-0| 2 | b | 100 | birds | abc-0/jack-3/bob-0| 3 | c | 100 | php | bcd-5/sally-194384| 4 | b | 2 | oranges | sally-194384 | ---------------------------------------------- side notes
1) there @ least 1 entry of cross-reference-items correlate first names, maximum of 7 entries (doesn't have in query, reference)
2) there 3 entries of items in table_agency
3) result set must qualified with: user_id=".$variable
4) original query, more or less... since there no commonality between additional information trying pull, forced create function make tie,.... should lend idea of trying achieve:
$query = "select a.*, b.name, b.id agency_id table_one a, table_agency b a.agency_id = b.id , a.user_id = ".$variable; now new addition of first_names correspond $result['name']
5) can use nested foreach( output final result set of ...nested array first_name???
6) please let query(response) commented, may learn hard-work! not attempting solely have question answered, learn on steps , methodology!
7) thank in advance help...this definately headscratcher. thanks!!!
you can output query
select ta.id, ta.name, to.num, to.likes, group_concat(tt.first_name separator '-') `names` table_agency ta left join table_one `to` on to.o_id = ta.id left join table_link tl on tl.rel_a = to.id left join (select id , first_name table_two) tt on tt.id = tl.rel_b to.user_id = 765 group to.id you can replace user_id in php code variable. , abc-0/jack-3/bob-0 can simple replace group_concat(tt.first_name separator '/') names
output
| id | name | num | likes | names | -------------------------------------------- | 1 | | 100 | android | abc-jack-bob | | 2 | b | 100 | birds | bob-abc-jack | | 3 | c | 100 | php | bcd-sally | | 2 | b | 2 | oranges | sally | edits:
here edited query. can use mysql concat function
select ta.id, ta.name, to.num, to.likes, group_concat(tt.first_name separator '/') `names` table_agency ta left join table_one `to` on to.o_id = ta.id left join table_link tl on tl.rel_a = to.id left join ( select id , concat(first_name,'-',num_times) first_name table_two ) tt on tt.id = tl.rel_b group to.id; output
| id | name | num | likes | names | -------------------------------------------------- | 1 | | 100 | android | abc-0/jack-3/bob-0 | | 2 | b | 100 | birds | abc-0/jack-3/bob-0 | | 3 | c | 100 | php | bcd-5/sally-194384 | | 2 | b | 2 | oranges | sally-194384 |
Comments
Post a Comment