Jump to content

Shorter way to do this, database tables


Mutley

Recommended Posts

I have 2 tables, one with these fields:

 

(Table name: loadout)

user_id | prod_it

 

Then the other with:

 

(Table name: products)

prod_id | name | description

 

What I want to do is list all the products names for a user. The user table only has the prod_id in it, not the name, so how do I simply take the name from the other table?

 

At the moment I do a SELECT query to get the prod_id from the user, then do an individual SELECT query for every single product name using the prod_id I got earlier, as you can imagine, it's a lot of code this way!

Link to comment
https://forums.phpfreaks.com/topic/43266-shorter-way-to-do-this-database-tables/
Share on other sites

Ahh, sorry, I got it wrong.

 

What I have is:

Table: loadout

user_id | slot1 | slot2 | slot3 | slot4 | slot5

 

The "slots" have the product id's in.

 

Then,

Table: products

prod_id | name

 

So I want to create a list for that user, by getting the name of the products from the prod_id in the "slot" fields.

What you should have is this (normalized data) ,

with a separate row in loadout for each user's slot

[pre]

user            loadout              product

----------      -----------          ------------

user_id  <-+    id            +----> prod_id

name        +--- user_id        |      name

                slot      -----+      description

[/pre]               

Otherwise you just create more work for yourself (and those willing to help)

Instead of

[pre]

user_id | Slot1 | Slot2 | Slot3 | Slot4 | Slot5 |

--------+-------+-------+-------+-------+-------+

  101  |  1  |  5  |  20  |  26  |  53  |

  102  |  6  |  29  |  42  |  108  |    1  |

[/pre]

 

it should be

[pre]

id  |  user_id | slot  |

-----+----------+-------+

1  |  101    |  1  |

2  |  101    |  5  |

3  |  101    |  20  |

4  |  101    |  26  |

5  |  101    |  53  |

6  |  102    |  6  |

7  |  102    |  29  |

8  |  102    |  42  |

9  |  102    | 108  |

10  |  102    |  1  |

[/pre]

If the slot number is significant, an alternative is this,

in which case the primary key would be (user_id, slot)

[pre]

user_id |slot |prod_id|

--------+-----+-------+

101    | 1  |  1  |

101    | 2  |  5  |

101    | 3  |  20  |

101    | 4  |  26  |

101    | 5  |  53  |

102    | 1  |  6  |

102    | 2  |  29  |

102    | 3  |  42  |

102    | 4  | 108  |

102    | 5  |  1  |

[/pre]

 

Here is an example of a query I'm using with the table I have now for loadout:

 

<?php
		$result2 = mysql_query("SELECT * FROM loadout WHERE user_id = '$login' LIMIT 1");
		while($row2 = mysql_fetch_array( $result2 ))
		{
	$l1_slot1 = $row2['slot1'];
	$l1_slot2 = $row2['slot2'];
	$l1_slot3 = $row2['slot3'];
	$l1_slot4 = $row2['slot4'];
	$l1_slot5 = $row2['slot5'];
		}

?>

 

So how would this change? If I used your method, wouldn't I have to do a loop query and attach variables?

Assuming this version of the table

[pre]

user_id |slot |prod_id|

--------+-----+-------+

101    | 1  |  1  |

101    | 2  |  5  |

101    | 3  |  20  |

101    | 4  |  26  |

101    | 5  |  53  |

102    | 1  |  6  |

102    | 2  |  29  |

102    | 3  |  42  |

102    | 4  | 108  |

102    | 5  |  1  |

[/pre]

<?php 
    $sql = "SELECT u.user_id, u.slot, p.name
            FROM loadout u
                INNER JOIN products p ON u.prod_id = p.prod_id
            ORDER BY u.user_id, u.slot";
            
    $res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>");
    while (list($user, $slot, $prod) = mysql_fetch_row($res)) {
        echo "$user $slot $prod<br>";
    }
?>

Table aliases.

 

Saves having repeat the whole table name each time as in

 

$sql = "SELECT loadout.user_id, loadout.slot, products.name
            FROM loadout 
                INNER JOIN products  ON loadout.prod_id = products.prod_id
            ORDER BY loadout.user_id, loadout.slot";

 

and IMO makes it more readable

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.