MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old June 8th, 2003, 01:49 AM
Erik Erik is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 3 Erik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
MySQL: multiple coloumn names after JOIN's

Hello,

I have a problem when using JOIN's. I have a situation like this:
Table Users
--ID (int, Primary Key, ...)
--name
--email
--...

Table Messages
--FromUser_ID
--ToUser_ID
--Title
--Content
--...

Now I want to display certain messages, but I also need the users data. So I used JOIN's (SELECT * FROM Messages m LEFT JOIN Users u1 ON u1.ID=m.FromUser_ID ...). This works but since I join both users (FromUser_ID and ToUser_ID), I can't use mysql_fetch_assoc() any more, because all field-names of the Users-table occure twice. I could do everything with mysql_fetch_row() using the field indices, but I don't like this way because the coloumn count of the tables may vary and I don't want to recalculate the indices all the time.
So there was only the possibility of using aliases. Because I need all fields of the two users (this table has around 100 coloumns), it would be very nasty to create an alias for each coloumn.

My question: is there any decent way to solve my problem? I basically search something like "SELECT u1.* AS FirstUser_*, u2.* AS SecondUser_* ..."; adding some text at the beginning of all the field names. But I tried all variations and this didn't work (always syntax errors occured).

Thank you in advance :-)

bye Erik

Last edited by Erik : June 8th, 2003 at 01:52 AM.

Reply With Quote
  #2  
Old July 9th, 2003, 12:31 AM
avit avit is offline
Not Yet Perfect
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: Squamish, BC
Posts: 111 avit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via ICQ to avit
Hmm, interesting.

It's hard to see what you are trying to do, but I think you will need to select the individual table fields by name as part of your statement.

Don't use wildcards for SELECT * or SELECT tablename.*

Reply With Quote
  #3  
Old July 9th, 2003, 07:21 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Surely you're not going to display a form or page with all 100 values for both users. If so, you should consider going back to the drawing board from a user interface perspective. Why not have your initial join select only the name, ID, and one or two other key pieces of data for each user and display the title and to/from values with links to a detail page for each user, which would key off the user ID and require a much easier query without the need for aliasing?

Reply With Quote
  #4  
Old July 9th, 2003, 11:27 AM
Erik Erik is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 3 Erik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hello :-)

thank you for your tips. In fact, I found a way to do this without having need to display everything at once. And of course I could use multiple queries. I just wanted to know whether there is any possibility to do this with one query.

Thank you

bye, Erik

Reply With Quote
  #5  
Old July 9th, 2003, 12:28 PM
avit avit is offline
Not Yet Perfect
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: Squamish, BC
Posts: 111 avit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via ICQ to avit
Can we know how you did it? Is it still one query? What if you used mysql_fetch_row() instead of mysql_fetch_assoc() ? Would that work, since the fetch_row array is keyed numerically, to avoid duplicate keys?

I know, I know, you ask one question and you get four in return... But I, for one, am curious!

Reply With Quote
  #6  
Old July 10th, 2003, 12:15 AM
Erik Erik is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 3 Erik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hello

as I said in my first post, mysql_fetch_row would work perfectly. But my problem is that the number of rows of the users table varies. So if I used mysql_fetch_row, I would have to get all coloumns first to calculate the indices of them.
In fact, I didn't solve the problem but just selected two fields via aliases and displayed all the other things on a special page per user.

But I had an idea know. Look at this query:
SELECT us1.*, us2.*, m.* FROM (messages m LEFT JOIN users us1 ON us1.ID=m.FromUser_ID) LEFT JOIN users us2 ON us2.ID=m.ToUser_ID ORDER BY m.Creation_Date DESC

This created my problem, you know. But I think it is possible to break it up into a maximum of three queries:
SELECT m.* FROM messages m ORDER BY m.Creation_Date DESC

SELECT us.* FROM messages m LEFT JOIN users us ON us.ID=m.FromUser_ID ORDER BY m.Creation_Date DESC

SELECT us.* FROM messages m LEFT JOIN users us ON us.ID=m.ToUser_ID ORDER BY m.Creation_Date DESC

So this is quite easy. In my opinion this is a decent way because the data is nicely seperated and using mysql_fetch_assoc for all three queries is no problem. And 3 queries are not too many. I was just too stupid to find this way. In my first solution, I looped through all the messages and started an extra query to get each specific user. Of course that is not good when having a few hundred messages. But using those 3 queries it will still be fast und nicely accessible I think

Greets, Erik

Last edited by Erik : July 10th, 2003 at 12:18 AM.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > MySQL: multiple coloumn names after JOIN's


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway