Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 23rd, 2008, 8:13 PM   #1
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
MySQL - Basic Combining Of Queries

What I need to do is output a table of matches. But at the same time, I need the username of each user_id found in the table.

So I have:

MySQL Syntax (Toggle Plain Text)
  1. SELECT * FROM $table_match

That fetches every row from the matches table. The matches table has the columns 'user_id1' and 'user_id2'. I want the username for each of these user_ids.

This can be done by calling the following two queries for each row fetched with the above query.

MySQL Syntax (Toggle Plain Text)
  1. SELECT name FROM $table_user WHERE user_id=$user_id1
  2. SELECT name FROM $table_user WHERE user_id=$user_id2

This works, but the problem with that is it will keep querying for usernames, even if it's already fetched it before. Despite MySQL's cache, I feel that this is slower than it needs to be. Maybe MySQL will optimize the query if I combine the two. Or even better, can I link the user_id column to the name column from the user table?

Thanks in advance.
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges!
Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download.
Sane is offline   Reply With Quote
Old Mar 23rd, 2008, 9:28 PM   #2
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 244
Rep Power: 3 Seif is on a distinguished road
Re: MySQL - Basic Combining Of Queries

not sure I 100% follow what you mean but why not just use an OR statement ?

SELECT name FROM $table_user WHERE user_id=$user_id1 OR user_id=$user_id2

Ideally, if you are constructing a set of results from more than one table that reference each other using foreign keys, you can perform a series of joins to obtain what you need. There are multiple ways of performing a join:

SELECT table_user.name
FROM table_user,table_match
WHERE table_user.userid1 = table_match.userid1 OR
 table_user.userid2 = table_match.userid2

or

SELECT table_user.name
FROM table_user
INNER JOIN table_match
ON 
 table_user.userid1 = table_match.userid1 OR
 table_user.userid2 = table_match.userid2

Both commands should construct a list of names for each userid. Have a look at JOIN SQL statements. They should provide all that you need.
Seif is offline   Reply With Quote
Old Mar 23rd, 2008, 9:52 PM   #3
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Re: MySQL - Basic Combining Of Queries

I was saying I need the username of every user_id I fetch, on top of the row I originally obtained. I can already do that with the MySQL queries I posted above. I'm now looking for a way to combine the queries so that I don't have to make 1+2*N queries. Instead just 1 query.

Modifying what you posted at the bottom might work. I will try some of that out. Thanks.

Edit: I can do it with one user id. Here's an example of where I need to output a table of ranks, and at the same time get the user name for every user_id.

MySQL Syntax (Toggle Plain Text)
  1. SELECT $table_ranking.*, $table_user.name
  2. FROM $table_ranking INNER JOIN $table_user
  3. ON $table_ranking.user_id=$table_user.user_id AND $table_ranking.game_id=$game_id

The problem comes from extending this to get the usernames of both user_id1 and user_id2 for each row. It seems like I would need multiple joins to accomplish that. Maybe I can try screwing around with nested joins.
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges!
Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download.

Last edited by Sane; Mar 23rd, 2008 at 10:14 PM.
Sane is offline   Reply With Quote
Old Mar 24th, 2008, 12:42 AM   #4
Arla
Professional Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 346
Rep Power: 4 Arla is on a distinguished road
Re: MySQL - Basic Combining Of Queries

How about

Select a.name as user1, b.name as user2
  from $table_user a
         ,$table_user b
         ,$table_match c
  where a.user_id = c.user_id1
  and     b.user_id = c.user_id2

I think that should do it (bit tired right now, so might have missed something)
Arla is offline   Reply With Quote
Old Mar 24th, 2008, 1:58 AM   #5
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
Re: MySQL - Basic Combining Of Queries

Not the most elegant, but perhaps fits your needs?
mysql Syntax (Toggle Plain Text)
  1. SELECT $table_ranking.*, $table_user.name
  2. FROM $table_ranking INNER JOIN $table_user ON $table_ranking.user_id=$table_user.user_id
  3. WHERE $table_user.user_id IN ($gameIds)
$gameIds should be a CSV list of user IDs put together in your PHP script somewhere (assumption: you're putting a list together in PHP, since this is in the PHP forum...)
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Mar 24th, 2008, 8:46 AM   #6
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 244
Rep Power: 3 Seif is on a distinguished road
Re: MySQL - Basic Combining Of Queries

Theres no reason why you need multiple queries. if you want every name for user_id1 and user_id2, just add that condition clause to the join. If you are concerned of duplicate name entries in the view from the query, then add a distinct to the statement.

I'm still having a bit of a job visualizing what your problem actually is. are user_id1 and user_id2 unique values? do they both make up a composite key ? it's even more confusing in your second post with differing table and field names.
If you cannot create a view from one query between two tables, then maybe you should reconsider the design of your database for performance gain.

The following single query "should" select a distinct set of names in table user for each user_id and game_id entry in table_ranking.

MySQL Syntax (Toggle Plain Text)
  1. SELECT DISTINCT $table_ranking.*, $table_user.name
  2. FROM $table_ranking INNER JOIN $table_user
  3. ON $table_ranking.user_id=$table_user.user_id OR $table_ranking.user_id=$table_ranking.game_id
Seif is offline   Reply With Quote
Old Mar 24th, 2008, 10:23 AM   #7
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Re: MySQL - Basic Combining Of Queries

@Arla: You got it in one shot. Thanks. Now I'll have to do some testing to see if it is indeed any faster.

@Seif: I didn't think it would be this hard to visualize. I am reporting a table of information. In the table there is a user_id column, but I don't want the user_id of each row. I want the user name for each row.

For example, here: http://codersblock.net/arena/ranking.php. The table "ranking" contains several rows, and a column called "user_id". I report the user name for each row by getting it from a second table. That is shown by the MySQL in my second post.

But now I need to do it where there's a user_id1 AND user_id2. Now, let's go back to my very first post.

Quote:
I want the username for each of these user_ids.
For example, here: http://codersblock.net/arena/lobby.php. The matches table has two user_ids, and I report the user name for "each of these user_ids". This is accomplished by the MySQL from my first post, which I now want to combine into one whole query.

Read my question over again and maybe it will all fall together now. But since I've already got it working with Arla's MySQL, now I only need to optimize it if possible.
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges!
Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download.
Sane is offline   Reply With Quote
Old Mar 24th, 2008, 6:28 PM   #8
Arla
Professional Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 346
Rep Power: 4 Arla is on a distinguished road
Re: MySQL - Basic Combining Of Queries

Sane,

Glad that was the right query, was a bit hard trying to read between the lines of what you actually wanted, especially when you added the table_ranking into the mix which wasn't even in your first post (and I ignored it, since it wasn't something you mentioned in the original problem).

If possible, post an example of what's in the tables and what you would want as output, since I wasn't sure if you wanted only distinct names as Seif had a query for, or you wanted what I posted, or something else entirely.
Arla is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Connecting MySQL and PHP titaniumdecoy PHP 10 Feb 25th, 2008 8:47 PM
BASIC - the best sort... HippyVanMan Other Programming Languages 6 Sep 23rd, 2007 8:03 PM
The basic of basic in programming... Simongcc Software Design and Algorithms 4 Aug 16th, 2007 4:34 AM
Tutorial - Using MySQL in C# Darkhack C# 12 Jan 17th, 2006 10:28 AM
[ANN] New script engine (Basic sintax) MKTMK C++ 3 Sep 1st, 2005 6:51 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:47 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC