I want to insert one item from specified row, to another table - mysql

So I have these 2 tables. I need to get whatever is in the 1st row, 3rd column of Table 1 (in this case, ducks) and insert it into Table 2, 1st row, 6th column (marked [here])
table 1 (teams)
PK
team_id groupno tname oname onumber oemail group series
1 1 ducks laura 123 a#b.com A 1
2 2 birds john 456 c#d.com A 1
3 3 redds hanna 789 e#f.com A 1
4 4 blues mark 102 g#h.com A 1
team_id and groupno while look the same, groupno goes up to 10 (counting all teams in a group), team_id goes on
table 2 (games)
PK
game_id match_id time field group team1 team2 series
1 1 0900 1 A [here] N/A 1
2 2 0930 1 A N/A N/A 1
3 3 1000 1 A N/A N/A 1
4 4 1030 1 A N/A N/A 1
game_id and match_id arent's the same, math_id goes up to 36 (It counts all the games played in single series), game_id goes on
Below what I've been working with
UPDATE games INNER JOIN teams
ON games.game_id = 1
SET games.team2 = teams.tname
WHERE teams.series=1;
What I want the result to be
Example:
vs 2. --- 3. vs 4.
vs 3. --- 2. vs 4.
vs 4. --- 2. vs 3.
It should take the names from table 1 and arranges them into game slots so each team plays against each other
PK
game_id match_id time field group team1 team2 series
1 1 0900 1 A ducks birds 1
2 2 0930 1 A reds blues 1
3 3 1000 1 A ducks reds 1
4 4 1030 1 A birds blues 1
5 5 1100 1 A ducks blues 1
6 6 1130 1 A reds birds 1

