Jump to content

Multiple Queries Help


SteveCatbert

Recommended Posts

Hi All,

 

I am designing a web page that needs to call up a database for multiple queries. The problem is that the current way I am running the script is that it requires me to run approximately 100+ queries on a single page and want to try to reduce this substantially, if not entirely. 

 

I have a database that holds four tables. One table is a list of ranks that can be assigned to a user. There are approximately 100-150 of these ranks. The second table consists of a list of groups that users can join. The third table is the user table. The user table consists of a field that contains the rank_id associated with the user and only one rank can be assigned to a user at a time. The fourth and final table is a joining table. This table connects the user table and the groups table together. 

 

The page I am designing is supposed to call up each rank, in a specific order, the order not correlating in any alphabetical or numerical sequence, and list all associated users with that rank. It then moves onto the next rank and repeats the query over and over until all ranks are used up. Currently I am manually creating the code to call up each rank and display all members with said rank.

 

Is there a way to run one query or fewer queries for this task? An example of what I am doing is below:

 

*Search the joining table for all users associated with group number 34*
$q_user_rank_search = mysql_query("select * from sv1user_group where group_id='34'") or die(mysql_error()); 
while($r_user_rank_search = mysql_fetch_assoc($q_user_rank_search))
 
*Search the users table for all users that are part of the above group AND have the rank with ID number 34*
$q_user_search = mysql_query("select username,user_id,user_rank from sv1users where user_id='$g_user_id' AND user_rank='34'") or die(mysql_error()); 
while($r_user_search = mysql_fetch_assoc($q_user_search)) 
{
echo $r_user_search['username']."<br>";


}

 

The second set of code above is repeated for each rank and this is where I would like to reduce the load on the database and limit the queries if possible.

Edited by SteveCatbert
Link to comment
Share on other sites

You need to learn about JOINS. You are also using deprecated code that will not work at all in the latest version of Php. You need to use PDO with prepared statements.

 

I believe your database design may be wrong as well. Post an SQL dump of your database.

Edited by benanamen
Link to comment
Share on other sites

Unfortunately I have not been able to get any mysqli_ commands to work with the current server version of php, which is why I am using mysql_.

 

For the sake of ease, I am only including,the necessary fields as there are many more that are not related to this issue.

 

'Join' table', user_group

group_id, user_id

 

Users Table, users

user_id, username, user_rank

 

Group Table, groups

group_id, group_name

 

Ranks Table, user_ranks

rank_id, rank_name

Link to comment
Share on other sites

given the structure is

       +-------------+           +-------------+                                       +--------------+                   
       | rank        |           | user        |                                       | group        |                   
       +-------------+           +-------------+                                       +--------------+                   
       | rank_id(PK) |-----+     | user_id(PK) |------+                        +-------| group_id(PK) |                   
       | rankname    |     |     | username    |      |                        |       | groupname    |                   
       +-------------+     +----<| user_rank   |      |     +--------------+   |       +--------------+                   
                                 +-------------+      |     | user_group   |   |                                          
                                                      |     +--------------+   |                                          
                                                      +----<| user_id(PK)  |   |                                          
                                                            | group_id(PK) |>--+                                          
                                                            +--------------+                

then you could

SELECT 
    g.groupname
  , r.rank_id  
  , r.rankname
  , u.username
FROM user_group ug
    INNER JOIN group g USING (group_id)
    INNER JOIN user u USING (user_id)
    INNER JOIN rank r ON u.user_rank = r.rank_id
WHERE ug.group_id = 34
ORDER BY u.rank_id, u.username

Link to comment
Share on other sites

Hi,

 

Thank you for your help. I put what you had above into my script (changed the names to the exact names of the tables) and I am getting an error saying the column name does not exist in the field list. Any idea why? I have confirmed that all the field and column names are correct. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.