Display result for mysql query - mysql

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.

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

SQL Order by a specific value of a one to many field

The context :
I have these three simple SQL tables : my database diagram
a Request has one or N Request_Field
Request_Field is a pivot table
Value inside the pivot table Request_Field can sometimes contain dates in string format
a Field has 0 or N Request_Field
These tables are part of a greater database and I cannot change them at the moment.
My question :
Is it possible to query multiple "Request" and order them by a specific value in "Request_Field" ?
Example :
Request table
id
1
2
3
Request_Field table
id
request_id
field_id
value
1
1
1
John
2
1
2
2021-01-01
3
2
1
Jane
4
2
2
2020-01-01
5
3
1
Jack
6
3
2
2022-01-01
Field table
id
label
1
name
2
desired_date
The SQL query I try to make : get all Request ordered by the desired_date record of the Field table (DESC).
Request desired results
id
3
1
2
Thank you

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

MYSQL - Add multiple users to a table column

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.

How to update fields of one table using other table?

How can we update table 1 such that it will replace b field value of table 1 by that of table 2 where a field value are found same?
Suppose I have two tables
table 1
fields a b c
1 5 10
1 5 8
2 5 0
1 4 11
and
table 2
fields a b
1 6
1 7
2 5
1 4
I'm going on 6th form knowledge so I'll leave the code for you to do, but here's basically how I'd do it:Select all values from table 2For each value, select the rows from table 1 with the matching 'a' valueCount number of matching valuesIf it's over one, update table 1 set 'b' as the new value where 'a' matches
Edit: Oh, just realised, the 'a' values aren't unique, unless both tables have matching ID for each row, I'm not sure you can do it.