Retrieve single name and multiple records - mysql

I have a requirement in my app which has multiple members and single name.In my web app i need to create a groups of some users.So suppose there are 2 groups test1 and test2.test1 group has 1 and 2 members and test2 has members 1,2 and 3(1 or 2 or 3 are not numbers consider them as group memebers.
My web app has 2 columns.if retrieve select * from groups then the output is like the following
groupname groupMembers
test1 1
test1 2
test2 1
test2 2
test2 3
but I want output like this
groupname groupMembers
test1 1
2
1
test2 2
3
So is it possible using any sql query.i am using jdbc for retrieving from mysql.
this is sqlfiddle http://sqlfiddle.com/#!2/d4610/1

You can do like that:
select name, GROUP_CONCAT(age) from groups group by name;
That's not exactly the ouput you want but i hope it helps

Related

How to join 2 rows in database?

I got some issues while structuring my database and I would like to know how to solve the below question .
I have a table in database :
id
id_study
writer
1
1
writer1
2
1
writer2
3
2
writer3
Please help me how to combine above 2 table rows to produce output like below :
id
id_study
writer
1,2
1
writer1, writer2
3
2
writer3
SELECT GROUP_CONCAT(id) as id, id_study,GROUP_CONCAT(Writer) as Writer
FROM TABLE
GROUP BY id_study;

How to form this MYSQL query

I have the following table
TABLE store
store name level
1 Tom 4
2 Joe 2
1 Chris 4
3 Tom 2
4 Ed 2
2 Tom 4
3 Chris 2
I want to return the number of level 4's from each distinct store
I know I can
select distinct store from store;
To get distinct stores and
select count(*) as level from store where level = 4;
to get the count of level 4's
How do I combine to return a query of number of level 4's in each distinct store
So the data above would return
store level4
1 2
2 1
3 0
4 0
It is not clear why your table is called store. Shouldn't you have a table with that name that has one row per store?
In any case, probably the simplest method for getting the 0 counts is conditional aggregation:
select store, sum(level = 4) as level4
from store
group by store;

how to get records from table with uniq on two columns - rails

I'm trying to fetch records from database that should be uniqued by two attributes
example
id name value
1 dog 4
2 dog 4
3 cat 5
4 cat 4
I want the result to be
id name value
1 dog 4
3 cat 5
4 cat 4
so the data has been uniqued by value and name , I've tried many ways with ruby to do but with no luck
You can use group by:
select min(id) as id, name, value
from table t
group by name, value;
it worked with group method of rails
table.all.group('value, name')
the generated query is
select * from table t group by value, name

query the same field multiple times in the same query

I need to filter a table in mysql but can't get past the beginning.
The table has 2 fields:
ID_house house_feature
1 1
1 2
1 4
1 5
2 1
2 3
2 4
3 1
3 2
3 3
I need to filter this table using the following parameters:
house feature = 1
AND
house feature = 2
AND
house feature = 3
So that I get all houses with the requested feature.
I already tried to create something similar to this:
SELECT *
FROM houses
WHERE
house_feature = 1
AND
house_feature = 2
AND
house_feature = 3
But it doesn't work as I expected.
Is there a way to get this result with MySQL?
It seems that I acn filter the table using only the OR operator but this way I can't get the right result.
Thanks in advance for any help.
tony
You can do so ,by matching the distinct count of features per house ,so the house with exactly these 3 features will be returned
SELECT *
FROM t
WHERE
house_feature IN(1 ,2,3)
group by ID_house
having count(distinct house_feature) = 3
Demo

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.