I think this should match every combination and put it in the game order:
UPDATE games g
INNER JOIN (
SELECT
(#cnt := #cnt + 1) game_id, t.team1, t.team2
FROM (
SELECT
DISTINCT t1.tname team1, t2.tname team2
FROM teams t1
CROSS JOIN (SELECT DISTINCT tname FROM teams) t2
WHERE t1.tname <> t2.tname
) t
CROSS JOIN (SELECT #cnt := 0) c
) mt ON mt.game_id = g.game_id
SET g.team1 = mt.team1,
g.team2 = mt.team2
;
though I'd recommend using team ids instead of team names.
If you mean ids then:
UPDATE table2 ti
SET ti.item2 = (SELECT t1.item FROM table1 t1 WHERE t1.id = 3)
WHERE ti.id = 1
;

Related

Calculating the win/loss ratio per team

I have the following two tables:
sport_a_statistics:
id team_id game_id points
1 1 1 7
2 2 1 8
3 3 2 6
4 1 2 9
sport_b_statistics:
id team_id game_id points
1 1 3 2
2 2 3 1
3 3 4 3
4 1 4 10
I want to calculate the win/loss ratio for each team. This includes making sure to capture the wins and losses from both sport tables since my tournament involves 2 sports. So the output I'm looking for is the following:
team_id wins loss ratio
1 3 1 3.0
2 1 1 1.0
3 0 2 0.0
I can't wrap my head around how I would do this in one query.
Assuming you have no ties, you can use window functions and union all:
select team_id,
sum(points = max_points) as num_wins,
sum(points < max_points) as num_losses,
sum(points = max_points) / nullif(sum(points < max_points), 0) as ratio
from ((select a.*, max(points) over (partition by game_id) as max_points
from sport_a a
) union all
(select b.*, max(points) over (partition by game_id) as max_points
from sport_b b
)
) ab
group by team_id
Made a small edit ^

Mysql select statement contains where clause so unsuitable for insert into

I'm very inexperienced. I've prepared a select statement which gives the information I need to populate a matches table. However it is not suitable because it contains a where clause. Is there a different way to use it, or how can I change it so that it is suitable for INSERT INTO.
The tables are as follows:-
match_order
match_order_id||match_descrip||first_player||second_player
1 1v2 1 2
2 1v3 1 3
3 2v3 2 3
4 1v4 1 4
5 2v4 2 4
6 3v4 3 4
entries
entry_id||round_id||league_id||box_id||box_position
1 1 1 1 1
2 1 1 1 2
3 1 1 1 3
4 1 2 1 4
5 1 2 1 2
6 1 2 1 1
7 1 2 1 1
matches
match_id||round_id||league_id||box_id||match_order_id||player1||player2
I need to insert new rows every month for a new round of matches. League size, box size & positions change each month.
This is the statement which gives the correct rows.
SELECT e.round_id, e.league_id, e.box_id, mo.match_order_id, e.entry_id as player1, e1.entry_id as player2
FROM match_order mo
LEFT JOIN entries e ON mo.first_player = e.box_position
LEFT JOIN entries e1 ON mo.second_player = e1.box_position
WHERE e.round_id = e1.round_id AND e.league_id = e1.league_id AND e.box_id = e1.box_id
ORDER BY round_id, league_id, box_id, match_order_id
Any help & advise would be greatly appreciated.
Thank you
Assuming match_id is an auto-increment column, you have the data for the other columns. You can just add the INSERT statement before your SELECT.
INSERT INTO matches(round_id, leage_id, box_id, match_order_id, player1, player2)
SELECT e.round_id, e.league_id, e.box_id, mo.match_order_id, e.entry_id as player1, e1.entry_id as player2
FROM match_order mo
LEFT JOIN entries e ON mo.first_player = e.box_position
LEFT JOIN entries e1 ON mo.second_player = e1.box_position
WHERE e.round_id = e1.round_id AND e.league_id = e1.league_id AND e.box_id = e1.box_id

Fetch rows which belonging to the parent category which has more than two rows

I have some combination of company and members
Member Table
id company_id companymember
1 1 john
2 1 Tam
3 2 haya
4 1 lee
5 3 kih
6 3 wild
7 3 cream
8 3 earth
What I want to pick up is
the 3 member names which belonging to the company which has more than two members
What I want is like this
company_id 2 has only 1 member, 3rd row is not selected
company_id 3 has 4 members, so 8th row is not selected
My Goal
1 1 john
2 1 Tam
4 1 lee
5 3 kih
6 3 wild
7 3 cream
I could make it , pick up company_ids first and
loop each id by script and fetch.
However in this way, it exec sql many times.
Is there any good way to do this on MySql by one sentence SQL??
Try this
select id,company_id,companyMember
from (Select id
,company_id
,companyMember
,Row_Number() OVER(PARTITION BY company_id ORDER By company_id) AS TotalCount
from MemberTable
) as Table1
where TotalCount <=3 and Company_id in(
Select Company_id
from MemberTable
group by Company_id
having COUNT(Company_id) >=3
)
order by id

Count distinct values on 2 colums when using GROUP BY

Considering the following query:
SELECT t.recording_id, m.release_id
FROM track t
JOIN medium m ON t.medium_id = m.medium_id
i get a result set similar to this one
recording id release id
----------------------------------
1 25
1 25
1 37
1 76
1 300
1 336
2 37
... ...
i need to output the following
recording id count
---------------------------------------------------
1 5
2 1
In other words, i need to group by the recording_id but not count the release_id duplicates for that recording_id
After researching this board i've tried the following, with no success :
SELECT t.recording_id, count(t.recording_id)
FROM track t
JOIN medium m ON t.medium_id = m.medium_id
group by t.recording_id, m.release_id
but, im getting
recording id release id
--------------------------
1 2
1 1
1 1
1 1
1 1
2 1
What's wrong?
Try this, you can use distinct in your count function to return distinct release ids for a recording_id
SELECT t.recording_id, count(distinct m.release_id) cnt
FROM track t
JOIN medium m ON t.medium_id = m.medium_id
group by t.recording_id

MySQL - Select from two rows by a shared value where the query values are different

Very difficult to phrase this. Let me explain. I have a few tables: teams, games, and team_games. Team_games has a "standing" field which is 0'd by default. If standing is greater than 0, the game is finished and scores have been reported. I'm trying to come up with a query that will take two team IDs, and count the number of games they have in common where the standing is greater than zero.
Here's the table. So far I've done everything except for what I want.
Teams
---------------------
team_id team_name
---------------------
1 Test
2 Test 2
Games
---------------------
game_id start_time
---------------------
1 (unix)
2 -
3 -
Team Games
---------------------------------
game_id team_id standing
---------------------------------
1 1 1
1 2 2
2 1 2
2 2 1
3 1 1
3 2 2
So again, I want to capture just the rows where team_id is either 1 or 2, and the game id is the same, then count the unique game IDs.
So if I was looking for the number of games which were finished between team one and team two, the final result would be:
-----
COUNT
-----
3
SELECT COUNT(*) totalgames
FROM
(
SELECT Game_ID
FROM TeamGames
WHERE team_ID IN (1, 2) -- specify the teams here
GROUP BY Game_ID
HAVING COUNT(*) = 2 -- number of teams specified
AND SUM(standing = 0) = 0
) s
SQLFiddle Demo
OUTPUT
╔════════════╗
║ TOTALGAMES ║
╠════════════╣
║ 3 ║
╚════════════╝
i'm not sure but this may work
select count(distinct game_id) as games_number from Team_Games where (team_id = 1 or team_id = 2) and standing >= 1;
You can remove team from select clause and as well as group by, if you want.
SELECT T1.TEAM_ID, T2.TEAM_ID, COUNT(*)
FROM
TEAM_GAMES T1, TEAM_GAMES T2
WHERE T2.TEAM_ID <> T1.TEAM_ID
AND T2.GAME_ID = T2.GAME_ID
AND T1.STANDING > 0 AND T2.STANDING > 0
GROUP BY T1.TEAM_ID, T2.TEAM_ID