MYSQL: How to find player_id from surname? - mysql

I'm now trying to populate my 'testMatch' table (below) with data from my unormalised 'summary' table:
TESTMATCH TABLE
+------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+-------+
| match_id | int(11) | NO | PRI | NULL | |
| match_date | date | YES | | NULL | |
| ground | varchar(50) | YES | MUL | NULL | |
| homeTeam | varchar(100) | YES | MUL | NULL | |
| awayTeam | varchar(100) | YES | MUL | NULL | |
| matchResult | varchar(100) | YES | MUL | NULL | |
| manOfMatch | varchar(30) | YES | | NULL | |
| homeTeam_captain | int(10) | YES | MUL | NULL | |
| homeTeam_keeper | int(10) | YES | MUL | NULL | |
| awayTeam_captain | int(10) | YES | MUL | NULL | |
| awayTeam_keeper | int(10) | YES | MUL | NULL | |
+------------------+--------------+------+-----+---------+-------+
There is no problem populating match_id -----> manOfMatch - it is 'homeTeam_captain', 'homeTeam_keeper', 'awayTeam_captain' and 'awayTeam_keeper' that i'm having problems bringing in.
SUMMARY TABLE
mysql> DESCRIBE SUMMARY;
+-----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| matchID | int(11) | NO | PRI | NULL | |
| Test | int(11) | YES | | NULL | |
| matchDate | date | YES | | NULL | |
| Ground | varchar(50) | YES | | NULL | |
| HomeTeam | varchar(100) | YES | | NULL | |
| AwayTeam | varchar(100) | YES | | NULL | |
| matchResult | varchar(50) | YES | | NULL | |
| MarginRuns | int(11) | YES | | NULL | |
| MarginWickets | int(11) | YES | | NULL | |
| ManOfMatch | varchar(40) | YES | | NULL | |
| HomeTeamCaptain | varchar(30) | YES | | NULL | |
| HomeTeamKeeper | varchar(30) | YES | | NULL | |
| AwayTeamCaptain | varchar(30) | YES | | NULL | |
| AwayTeamKeeper | varchar(30) | YES | | NULL | |
+-----------------+--------------+------+-----+---------+-------+
I need to somehow select the data from summary, get the corresponding player_id and input the player_id into my 'testMatch'. Player table below:
PLAYERS TABLE
mysql> describe players;
+----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+----------------+
| player_id | int(11) | NO | PRI | NULL | auto_increment |
| player_surname | varchar(30) | YES | | NULL | |
| team | varchar(100) | YES | MUL | NULL | |
+----------------+--------------+------+-----+---------+----------------+
So to clarify, eg. I want to select homeTeam_captain data FROM summary table BUT not the name, I want the corresponding player_id instead.
I assume I need to use some sort of join/subqueries to get this done... i've tried selecting with:
select matchID, player_id, player_surname, team from players p, summary s
where
s.hometeamcaptain = p.player_surname ORDER BY matchID;
but this brings back 73 rows and there should only be 65 (65 matches).
I hope this makes sense and thanks for your help!!
Theresa

Are there overlapping names? If so, also assure that the teams correspond (add s.HomeTeam = p.team to the where block). If there are players with the same name in one team, you will have to solve these conflicts manually.
To select all the keepers/captains at once, you need left outer joins. I guess it will be one join per player, so you have to join the same table 4 times.
Once you've selected the right data, you can insert it in your testMatch table with INSERT ... SELECT.

Related

MySQL: Trying to return NULL values in a ER model, but not having any luck

