943,660 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Marked Solved
  • Views: 4857
  • MySQL RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 19th, 2008
0

Pulling related data from relational dB re: thread: Problems with a many-to-many inse

Expand Post »
OK, now that I managed to insert data properly into a relational database using a linking table, I am now faced with pulling the data properly out of the database, a displaying it so it makes sense. As a recap, I have inserted user data into a data base that included the usual name address, phone, email etc, as well as the fact that they have access to various airports and at each airport, access to one to three services, which are the same for every airport. ( see my previous thread called Problems with a many-to-many insert for more details ).

I have written the following SQL statement, which does indeed get all of the airports and services that a particular user has access to but I need to get them out in a related manner. For example, the user has access to airport YVR and has access to services Jet and Ground at this airport. He also has access to airport YYZ and at this airport, he has access to services Jet Ground and Glycol. Here is the SQL:

MySQL Syntax (Toggle Plain Text)
  1. SELECT airport.airport, service.service, userairportservices.usr_id_users
  2. FROM airport, service, userairportservices
  3. WHERE userairportservices.usr_id_users = usr_id AND service.service_id = userairportservices.service_id_service AND airport.airport_id = userairportservices.airport_id_airport

And here is the info it pulls out: [ attached screen grab ]

So, I need to figure out a way to put this back into the page for display [ see second image for example of page ]. Obviously, it is being displayed in two forms: one as a non-editable details display and two, as an editable form for updating the record. So th image merely gives you an example of how I want to display the relationship between airport and services.

As always, I appreciate any insight and help.

Dave
Attached Thumbnails
Click image for larger version

Name:	Picture 1.png
Views:	22
Size:	36.3 KB
ID:	8250   Click image for larger version

Name:	test_insert_form.02.png
Views:	19
Size:	71.3 KB
ID:	8251  
Reputation Points: 10
Solved Threads: 1
Junior Poster
filch is offline Offline
132 posts
since Nov 2008
Nov 19th, 2008
0

Re: Pulling related data from relational dB re: thread: Problems with a many-to-many inse

lol oh filch

this is where the dynamic piece would have helped on the saving

i would suggest to possibly alphabetize them, that is unless they are in order by id

to your query add this


MySQL Syntax (Toggle Plain Text)
  1. ORDER airport.airport asc, service.service asc
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Nov 19th, 2008
0

Re: Pulling related data from relational dB re: thread: Problems with a many-to-many

Actually, the insert form is being built dynamically. I did manage to get that done. This is a separate page and is not a form. This is a page that simply displays the users details. I am just trying to now display it in a grid, similar to the grid I used to insert it.

I am using the ORDERBY command first by airport and then by service, as you suggest but I want to only output the airport name once but if you look at the output I am getting, you see that an airport can have up to three records per user, depending on how many of the three available services the user has access to.

I am looking for a method to have the data repeat like in a repeat region. But only one row per airport but inside that row, a nested repeat that outputs all the services for that airport. And then moves on to the next airport.

Dave
Reputation Points: 10
Solved Threads: 1
Junior Poster
filch is offline Offline
132 posts
since Nov 2008
Nov 19th, 2008
0

Re: Pulling related data from relational dB re: thread: Problems with a many-to-many inse

why not do the similar thing as before

loop through the results
when the airportcode changes that means you are in a new group

if in same group, loop through the services and check the appropriate box

i'm not really following the part you are having trouble with, unles you are meaning you want rows to columns? meaning one single row per airport with columns of services
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Nov 19th, 2008
0

Re: Pulling related data from relational dB re: thread: Problems with a many-to-many

Yeah I want there to be one row per airport and then, inside of that row, I want to list the related services, either as columns or as rows.

I have attached what my current SQL query is giving me. So now I just need a way to put it on the page.

Dave
Attached Thumbnails
Click image for larger version

Name:	Picture 4.png
Views:	17
Size:	42.6 KB
ID:	8264  
Last edited by filch; Nov 19th, 2008 at 6:16 pm.
Reputation Points: 10
Solved Threads: 1
Junior Poster
filch is offline Offline
132 posts
since Nov 2008
Nov 19th, 2008
0

Re: Pulling related data from relational dB re: thread: Problems with a many-to-many inse

Quote ...
Yeah I want there to be one row per airport and then, inside of that row, I want to list the related services, either as columns or as rows.
you are meaning sql or php?
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Nov 19th, 2008
0

Re: Pulling related data from relational dB re: thread: Problems with a many-to-many

Ahh yes .. I mean in PHP. I guess, as I have an SQL query that gives me what I want, this question would not be appropriate here? I think I was wondering if there was a way to write an SQL query that give me the one airport as well as the one to three associated services back as one record rather than two or three.

D
Reputation Points: 10
Solved Threads: 1
Junior Poster
filch is offline Offline
132 posts
since Nov 2008
Nov 19th, 2008
0

Re: Pulling related data from relational dB re: thread: Problems with a many-to-many inse

just to be sure this is what you are meaning, here's a similar post

http://www.daniweb.com/forums/post735371-9.html

is this what you want?
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Nov 19th, 2008
0

Re: Pulling related data from relational dB re: thread: Problems with a many-to-many

To be truthful I am not sure. I understand some of that but not enough to know if it would work with my situation. It seems like it would but I am not sure.

Dave
Reputation Points: 10
Solved Threads: 1
Junior Poster
filch is offline Offline
132 posts
since Nov 2008
Nov 19th, 2008
0

Re: Pulling related data from relational dB re: thread: Problems with a many-to-many inse

will you post the structure and i get you a query?
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: Which is better? Tons of separate tables with a little data or one big table?
Next Thread in MySQL Forum Timeline: update in steps? is it possible ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC