I need some direction on how to create this MySQL query.
I have a table that looks like this.
id group name value
1 1 user mike
2 1 setting 1
3 2 user joe
4 2 setting 2
5 3 user jill
6 3 setting 1
7 4 user mark
8 4 setting 1
9 4 other 22
I would like to format the query to group users that have identical settings (IE mike and jill would be grouped in this example, not mark because of "other")
At the end of the day I am trying to consolidate and make the table look like this. If I can figure out the query to properly group them ,I will use PHP to combine the values and save it back to the DB.
id group name value
1 1 user mike OR jill
2 1 setting 1
3 2 user joe
4 2 setting 2
7 4 user mark
8 4 setting 1
9 4 other 22
Thank you!
Related
This question already has answers here:
How can I return pivot table output in MySQL?
(10 answers)
MySQL - Rows to Columns
(13 answers)
Closed last month.
if have 2 tables:
tool_id
name
status
1
Mike
1
1
Claus
3
1
Max
3
2
Mike
2
2
Claus
2
3
Mike
1
3
Claus
3
3
Max
1
3
Sam
1
4
Mike
2
and
tool_id
tool_name
1
51363
2
52514
3
51458
4
51608
I need a view that shows me these 2 tables as a matrix. The tools should be displayed in the table headers and the names and their status for the corresponding tool as a table.
If a name is not assigned to a tool, it should be given a zero.
name
51363
52514
51458
51608
Mike
1
2
1
2
Claus
3
2
3
0
Max
3
0
3
0
Sam
0
0
1
0
I have more than 400 tools and workers each in the two tables.
Is something like this possible?
Edit: This is not an duplicate of the linked posts.
I need a dynamic creation of columns.
I want to have a SQL result look like this (match result query):
ID Arena Winner Loser Winner_score Loser_score
-----------------------------------------------------------------
1 1 Johnny Mark 16 8
2 2 Andrew Luke 16 7
Here are my tables (simplified)
Player_tbl
ID Player
-------------
1 Johnny
2 Mark
3 Andrew
4 Luke
Match_tbl
ID Match Arena
----------------------
1 Match 1 1
2 Match 2 2
Match_results_tbl
ID Match player_id player_score
-----------------------------------------
1 Match 1 1 16
2 Match 1 2 8
1 Match 2 3 16
2 Match 2 4 7
I am wondering how to structure my query to have the desired result.
Ok, I figured out my own issue. 1st, the match_results must be put in a table with a different structure, and 2nd, the inner sql query needed to be adjusted to pull from the proper database tables.
I have an apartment table:
Id: name:
1 apartment1
2 apartment2
3 apartment3
3 apartment4
And an availabilities table:
Id apartment_id availability_date remaning_number_places
1 1 01/02/2017 3
2 1 02/02/2017 2
3 1 03/02/2017 2
4 1 04/02/2017 2
5 2 01/02/2017 2
6 2 03/02/2017 2
7 2 04/02/2017 2
8 3 12/02/2017 2
9 3 03/02/2017 1
10 3 04/02/2017 1
11 4 12/02/2017 2
12 4 02/02/2017 2
I would like to do a search by starting and ending date with a minimum number of places, for example if I search from 01/02/2017 to 04/02/2017 I should have this result:
Apartment_ids availabilities starting_date ending_date nbr_places
[1] [1,2,3,4] 01/02/2017 04/02/2017 2
[2,4] [5,6,7,12] 01/02/2017 04/02/2017 2
The result must be a concatenation of contiguous days corresponding to given dates.
Thank you at advance.
EDIT : Trying to give more details hereunder:
I need an SQL query to look for an apartment availability for a given period.
As a result, if an apartment is available for the whole period I'm expecting to get it. In addition, I also need to get if more than one apartment are required to cover this given period. In that case, I'm expecting a contiguous list of apartments as a result.
Please, can someone help me build this query?
Whats the best way to check if different groups of rows in a table with the same GroupID such as different teams have a SINGLE captain? Captain Identifier for example could be '10', so its crucial that it goes through multiple records with the same groupID and checks to see if theres ONLY ONE record with the positionID as '10'. I need it to do it for all teams, i.e all groupID's
-------------------------------------------------
ID | Group ID | Name | Position|
-------------------------------------------------
1 1 John 3
2 1 jim 3
3 1 Hahn 4
4 1 Mary 4
5 1 Moe 4
6 1 Charlie 10
7 2 taylor 4
8 2 Geoff 4
9 2 adam 4
10 2 cam 10
11 3 sharon 2
12 3 tony 4
13 3 steve 3
14 3 eve 4
15 3 gwen 10
--------------------------------
So what I need it to do is check that every groupID only had ONE 10 as the position.
Thanks in advance guys. Check out the image link at the bottom.
im using mysql btw
Sorry if this is badly described
If I understood you - I think you need something like :
select groupId, sum(case when positionID='10' then 1 else 0 end) as captains
from tbl_name
group by groupID
having captains = 1
am I close???
If I understood you correctly, you want an indication that will tell you whether the groupID had more then one captain.
So what you need is this:
select groupID,case when cnt = 1 then 1 else 0 end as IND_ONE_CAPTAIN from (
select groupID, sum(case when positionid = '10' then 1 else 0 end) as cnt
from players
group by groupID)
Now IND_ONE_CAPTAIN columns consist 1 or 0, 1 is for the group id only had 1 captain, and 0 is when they had more.
i am having table Category with the columns like
id(AutoIncrement),Parent_id,Level,Name
initially for level 1 datas has Parent_id is 0. autoincrement id will be Parent_id for next levels .my table table table datas will bw like this
id Parent_id Level Name
1 0 1 Indian
2 0 1 International
3 0 1 Nri
4 1 2 BC
5 2 2 Christian
6 2 2 Muslim
7 4 3 MBC-1
8 7 4 OBC-2
9 1 2 FC
i want to show records in this format (its like a tree view)
id Parent_id Level Name
1 0 1 Indian
4 1 2 BC
7 4 3 MBC-1
8 7 4 OBC-2
9 1 2 FC
5 2 2 Christian
6 2 2 Muslim
2 0 1 International
3 0 1 Nri
4 1 2 BC
Can any one should help me to get this arrangement of datas using sql Query?
If it does not have a set number of branches you likely want to loop over a query in your app or write an SP to obtain all nodes. Some good reading here:
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/