I have created a realistic table set using the ER model for MySQL. I am attempting to do joins, but seem to either be missing something, or not doing them correctly in order to produce NULL values. Below are my created tables:
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| FilmNumber | char(3) | NO | PRI | NULL | |
| FilmName | varchar(60) | YES | | NULL | |
| OpeningDate | varchar(20) | YES | | NULL | |
| TopBilledActor | char(60) | YES | | NULL | |
| Genre | varchar(20) | YES | | NULL | |
+----------------+-------------+------+-----+---------+-------+
+------------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+-------------+------+-----+---------+-------+
| CriticSiteNumber | char(3) | NO | PRI | NULL | |
| CriticSiteName | varchar(30) | YES | | NULL | |
| CriticName | char(50) | YES | | NULL | |
+------------------+-------------+------+-----+---------+-------+
+------------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+---------+------+-----+---------+-------+
| FilmNumber | char(3) | YES | MUL | NULL | |
| CriticSiteNumber | char(3) | YES | MUL | NULL | |
| OverallScore | int(5) | YES | | NULL | |
+------------------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)
My question is, what type of join query would I need in order to produce NULL values, or does something in my table need to be modified because each of the queries I attempt do not return those values.

MySQL : why is left join slower then inner join? Optimization help required

I have a MySQL query which joins between the two table. I need to map call id from first table with second table. Second table may not have the call id, hence I need to left join the tables. Below is the query, it takes around 125 seconds to finish.
select uniqueid, TRANTAB.DISP, TRANTAB.DIAL FROM
closer_log LEFT JOIN
(select call_uniqueId, sum(dispo_duration) as DISP, sum(dialing_duration) as DIAL
from agent_transition_log group by call_uniqueId) TRANTAB
on closer_log.uniqueid=TRANTAB.call_uniqueId;
Here is the explain output of the query with left join.
+----+-------------+----------------------+-------+---------------+----------------------------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------------+-------+---------------+----------------------------+---------+------+--------+-------------+
| 1 | PRIMARY | closer_log | index | NULL | uniqueid | 43 | NULL | 37409 | Using index |
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 32535 | |
| 2 | DERIVED | agent_transition_log | index | NULL | index_agent_transition_log | 43 | NULL | 159406 | |
+----+-------------+----------------------+-------+---------------+----------------------------+---------+------+--------+-------------+
If I do the internal join, then execution time is around 2 seconds.
select uniqueid, TRANTAB.DISP, TRANTAB.DIAL FROM
closer_log JOIN
(select call_uniqueId, sum(dispo_duration) as DISP, sum(dialing_duration) as DIAL
from agent_transition_log group by call_uniqueId) TRANTAB
on closer_log.uniqueid=TRANTAB.call_uniqueId;
Explain output of query with internal join.
+----+-------------+----------------------+-------+------------------------------------+----------------------------+---------+-----------------------+--------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------------+-------+------------------------------------+----------------------------+---------+-----------------------+--------+--------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 32535 | |
| 1 | PRIMARY | closer_log | ref | uniqueid,index_closer_log | index_closer_log | 43 | TRANTAB.call_uniqueId | 1 | Using where; Using index |
| 2 | DERIVED | agent_transition_log | index | NULL | index_agent_transition_log | 43 | NULL | 159406 | |
+----+-------------+----------------------+-------+------------------------------------+----------------------------+---------+-----------------------+--------+--------------------------+
My question is, why is internal join so much faster then left join. Does my query has any logical fault which is causing the slow execution? What are my optimization options. The call ids in both the tables are indexed.
Edit 1) Added table descriptions
mysql> desc agent_transition_log;
+--------------------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+----------------------+------+-----+---------+-------+
| user_log_id | int(9) unsigned | NO | MUL | NULL | |
| event_time | datetime | YES | | NULL | |
| dispoStatus | varchar(6) | YES | | NULL | |
| call_uniqueId | varchar(40) | YES | MUL | NULL | |
| xfer_call_uid | varchar(40) | YES | | NULL | |
| pause_duration | smallint(5) unsigned | YES | | 0 | |
| wait_duration | smallint(5) unsigned | YES | | 0 | |
| dialing_duration | smallint(5) unsigned | YES | | 0 | |
| ring_wait_duration | smallint(5) unsigned | YES | | 0 | |
| talk_duration | smallint(5) unsigned | YES | | 0 | |
| dispo_duration | smallint(5) unsigned | YES | | 0 | |
| park_duration | smallint(5) unsigned | YES | | 0 | |
| rec_duration | smallint(5) unsigned | YES | | 0 | |
| xfer_wait_duration | smallint(5) unsigned | YES | | 0 | |
| logged_in_duration | smallint(5) unsigned | YES | | 0 | |
| sub_status | varchar(6) | YES | | NULL | |
+--------------------+----------------------+------+-----+---------+-------+
16 rows in set (0.00 sec)
mysql> desc closer_log;
+----------------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+----------------------+------+-----+---------+----------------+
| closecallid | int(9) unsigned | NO | PRI | NULL | auto_increment |
| lead_id | int(9) unsigned | NO | MUL | NULL | |
| list_id | bigint(14) unsigned | YES | | NULL | |
| campaign_id | varchar(20) | YES | MUL | NULL | |
| call_date | datetime | YES | MUL | NULL | |
| start_epoch | int(10) unsigned | YES | | NULL | |
| end_epoch | int(10) unsigned | YES | | NULL | |
| length_in_sec | int(10) | YES | | NULL | |
| status | varchar(6) | YES | | NULL | |
| phone_code | varchar(10) | YES | | NULL | |
| phone_number | varchar(18) | YES | MUL | NULL | |
| user | varchar(20) | YES | | NULL | |
| comments | varchar(255) | YES | | NULL | |
| processed | enum('Y','N') | YES | | NULL | |
| queue_seconds | decimal(7,2) | YES | | 0.00 | |
| user_group | varchar(20) | YES | | NULL | |
| xfercallid | int(9) unsigned | YES | | NULL | |
| uniqueid | varchar(40) | YES | MUL | NULL | |
| callerid | varchar(40) | YES | | NULL | |
| agent_only | varchar(20) | YES | | | |
| queue_position | smallint(4) unsigned | YES | | 1 | |
| root_uid | varchar(40) | YES | | NULL | |
| parent_uid | varchar(40) | YES | | NULL | |
| extension | varchar(100) | YES | | NULL | |
| alt_dial | varchar(6) | YES | | NULL | |
| talk_duration | smallint(5) unsigned | YES | | 0 | |
| did_pattern | varchar(50) | YES | | NULL | |
+----------------+----------------------+------+-----+---------+----------------+
Left join looks for the fields from left + unmatched entries from right, so it has to check every joined field in the right table which might be NULL (if you don't have an index on the fields for that JOIN, it means the query will check the whole right table every time). Inner join looks only for direct matches, so it might not have to go over the whole table to perform a join (Especially if you join on indexed fields).
By the way, if you only want to display the entries mentioned in agent_transition_log, you don't need join at all:
select call_uniqueId, sum(dispo_duration) as DISP, sum(dialing_duration) as DIAL
from agent_transition_log group by call_uniqueId;
will do the job.
OR if you do want to add the missing entries:
SELECT call_uniqueId, sum(dispo_duration) as DISP, sum(dialing_duration) as DIAL
from agent_transition_log group by call_uniqueId
UNION
SELECT uniqueid as call_uniqueid, NULL as DISP, NULL as DIAL from closer_log
WHERE uniqueid not in (SELECT call_uniqueid FROM agent_transition_log);

Select tracks and the date that has maximum visits

Here is the deal. I have two tables
tracks
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| track_id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| artist_id | int(11) | YES | | NULL | |
| genre_id | int(11) | YES | | NULL | |
| track_artist | varchar(255) | YES | | NULL | |
| track_title | varchar(255) | NO | | NULL | |
| track_lyric | text | YES | | NULL | |
| track_video | varchar(255) | YES | | NULL | |
| play_url | varchar(255) | YES | | NULL | |
| shares | int(11) | NO | | 0 | |
| likes | int(11) | NO | | 0 | |
| dislikes | int(11) | NO | | 0 | |
| is_active | enum('T','F') | NO | | T | |
| created_at | int(10) | YES | | NULL | |
+--------------+------------------+------+-----+---------+----------------+
track_visits
+----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+----------------+
| track_visit_id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| track_id | int(11) | NO | MUL | NULL | |
| ip_address | varchar(255) | NO | | NULL | |
| created_at | int(10) | YES | | NULL | |
+----------------+------------------+------+-----+---------+----------------+
And the question is:
How can I select all the tracks and the date that has maximum visits for every specific track?
Regards
Nasko
Since you are using MySql, you can use this:
SELECT tracks.*, track_visits.*
FROM
tracks LEFT JOIN (
SELECT track_id, MAX(created_at) MaxCreatedAt
FROM track_visits
GROUP BY track_id) mx
ON tracks.track_id=mx.track_id
LEFT JOIN track_visits
ON mx.track_id=track_visits.track_id
AND mx.MaxCreatedAt=track_visits.created_at
GROUP BY
tracks.track_id

Writing MySQL query with several table joins or multiple select

I am trying to write a MySQL query that gives me results of Organisation Name, its Post Code, any Events that belong to the Organisation and the Post Code of that Event. I've tried all sorts of of join, join and select combinations to no avail. Is this something that is possible ? (I could have a separate table for Org Address and Event Address but it seems like it should be possible to use just one table)
My table structures:
mysql> DESCRIBE cc_organisations;
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| user_id | int(10) unsigned | NO | MUL | NULL | |
| type | enum('C','O') | YES | | NULL | |
| name | varchar(150) | NO | MUL | NULL | |
| description | text | YES | | NULL | |
+-------------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql> DESCRIBE cc_events;
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| org_id | int(10) unsigned | NO | MUL | NULL | |
| name | varchar(150) | NO | MUL | NULL | |
| start_date | int(11) | NO | MUL | NULL | |
| end_date | int(11) | YES | MUL | NULL | |
| start_time | int(11) | NO | | NULL | |
| end_time | int(11) | NO | | NULL | |
| description | text | YES | | NULL | |
+-------------+------------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)
mysql> DESCRIBE cc_addresses;
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| org_id | int(10) unsigned | YES | MUL | NULL | |
| event_id | int(10) unsigned | YES | MUL | NULL | |
| post_code | varchar(7) | NO | MUL | NULL | |
| address_1 | varchar(100) | NO | | NULL | |
| address_2 | varchar(100) | YES | | NULL | |
| town | varchar(50) | NO | | NULL | |
| county | varchar(50) | NO | | NULL | |
| email | varchar(150) | NO | | NULL | |
| phone | int(11) | YES | | NULL | |
| mobile | int(11) | YES | | NULL | |
| website_uri | varchar(150) | YES | | NULL | |
| facebook_uri | varchar(250) | YES | | NULL | |
| twitter_uri | varchar(250) | YES | | NULL | |
+--------------+------------------+------+-----+---------+----------------+
14 rows in set (0.00 sec)
select o.Name, oAddress.PostCode, e.Name, eAddress.PostCode
from cc_organisations o
inner join cc_addresses oAddress on oAddress.org_id = o.id
left outer join cc_events e on e.org_id=o.id
inner join cc_addresses eAddress on eAddress.event_id = e.id
SELECT cco.name as OrgName, cca.post_code as OrgPostCode, cce.id,
cce.org_id, cce.name, cce.start_date, cce.end_date, cce.start_time,
cce.end_time, cce.description
FROM cc_events cce, cc_addresses cca, cc_organisations cco
WHERE cca.event_id = cce.id AND cco.id=cce.org_id
ORDER BY cce.start_date
LIMIT 50;
You can change your sort and limit, I just added those in because I don't know how big your DB is... You may even be able to get away with:
SELECT cco.name as OrgName, cca.post_code as OrgPostCode, cce.*
FROM cc_events cce, cc_addresses cca, cc_organisations cco
WHERE cca.event_id = cce.id AND cco.id=cce.org_id
ORDER BY cce.start_date LIMIT 50;
But im not 100% sure if the 2nd query will bum out or not.
Your address table has the post codes in it; but it also has an organization id and event id foreign keys. We only need to check the event_id from the address table because any event will belong to an organization.
Address's Event matched Event ID
Event's Organization matched Organization ID

