MySql x rows with x-1 similar columns, combine last column - mysql

I have a mysql table that has several values like this
id name number value
------------------------------------
1 John 3 blue
1 John 3 red
1 John 3 green
2 Aly 2 red
2 Aly 2 blue
3 Sam 1 green
4 Tiad 6 white
5 Krix 5 orange
Is there a SQL command that can group by or combine these values into one row, with the final column's values put into that one row as values separated by commas? So basically, what command could take the above table and change it into
id name number value
------------------------------------
1 John 3 blue, red, green
2 Aly 2 red, blue
3 Sam 1 green
4 Tiad 6 white
5 Krix 5 orange
Is there such a command?

you can try sommething like this
Updated:
SELECT id,name,number, GROUP_CONCAT(value SEPARATOR ', ') AS value
FROM yourtable GROUP BY id

Related

Gaming Database Query for multiple players

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.

Using Max on a Table joined with another [duplicate]

This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Retrieving the last record in each group - MySQL
(33 answers)
Closed 3 years ago.
The goal is to create an overview of data records. These records come from multiple tables. One of these Tables holds several rows per record, each having an ID number. Only the one with the highest ID number should be shown.
I've tried doing some things with Inner Joins but i can't get it right.
a simplified view of the problem:
Tabel1
Tabel1_Id ValueA ValueB
1 ABC DEF
2 GHI JKL
3 MNO PQR
4 STU VWX
Tabel2
Tabel2_id Tabel1_Id Number ValueC
1 1 1 Green
2 1 2 Yellow
3 2 1 Blue
4 1 3 Red
5 3 1 Purple
6 3 2 Pink
7 2 2 Violet
8 4 1 Magenta
9 2 3 Cyan
10 4 2 Teal
Desired Result
ValueA ValueB ValueC
ABC DEF Red
GHI JKL Cyan
MNO PQR Pink
STU VWX Teal
if your DBMS support row_number() then you can use below
with cte as
(select *,row_number()over(partition by Tabel1_Id order by Number desc) rn
from table2
) select valueA,valueV,valuec
from cte join table1 t1 on cte.Tabel1_Id=t1.Tabel1_Id
where cte.rn=1

How to check for one value in multiple rows

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.

Single Foreign Key for multiple look up tables

I'm building a simple application that would record several widget selections within a form (drop-down list and multiple select for this example) and save it within a log table in a small database.
http://i.stack.imgur.com/THpfs.png
This schema represents a selection of a Fruit from a drop down, and 1-n Fruit Type Selection from a multi-select:
Fruit
FruitId Name
1 Apple
2 Orange
3 Melon
Orange
OrangeId Name PLU1 PLU2
1 Navel 123 321
2 Blood 213 412
3 Cara 512 433
LogFruitSelection
LogId FruitId FruitSelection
1 2 2
1 2 3
2 1 1
2 1 2
2 1 3
2 1 4
The user is restricted to select only one fruit which will give them an option to select multiple types of that fruit, 1 type of fruit per submission. The selections will be held within a selection table which will reference a LogId, a FruitId, and a SelectionId which references a lookup table.
I am lost as to how to create a schema that would let me use a selection table with a single foreign key that would reference several look up tables based on another columns value.
Hope this is what you want
Fruit
FruitId Name
1 Apple
2 Orange
3 Melon
Fruit Type
TypeId Name PLU1 PLU2 FruitId
1 Navel 123 321 2
2 Blood 213 412 2
3 Cara 512 433 2
LogFruitSelection
LogId FruitId FruitSelection
1 2 2
1 2 3
2 1 1
2 1 2
2 1 3
2 1 4

mysql compare columns in same table

I have a table in a mysql db as
id fruit number_eaten day
----------------------------------
1 apple 2 1
2 banana 1 1
3 orange 3 2
4 apple 1 2
5 banana 2 3
6 peach 1 3
I'm trying to figure out how to select such that I can compare how many was eaten per day and place into a spreadsheet so i get
fruit number_eaten_day_1 number_eaten_day_2 number_eaten_day_3
------------------------------------------------------------------------
apple 2 1 0
banana 1 0 2
orange 0 3 0
peach 0 0 1
it's easier to have separate row per fruit and day with summed value number_eaten:
select fruit, day, sum(number_eaten)
from fruits_eaten_by_day
group by fruit, day
but it should also be possible to have exact result you need by doing this:
select
fruit,
sum(if(day=1, number_eaten, 0)) as number_eaten_day_1,
sum(if(day=2, number_eaten, 0)) as number_eaten_day_2,
sum(if(day=3, number_eaten, 0)) as number_eaten_day_3
from fruits_eaten_by_day
group by fruit