MYSQL - Add multiple users to a table column - mysql

Maybe it's because I don't understand how to search for the right verbiage, but I'm having difficulty understanding how to attach multiple users to a table with multiple columns.
Here is what I'm attempting to do:
table name: user
user_id user_name
1 abc
2 xyz
3 pqr
4 new
table2 name : brackets
id user_id bracket_name
1 4,2 bracket_1
2 4,3,1 bracket_2
3 2,1 bracket_3
4 3,4,2 bracket_4
-- OR --
table name: user
user_id user_name brackets_id
1 abc 2,3
2 xyz 1,3,4
3 pqr 2,4
4 new 1,2,4
table2 name : brackets
brackets_id user_id bracket_name
1 4,2 bracket_1
2 4,3,1 bracket_2
3 2,1 bracket_3
4 3,4,2 bracket_4
I'm using nodejs and sequalize as my ORM and understand enough to read, write, delete and update to these tables, but when it comes to organizing my data, I'm completely lost!
Is it possible to add an array to MYSQL with the user ID's or the brackets that the user is allowed to access? The bracket are generated by a user and then they can invite their friends to join a bracket. Users can join multiple brackets and join other brackets as users.
Any guidance would be very helpful!

I think a Junction Table would simplify this for you: http://en.wikipedia.org/wiki/Junction_table
It would look something like:
table name: user
user_id user_name
1 abc
2 xyz
3 pqr
4 new
table2 name : brackets
brackets_id bracket_name
1 bracket_1
2 bracket_2
3 bracket_3
4 bracket_4
table3 your junction table:
user_id brackets_id
1 2
1 3
2 1
2 3
2 4
etc.etc.

Related

Define table of choosed user row in multiple table list

I have control dashboard where multiple tables are listed with query
And in dashboard I can switch it to one table from ALLData to User1Table... and vice versa.
When there is only one table chosed I can easily manipulate data. However, I am struggling with updating rows when ALLData(all tables) are listed in dashboard. I can update it checking each table. I was wondering is there any better way to update it.
Tables have no DR. All tables have same column names.
//ALLData
SELECT * FROM users1
UNION ALL
SELECT * FROM users2...
user1
id name tel status
1 Bob 911 1
user2
id name tel status
3 Anna 11 0
3 Jack 12 1
//ALLData in dashboard
id name tel status
1 Bob 911 1
3 Anna 11 0
3 Jack 12 1
I can use id and status as PK

Get unique data across certain column in multiple tables - sql

I know this is probably so odd to ask. But lets say I have 3 tables:
Table 1
ID
Name
1
Adam
2
David
3
Conor
Table 2
ID
Name
1
Adam
2
Derek
3
Niall
Table 3
ID
Name
1
Adam
2
David
3
John
Is there any way I can write a query to get the unique names across all 3 tables. So it would return "Adam, David, Conor, Derek, Niall, John"
Order doesn't matter
If it helps, all name values are related to a names table
yes , one way is to union them
select name from table1
union
select name from table2
union
select name from table3
union automatically removes duplicate cases

How can i convert a comma separated columns into junction table?

I am pretty new to mysql and this site. I got an old mysql database (100.000 entries) to migrate to our new system. This is the old table:
CUSTOMER
Customer_ID Name Categories
1 Bob 1,2
2 Phil NULL
3 Ines 10,8
4 Carol 1
5 Rick 13,2
And i need the following structure:
CUSTOMER
Customer_ID Name
1 Bob
2 Phil
3 Ines
4 Carol
5 Rick
Category
Category_ID Category_Name
1 Biker
2 Doctors
3 Teacher
... ...
13 Drivers
CustomerHasCategory
Customer_ID Category_ID
1 1
1 2
3 10
3 8
4 1
5 13
5 2
Thanks for any help.
I also had this problem but not in MySQL. I solved it with Python using the Pandas library. So, the exact steps I followed won't be useful for you. However, I'll show you the general idea behind the solution I used.
Below is image of the original column
First, I splitted the text into columns using the comas as the delimiter.
Next, I 'stacked' the columns
Finally, I removed the artefact column(s). So, I have only the ID and the values columns. This creates a one-to-many relationship.

Display result for mysql query

table A
id name group_id email
1 a 1 a#g.com
2 b 3,4 b#g.com
3 c 1,3,4 c#g.com
4 d 2,5,1 d#g.com
table b
id user user_group_id
1 x 1,3
The table structure is as above
OUTPUT: if i search for user_group_id (from table B) for 1,3 in table A then i should get 4 email addresses i.e a#g.com,b#g.com,c#g.com,d#g.com. Since 1 is present in 3 rows in table A and 3 is present in 2 rows.
While I don't exactly understand what your problem is, I have the impression that your table structure is not properly normalized.
Your table b should have two entries:
id user user_group_id
1 x 1
2 x 3
In this case, you can properly join your tables and get all answers when querying for a certain user name.

how to design table for accessing the different places

The tables are for users to access different places
The design as below:
user table:
<user>
userid
username
place (the row define access rights)
place table:
<place>
placeid
placename
floor
My thoughts:
three places and placeid are 001,002,003
one user and userid is 001 to aceess these three places
<user>
userid username place
001 john 001,002,003
<place>
placeid placename floor
001 A 1
002 B 2
003 C 3
004 D 4
My question is,
in "user" table, the attribute "place" contains many placeids,
and separate by a comma, this design is fine or bad ?
It needs to separate the place values from "user" table ?
Using a comma delimited list to do a many to many relationship is bad design. You should use an intermediate table instead:
<user>
userid
username
<place>
placeid
placename
floor
<accessrights>
userid
placeid
Instead of putting "1,2,3" in user.place for userid 001, then, you put three rows in accessrights, all with userid 001 and one with each placeid.
That's not a good idea. Make a separate table to store the relationship:
<users-places>
userid placeid
1 1
1 2
1 3
Indexing your approach would not be straight forward - although possible.
Use the name "users-places" as it implies what 2 tables it relates. Change the name if you significantly store more information about this relationship - ie, you start adding columns to this new table.
Also, name your tables in the plural form. Singular is reserved for class names. Tables are thought of as collections.
Thank you all,
I modified my design as below, is it fine ?
<user>
userid username groupid
1 john 1
<group>
groupid groupname
1 admin
2 general
3 special
<group_manage>
groupno groupid placeid
1 1 1
2 1 2
3 1 3
4 1 4
5 2 1
6 2 2
7 2 3
<place>
placeid placename floor
1 A 1
2 B 2
3 C 3
4 D 4