mysql problem: populate table from another table but referencing ID instead of name

I'm now trying to populate my 'testMatch' table (below) with data from my 'summary' table:
TESTMATCH TABLE
+------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+-------+
| match_id | int(11) | NO | PRI | NULL | |
| match_date | date | YES | | NULL | |
| ground | varchar(50) | YES | MUL | NULL | |
| homeTeam | varchar(100) | YES | MUL | NULL | |
| awayTeam | varchar(100) | YES | MUL | NULL | |
| matchResult | varchar(100) | YES | MUL | NULL | |
| manOfMatch | varchar(30) | YES | | NULL | |
| homeTeam_captain | int(10) | YES | MUL | NULL | |
| homeTeam_keeper | int(10) | YES | MUL | NULL | |
| awayTeam_captain | int(10) | YES | MUL | NULL | |
| awayTeam_keeper | int(10) | YES | MUL | NULL | |
+------------------+--------------+------+-----+---------+-------+
There is no problem populating match_id -----> manOfMatch - it is 'homeTeam_captain', 'homeTeam_keeper', 'awayTeam_captain' and 'awayTeam_keeper' that i'm having problems bringing in.
SUMMARY TABLE
mysql> DESCRIBE SUMMARY;
+-----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| matchID | int(11) | NO | PRI | NULL | |
| Test | int(11) | YES | | NULL | |
| matchDate | date | YES | | NULL | |
| Ground | varchar(50) | YES | | NULL | |
| HomeTeam | varchar(100) | YES | | NULL | |
| AwayTeam | varchar(100) | YES | | NULL | |
| matchResult | varchar(50) | YES | | NULL | |
| MarginRuns | int(11) | YES | | NULL | |
| MarginWickets | int(11) | YES | | NULL | |
| ManOfMatch | varchar(40) | YES | | NULL | |
| HomeTeamCaptain | varchar(30) | YES | | NULL | |
| HomeTeamKeeper | varchar(30) | YES | | NULL | |
| AwayTeamCaptain | varchar(30) | YES | | NULL | |
| AwayTeamKeeper | varchar(30) | YES | | NULL | |
+-----------------+--------------+------+-----+---------+-------+
I need to somehow select the data from summary, get the corresponding player_id and input the player_id into my 'testMatch'. Player table below:
PLAYERS TABLE
mysql> describe players;
+----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+----------------+
| player_id | int(11) | NO | PRI | NULL | auto_increment |
| player_surname | varchar(30) | YES | | NULL | |
| team | varchar(100) | YES | MUL | NULL | |
+----------------+--------------+------+-----+---------+----------------+
So to clarify, eg. I want to select homeTeam_captain data FROM summary table BUT not the name, I want the corresponding player_id instead.
I assume I need to use some sort of join/subqueries to get this done... i've tried finding the correct select query first to make sure i'm pulling out the right data, and I have been using the below code for testing (thanks to user Larry_Croft for helping me with this):
select matchID, player_id, player_surname, team from players p, summary s
where
s.hometeamcaptain = p.player_surname AND s.HomeTeam = p.team ORDER BY matchID;
But this correctly brings back 65 rows (65 matches) BUT i then tried it with:
select matchID, player_id, player_surname, team from players p, summary s
where
s.hometeamKEEPER = p.player_surname AND s.HomeTeam = p.team ORDER BY matchID;
But this brings back only 61 rows (should be 65) so i must have an error in the logic.
Once I get this select to work, i then need to somehow include it into my 'INSERT INTO.....SELECT statement to put all the data into 'testMatch' table.
I hope this makes sense and thanks for your help!
Well for me it looks that there is eitehr a hometeamKEEPER that has a null value or that the value of hometeamKEEPER is not in the players table.
Using the following query you should be able to find the hometownKEEPER that are not in the players table:
SELECT matchID, player_id, player_surname, team
FROM players p
RIGHT JOIN summary s ON p.hometeamKEEPER = p.player_surname AND
s.HomeTeam = p.team
ORDER BY matchID;