in reply to AoH refs for setting HTML::Template loops
id username branches 1 vroom gods,QandA,janitors 2 merlyn janitors 3 Ovid power users,janitors 4 dws power users,janitors,pmdevThis is violating the First Normal Form, which roughly states that no field of a table may contain multiple values. That's why we use Relational Databases in the first place, so that we don't have to use an outside Programming Language to split the values apart. Instead, you make another table, called branch or branches:
id name 1 gods 2 QandA 3 janitors 4 power users 5 pmdevNow, if a given user can only belong to one 'branch', then our user table might look like: (2nd Normal Form)
id username branch_id 1 vroom 1 2 merlyn 3 3 Ovid 4 4 dws 4If we need to pull out the branches, SQL to the rescue!
However, a given user can belong to multiple branches, so we need employ the 3rd Normal Form. Start by removing the branch_id column from the user table, then create a new table - a 3rd table that will join Users to Branches: (user_branches)SELECT user.username, branch.name FROM user INNER JOIN branch ON user.branch_id = branch.id
user_id branch_id 1 1 1 2 1 3 (vroom belongs to 1, 2, and 3) ------------- 2 3 (merlyn is a janitor) ------------- 3 4 3 3 (Ovid is power user and janitor) ------------- 4 4 4 3 4 5The SQL to join all three tables together is trickier, but the more you practice, the better you get. Here it is:
If you database tables are set up "properly", then you minimize the amount of work you have to do youself in Perl. Let the database do that work for you. :)SELECT user.username, branch.name FROM user INNER JOIN user_branches ON user.id = user_branches.user_id INNER JOIN branch ON user_branches.branch_id = branch.id
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat)
|
|---|