I need to normalise a table which contains cricket players:
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| matchID | int(11) | NO | PRI | 0 | |
| innings | int(11) | NO | PRI | 0 | |
| Bat1Name | varchar(30) | YES | | NULL | |
| Bat1Score | int(11) | YES | | NULL | |
| balls1 | int(11) | YES | | NULL | |
| sixes1 | int(11) | YES | | NULL | |
| fours1 | int(11) | YES | | NULL | |
| out1 | varchar(10) | YES | | NULL | |
| catcher1 | varchar(30) | YES | | NULL | |
| bowler1 | varchar(30) | YES | | NULL | |
| Bat2Name | varchar(30) | YES | | NULL | |
| Bat2Score | int(11) | YES | | NULL | |
| balls2 | int(11) | YES | | NULL | |
| sixes2 | int(11) | YES | | NULL | |
| fours2 | int(11) | YES | | NULL | |
| out2 | varchar(10) | YES | | NULL | |
| catcher2 | varchar(30) | YES | | NULL | |
| bowler2 | varchar(30) | YES | | NULL | |
| Bat3Name | varchar(30) | YES | | NULL | |
| Bat3Score | int(11) | YES | | NULL | |
| balls3 | int(11) | YES | | NULL | |
| sixes3 | int(11) | YES | | NULL | |
| fours3 | int(11) | YES | | NULL | |
| out3 | varchar(10) | YES | | NULL | |
| catcher3 | varchar(30) | YES | | NULL | |
| bowler3 | varchar(30) | YES | | NULL | |
| Bat4Name | varchar(30) | YES | | NULL | |
| Bat4Score | int(11) | YES | | NULL | |
| balls4 | int(11) | YES | | NULL | |
| sixes4 | int(11) | YES | | NULL | |
| fours4 | int(11) | YES | | NULL | |
| out4 | varchar(10) | YES | | NULL | |
| catcher4 | varchar(30) | YES | | NULL | |
| bowler4 | varchar(30) | YES | | NULL | |
| Bat5Name | varchar(30) | YES | | NULL | |
| Bat5Score | int(11) | YES | | NULL | |
| balls5 | int(11) | YES | | NULL | |
| sixes5 | int(11) | YES | | NULL | |
| fours5 | int(11) | YES | | NULL | |
| out5 | varchar(10) | YES | | NULL | |
| catcher5 | varchar(30) | YES | | NULL | |
| bowler5 | varchar(30) | YES | | NULL | |
| Bat6Name | varchar(30) | YES | | NULL | |
| Bat6Score | int(11) | YES | | NULL | |
| balls6 | int(11) | YES | | NULL | |
| sixes6 | int(11) | YES | | NULL | |
| fours6 | int(11) | YES | | NULL | |
| out6 | varchar(10) | YES | | NULL | |
| catcher6 | varchar(30) | YES | | NULL | |
| bowler6 | varchar(30) | YES | | NULL | |
| Bat7Name | varchar(30) | YES | | NULL | |
| Bat7Score | int(11) | YES | | NULL | |
| balls7 | int(11) | YES | | NULL | |
| sixes7 | int(11) | YES | | NULL | |
| fours7 | int(11) | YES | | NULL | |
| out7 | varchar(10) | YES | | NULL | |
| catcher7 | varchar(30) | YES | | NULL | |
| bowler7 | varchar(30) | YES | | NULL | |
| Bat8Name | varchar(30) | YES | | NULL | |
| Bat8Score | int(11) | YES | | NULL | |
| balls8 | int(11) | YES | | NULL | |
| sixes8 | int(11) | YES | | NULL | |
| fours8 | int(11) | YES | | NULL | |
| out8 | varchar(10) | YES | | NULL | |
| catcher8 | varchar(30) | YES | | NULL | |
| bowler8 | varchar(30) | YES | | NULL | |
| Bat9Name | varchar(30) | YES | | NULL | |
| Bat9Score | int(11) | YES | | NULL | |
| balls9 | int(11) | YES | | NULL | |
| sixes9 | int(11) | YES | | NULL | |
| fours9 | int(11) | YES | | NULL | |
| out9 | varchar(10) | YES | | NULL | |
| catcher9 | varchar(30) | YES | | NULL | |
| bowler9 | varchar(30) | YES | | NULL | |
| Bat10Name | varchar(30) | YES | | NULL | |
| Bat10Score | int(11) | YES | | NULL | |
| balls10 | int(11) | YES | | NULL | |
| sixes10 | int(11) | YES | | NULL | |
| fours10 | int(11) | YES | | NULL | |
| out10 | varchar(10) | YES | | NULL | |
| catcher10 | varchar(30) | YES | | NULL | |
| bowler10 | varchar(30) | YES | | NULL | |
| Bat11Name | varchar(30) | YES | | NULL | |
| Bat11Score | int(11) | YES | | NULL | |
| balls11 | int(11) | YES | | NULL | |
| sixes11 | int(11) | YES | | NULL | |
| fours11 | int(11) | YES | | NULL | |
| out11 | varchar(10) | YES | | NULL | |
| catcher11 | varchar(30) | YES | | NULL | |
| bowler11 | varchar(30) | YES | | NULL | |
| extras | int(11) | YES | | NULL | |
| wides | int(11) | YES | | NULL | |
| noBalls | int(11) | YES | | NULL | |
| byes | int(11) | YES | | NULL | |
| legByes | int(11) | YES | | NULL | |
| score | int(11) | YES | | NULL | |
| wickets | int(11) | YES | | NULL | |
| overs | float | YES | | NULL | |
| runrate | float | YES | | NULL | |
| team | varchar(100) | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
The rows I am interested in is all Bat?Name and Team fields eg. Bat1Name, Bat2Name....Bat11Name and the team they belong to.
I have created a player 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 | |
+----------------+--------------+------+-----+---------+----------------+
3 rows in set (0.03 sec)
I successfully selected the player's names (without duplicates) into the player's table using the below UNION code:
INSERT INTO players (player_surname)
SELECT DISTINCT bat1Name from details
UNION
SELECT DISTINCT bat2Namen from details
UNION
SELECT DISTINCT bat3Name from details
(etc............................)
SELECT DISTINCT bat11Name from details;
This gave me 300 records which is correct.
But then realised I needed to include the teams so I added team:
INSERT INTO players (player_surname, team)
SELECT DISTINCT bat1Name,team from details
UNION
SELECT DISTINCT bat2Namen,team from details
UNION
SELECT DISTINCT bat3Name,team from details
(etc.............................)
SELECT DISTINCT bat11Name,team from details;
But this provides me with 310 records and says (for example) that Smith plays for UK, New Zealand and south africa but this is incorrect. It has also incorrectly pulled out a couple of others. I'm at a loss!
I assume my method of UNION is flawed so how can I get all batsmen out of the table with their corresponding team?
If there are two players with the same surnames that were playing in the different teams they will merge in the first query but not the second:
bat1 bat2 team
Smith Jones UK
Doe Smith Zealand
The first query will return Smith once, the second one twice.
It is most probable that your first query is wrong, not the second one. Try running this for your first query:
SELECT batname
FROM (
SELECT bat1Name AS batname, team
FROM details
UNION
SELECT bat2Name, team
FROM details
UNION …
) q
You don't need to do DISTINCT here: UNION will take care of this automatically.
As Quassnoi says it is probably the first query that is wrong as the second one will pull out the right data.
Also the player table should only store the player related data with a relation to another table storing the teams.
Related
I have the next GUI made in Python which lets me to load all the data saved in a MySQL table for tenants, and just one data from a related table called 'contracts' (marked with a red circle).
enter image description here
The Python code I use for loading this form is the next one:
def fill_entries():
i = lb.curselection()[0]
valor = lb.get(i)
nombs, apells = _arrendatarios[valor]
cursor.execute("SELECT *, c_cod FROM arrendatarios LEFT JOIN contratos ON arrendatarios.a_cc = contratos.a_cc WHERE a_nombres = %s AND a_apellidos = %s", (nombs, apells))
result = cursor.fetchall()
connect.commit()
for item in result:
d1 = item[1] #ID
d2 = item[2] #Mr/Mrs
d3 = item[3] #City
d4 = item[4] #Names
d5 = item[5] #Last names
#------------And so on
d53 = item[53] # Num Contract
cedula.set(d1)
titulo.set(d2)
residencia.set(d3)
nombres.set(d4)
apellidos.set(d5)
direccion.set(d6)
#------------And so on
numcontract.set(d53)# Num Contract
But the thing is that what I want from the contracts table is the c_cod data, not de c_id, because part of this table description is this:
MariaDB> desc contratos;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| c_id | int(11) | NO | UNI | NULL | auto_increment |
| c_cod | int(11) | NO | PRI | NULL | |
| a_cc | varchar(50) | YES | MUL | NULL | |
| inquilino | varchar(100) | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
And if, for example, I run the query in MySQL I got this:
MariaDB> SELECT arrendatarios.a_cc, c_cod FROM arrendatarios LEFT JOIN contratos ON arrendatarios.a_cc = contratos.a_cc;
+------------+-------+
| a_cc | c_cod |
+------------+-------+
| 900157048 | NULL |
| 71337237 | NULL |
| 43057196 | 22789 |
| 542550033 | NULL |
| 3502278 | NULL |
| 3472265 | NULL |
| 32460023 | NULL |
| And so on... |
+------------+-------+
34 rows in set (0.01 sec)
But Python is not drawing me the c_cod from contracts table but the c_id as if I were running:
MariaDB> SELECT arrendatarios.a_cc, c_id FROM arrendatarios LEFT JOIN contratos ON arrendatarios.a_cc = contratos.a_cc;
+------------+------+
| a_cc | c_id |
+------------+------+
| 900157048 | NULL |
| 71337237 | NULL |
| 43057196 | 8 |
| 542550033 | NULL |
| 3502278 | NULL |
| 3472265 | NULL |
| 32460023 | NULL |
| And so on... |
+------------+------+
34 rows in set (0.00 sec)
No matter what column I write, it always draws me the same result, the c_id. I'm a newbie with MySQL, I just new the basic. So, am I using wrong LEFT JOIN? Because what I want is just to show tenant info and this contract number n matter if it has it or not.
Here's the description of the tenants table too.
MariaDB> desc arrendatarios;
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| a_id | int(11) | NO | UNI | NULL | auto_increment |
| a_cc | varchar(50) | NO | PRI | NULL | |
| a_titulo | varchar(20) | YES | | NULL | |
| a_reside | varchar(50) | YES | | NULL | |
| a_nombres | varchar(50) | YES | | NULL | |
| a_apellidos | varchar(50) | YES | | NULL | |
| a_direccion | varchar(50) | YES | | NULL | |
| a_telefono | varchar(50) | YES | | NULL | |
| a_envio | varchar(50) | YES | | NULL | |
| a_email | varchar(50) | YES | | NULL | |
| a_celular | varchar(50) | YES | | NULL | |
| a_dia | int(11) | YES | | NULL | |
| a_mes | varchar(10) | YES | | NULL | |
| a_cumple | date | YES | | NULL | |
| a_profesion | varchar(50) | YES | | NULL | |
| a_empresa | varchar(50) | YES | | NULL | |
| a_oficina | varchar(50) | YES | | NULL | |
| a_tel | varchar(50) | YES | | NULL | |
| a_fax | varchar(50) | YES | | NULL | |
| a_banco | varchar(50) | YES | | NULL | |
| a_tcuenta | varchar(10) | YES | | NULL | |
| a_numcuenta | varchar(50) | YES | | NULL | |
| a_tpersona | int(11) | YES | | NULL | |
| a_retefuente | int(11) | YES | | NULL | |
| a_reteiva | int(11) | YES | | NULL | |
| a_contribuyente | int(11) | YES | | NULL | |
| a_gfactura | int(11) | YES | | NULL | |
| a_gcheque | int(11) | YES | | NULL | |
| a_nota | varchar(200) | YES | | NULL | |
| co1_cc | varchar(50) | YES | | NULL | |
| co1_nombres | varchar(50) | YES | | NULL | |
| co1_dir | varchar(50) | YES | | NULL | |
| co1_tel1 | varchar(50) | YES | | NULL | |
| co1_cargo | varchar(50) | YES | | NULL | |
| co1_empresa | varchar(59) | YES | | NULL | |
| co1_oficina | varchar(50) | YES | | NULL | |
| co1_tel2 | varchar(50) | YES | | NULL | |
| co2_cc | varchar(50) | YES | | NULL | |
| co2_nombres | varchar(50) | YES | | NULL | |
| co2_dir | varchar(50) | YES | | NULL | |
| co2_tel1 | varchar(50) | YES | | NULL | |
| co2_cargo | varchar(50) | YES | | NULL | |
| co2_empresa | varchar(59) | YES | | NULL | |
| co2_oficina | varchar(50) | YES | | NULL | |
| co2_tel2 | varchar(50) | YES | | NULL | |
| co3_cc | varchar(50) | YES | | NULL | |
| co3_nombres | varchar(50) | YES | | NULL | |
| co3_dir | varchar(50) | YES | | NULL | |
| co3_tel1 | varchar(50) | YES | | NULL | |
| co3_cargo | varchar(50) | YES | | NULL | |
| co3_empresa | varchar(59) | YES | | NULL | |
| co3_oficina | varchar(50) | YES | | NULL | |
| co3_tel2 | varchar(50) | YES | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
53 rows in set (0.06 sec)
As commented this solved the problem:
cursor.execute("SELECT arrendatarios.*, c_cod FROM arrendatarios LEFT JOIN contratos ON arrendatarios.a_cc = contratos.a_cc WHERE a_nombres = %s AND a_apellidos = %s", (nombs, apells))
I am also looking for recursive select in MySQl.
I have a table as
MariaDB [marin_bi_dim]> desc dim_sf_account;
+---------------------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------------------+--------------+------+-----+---------+-------+
| Id | varchar(512) | YES | MUL | NULL | |
| Name | varchar(512) | YES | MUL | NULL | |
| Customer__c | varchar(128) | YES | | NULL | |
| ParentId | varchar(18) | YES | | NULL | |
| Account_ID_18__c | varchar(256) | YES | MUL | NULL | |
| Account_Tier_manual__c | varchar(256) | YES | | NULL | |
| Agency_of_Record__c | varchar(256) | YES | | NULL | |
| Type | varchar(256) | YES | | NULL | |
| Subtype__c | varchar(256) | YES | | NULL | |
| Territory__c | varchar(256) | YES | | NULL | |
| Region__c | varchar(256) | YES | | NULL | |
| OwnerId | varchar(256) | YES | | NULL | |
| Sales_Owner__c | varchar(256) | YES | | NULL | |
| Customer_Engagement_Manager__c | varchar(256) | YES | | NULL | |
| Tier__c | varchar(256) | YES | | NULL | |
| Account_Tier__c | varchar(256) | YES | | NULL | |
| BillingCity | varchar(256) | YES | | NULL | |
| Last_MRR__c | varchar(256) | YES | | NULL | |
| Average_Monthly_Revenue__c | varchar(256) | YES | | NULL | |
| Global_Ultimate_Account_Name__c | varchar(256) | YES | | NULL | |
| pw_cc__BillingCountryLookup__c | varchar(256) | YES | | NULL | |
| BillingCountry | varchar(256) | YES | | NULL | |
| Account_Status__c | varchar(256) | YES | | NULL | |
| Social_Media_Consultant__c | varchar(256) | YES | | NULL | |
| Search_Customer__c | varchar(256) | YES | | NULL | |
| Social_Customer__c | varchar(256) | YES | | NULL | |
| Display_Customer__c | varchar(256) | YES | | NULL | |
| Social_Agency_of_Record__c | varchar(256) | YES | | NULL | |
| Network_Agency__c | varchar(256) | YES | | NULL | |
+---------------------------------+--------------+------+-----+---------+-------+
29 rows in set (0.00 sec)
Detail: Every employee has Id, parent_id is the boss's Id of Id. Parent_id can also be in Id and they may have parent_id or not. Id's that do not have parent_id could be the head of the tree. There is no one tree. Many trees could be formed.
Is it possible at all through MySQL select statement or do I need to create a bash script to execute this ?
I have a table called organization which has a column treeId. Tree id is used to manage wich organization is allowed to watch the profile of the other sub organizations. The values of the treeId in the table could be
1|2|44|23|56|12|
1|2|44|23|56|5|
1|2|42|21|52|
Now I want to show all organization name which comes after the searched organizationId also if I would look for 44 --> I want to show the organization name of 56, 12, 5 also all number after 23 without repetition. Is there a function in mysql to manage it?
organization table
+------------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| lastWrite | datetime | NO | | NULL | |
| status | varchar(1) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| city | varchar(100) | YES | | NULL | |
| email | varchar(100) | YES | | NULL | |
| homepage | varchar(100) | YES | | NULL | |
| mobile | varchar(30) | YES | | NULL | |
| nameAffix | varchar(100) | YES | | NULL | |
| notice | varchar(500) | YES | | NULL | |
| phone | varchar(30) | YES | | NULL | |
| poBox | varchar(20) | YES | | NULL | |
| street | varchar(100) | YES | | NULL | |
| zipCode | varchar(20) | YES | | NULL | |
| fax | varchar(30) | YES | | NULL | |
| providerId | bigint(20) | YES | MUL | NULL | |
| treeId | varchar(255) | YES | | NULL | |
| showCopyright | bit(1) | NO | | b'1' | |
| type | varchar(30) | YES | | NULL | |
| trackingUserInterface | varchar(20) | YES | | NULL | |
| country | varchar(20) | YES | | NULL | |
| iconStrategy | varchar(20) | YES | | NULL | |
| notificationInterval | bigint(20) | YES | | NULL | |
| refreshInterval | bigint(20) | YES | | NULL | |
| dateFormat | varchar(20) | YES | | NULL | |
| dateTimeFormat | varchar(20) | YES | | NULL | |
| locale | varchar(20) | YES | | NULL | |
| timeFormat | varchar(20) | YES | | NULL | |
| timeZone | varchar(20) | YES | | NULL | |
| defaultOrganization | bit(1) | YES | | NULL | |
| longDateFormat | varchar(20) | YES | | NULL | |
| showAllStatus | bit(1) | YES | | NULL | |
| coordinateFormat | varchar(20) | YES | | NULL | |
| params | text | YES | | NULL | |
| mapAndReportWidth | int(11) | YES | | NULL | |
| reverseGeocoderGroupId | bigint(20) | YES | MUL | NULL | |
| memo | text | YES | | NULL | |
| redirectAfterLogin | varchar(100) | YES | | NULL | |
+------------------------+--------------+------+-----+---------+----------------+
I have a Parent table called Run, that has Shifts as a child foreign key and those shifts have descriptions in their table. I need to select the Description of a child shift in a query and access the Description of that shift to compare to another shift.
here are the table descriptions:
Run
+----------------------+--------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+--------------+------+-----+-------------------+-----------------------------+
| RunId | int(11) | NO | PRI | NULL | auto_increment |
| RunType | varchar(1) | YES | | NULL | |
| Plant | varchar(2) | NO | MUL | NULL | |
| Warehouse | varchar(20) | YES | | NULL | |
| LegacyId | int(11) | YES | | NULL | |
| RunDate | date | NO | | NULL | |
| Shift | varchar(20) | NO | | NULL | |
| NumEmployees | int(11) | YES | | NULL | |
| DryersRan | tinyint(1) | YES | | NULL | |
| HogfuelDelivered | int(11) | YES | | NULL | |
| ScheduledStart | datetime | YES | | NULL | |
| ScheduledEnd | datetime | YES | | NULL | |
| ScheduledHours | decimal(4,2) | YES | | NULL | |
| Downtime | int(11) | YES | | NULL | |
| ProductLength | varchar(10) | YES | | NULL | |
| ProductWidth | varchar(10) | YES | | NULL | |
| ProductThickness | varchar(10) | YES | | NULL | |
| Species | varchar(10) | YES | MUL | NULL | |
| NumBlocks | int(11) | YES | | NULL | |
| TestRun | tinyint(1) | YES | | NULL | |
| AveDiameter | decimal(5,2) | YES | | NULL | |
| BlockScale | int(11) | YES | | NULL | |
| LogScale | int(11) | YES | | NULL | |
| NumSpinouts | int(11) | YES | | NULL | |
| Pieces | int(11) | YES | | NULL | |
| Footage | int(11) | YES | | NULL | |
| Surface | int(11) | YES | | NULL | |
| Coreline | int(11) | YES | | NULL | |
| Dryer1 | int(11) | YES | | NULL | |
| Dryer2 | int(11) | YES | | NULL | |
| Dryer3 | int(11) | YES | | NULL | |
| Sander | int(11) | YES | | NULL | |
| Redry | int(11) | YES | | NULL | |
| GradeStamped | tinyint(1) | YES | | NULL | |
| Status | varchar(10) | YES | | NULL | |
| Notes | text | YES | | NULL | |
| InventoryTransaction | int(11) | YES | | NULL | |
| LastEdited | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| LastEditedBy | varchar(20) | YES | | NULL | |
| LegacyRun | int(11) | YES | | NULL | |
| WorkCenter | varchar(20) | YES | MUL | NULL | |
+----------------------+--------------+------+-----+-------------------+-----------------------------+
RunItem:
+--------------+-------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+-------------------+-----------------------------+
| ItemId | int(11) | NO | PRI | NULL | auto_increment |
| ItemType | varchar(2) | NO | | NULL | |
| Run | int(11) | YES | MUL | NULL | |
| LegacyRun | int(11) | YES | | NULL | |
| WorkCenter | varchar(20) | YES | | NULL | |
| Product | int(11) | YES | MUL | NULL | |
| DayProduced | date | YES | | NULL | |
| Source | varchar(20) | YES | | NULL | |
| SourceShift | varchar(20) | YES | | NULL | |
| PieceCount | int(11) | YES | | NULL | |
| Surface | int(11) | YES | | NULL | |
| Coreline | int(11) | YES | | NULL | |
| Footage | int(11) | YES | | NULL | |
| LastEdited | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| LastEditedBy | varchar(20) | YES | | NULL | |
| Bundle | int(11) | YES | MUL | NULL | |
+--------------+-------------+------+-----+-------------------+-----------------------------+
This is the Query that I've tried so far and it wont let me do (i.Shift iss) how can this be done?
update RunItem i, Run r, i.Shift iss, r.Shift rs
set i.ItemId = (SELECT RunId from Run r where r.LegacyRun = i.LegacyRun and
rs.Description
= is.Description
and r.WorkCenter != i.WorkCenter;
gives this error:
ERROR 1146 (42S02): Table 'i.Shift' doesn't exist
UPDATE:
select r.RunId, i.Run, r.WorkCenter as runWork, i.WorkCenter as itemWork,
r.Footage, i.LegacyRun as itemLegacy from Run r, RunItem i where r.RunId = i.Run
and r.WorkCenter != i.WorkCenter and RunDate >= '2016-02-01' and RunDate <= '2016-02-29';
| 39550 | 39550 | P3-LAYUP | P3-SPREADER | 260818 | 21280 |
| 39553 | 39553 | P3-LAYUP | P3-SPREADER | 267421 | 21281 |
| 39566 | 39566 | P3-8FTSAWLINE | P3-SPREADER | 351547 | 21286 |
| 39569 | 39569 | P3-8FTSAWLINE | P3-SPREADER | 527049 | 21287 |
| 39605 | 39605 | P3-8FTSAWLINE | P3-SPREADER | 460826 | 21316 |
| 39605 | 39605 | P3-8FTSAWLINE | P3-SPREADER | 460826 | 21316 |
| 39605 | 39605 | P3-8FTSAWLINE | P3-SPREADER | 460826 | 21316 |
| 39608 | 39608 | P3-8FTSAWLINE | P3-SPREADER | 458272 | 21317 |
| 39625 | 39625 | P3-8FTSAWLINE | P3-SPREADER | 503324 | 21327 |
| 39628 | 39628 | P3-LAYUP | P3-SPREADER | 339615 | 21328 |
| 39628 | 39628 | P3-LAYUP | P3-SPREADER | 339615 | 21328 |
+-------+-------+---------------+-------------+---------+------------+
select RunId, WorkCenter, Shift,
LegacyRun from Run where Footage is null
and RunDate >='2016-02-01' and RunDate <='2016-02-29';
| 39552 | P3-SPREADER | P3-DAY-SPREAD | 21280 |
| 39555 | P3-SPREADER | P3-SWING-SPREAD | 21281 |
| 39568 | P3-SPREADER | P3-DAY-SPREAD | 21286 |
| 39571 | P3-SPREADER | P3-SWING-SPREAD | 21287 |
| 39607 | P3-SPREADER | P3-DAY-SPREAD | 21316 |
| 39609 | P3-SPREADER | P3-SWING-SPREAD | 21317 |
| 39626 | P3-SPREADER | P3-DAY-SPREAD | 21327 |
| 39629 | P3-SPREADER | P3-SWING-SPREAD | 21328 |
| 39647 | P3-8FTSAWLINE | P3-DAY-SL | 21339 |
| 39649 | P3-8FTSAWLINE | P3-SWING-SL | 21340 |
+-------+---------------+-----------------+-----------+
In the table above I'm selecting the runs where the RunItem WorkCenter and Run WorkCenter don't match, this tells me that under that Run
Some of the RunItems are pointing to the wrong run. The table below is the Table where Runs have a footage value of zero which means they don't have runItems pointing to them.
So what I need is when the RunItem WorkCenter and Run WorkCenter don't match grab the LegacyRun(RunId) and that Legacy RunShift, point all the RunItems where the workCenters don't match to the Run
that has the same LegacyRun(Id) and same Shift.Description and the LegacyRun.
UPDATE_2:
lets take the first from the top table
| 39550 | 39550 | P3-LAYUP | P3-SPREADER | 260818 | 21280 |
notice that the legacyRun(Id) is 21280
it just so happends that the top rows of the bottom Table also has the LegacyRun of 21280
if we select ItemId, WorkCenter from RunItem where Run = 39550(run from the top table where the runItem workcenter doesnt math the run);
we get:
+--------+-------------+
| ItemId | WorkCenter |
+--------+-------------+
| 110336 | P3-LAYUP |
| 110344 | P3-SPREADER |
+--------+-------------+
we know we have to point that runItem with the WorkCenter of spreader to First row of the bottom table that doesn't have runItems pointing to it.
so to fix this one row we do:
update RunItem set Run = 39552 where ItemId = 110344;
I could go through and manually do this but I want something that can update all rows
I got the Query I needed thanks to #Barmar:
UPDATE RunItem i JOIN Run r
ON r.LegacyRun = i.LegacyRun
AND r.WorkCenter = i.WorkCenter
JOIN Run rr
ON rr.RunId = i.Run
SET i.Run = r.RunId
where i.WorkCenter != rr.WorkCenter;
This is a much simpler and elegant Query once I added the JOIN ON.
I want to get location from members IP. I can use the command on phpmyadmin, and it costs about 2 seconds. But it will be stuck when the command is executed on MySQL through terminal. How can I fix the problem.
SQL Command:
SELECT
ID,country
FROM
(SELECT ID,substring_index(members.server_ids,',',1) AS IP FROM members) AS A
JOIN
(SELECT country,start,end FROM ip) AS B
ON
INET_ATON(A.IP) BETWEEN INET_ATON(B.start) AND INET_ATON(B.end);
desc members:
+---------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+--------------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| USER_NAME | varchar(100) | NO | UNI | NULL | |
| PASSWORD | varchar(32) | YES | | NULL | |
| NAME | varchar(100) | YES | | NULL | |
| EMAIL | varchar(50) | YES | MUL | NULL | |
| SAFE_EMAIL | varchar(50) | YES | | NULL | |
| NICK_NAME | varchar(100) | YES | MUL | NULL | |
| GENDER | tinyint(1) | YES | | 0 | |
| BIRTHDAY | date | YES | | NULL | |
| CREATEDAY | datetime | YES | | NULL | |
| PHONE | varchar(20) | YES | | NULL | |
| MOBILE | varchar(15) | YES | | NULL | |
| ID_CARD | varchar(32) | YES | | NULL | |
| COUNTY | int(11) | YES | | NULL | |
| ADDRESS | varchar(255) | YES | | NULL | |
| LOGIN_TIME | datetime | YES | | NULL | |
| BUY_SN | varchar(50) | YES | | NULL | |
| BUY_KIND | varchar(50) | YES | | NULL | |
| PARTNER_SN | varchar(50) | YES | | NULL | |
| cookie_value | varchar(100) | YES | | NULL | |
| CONSIGNEE_INFO | int(11) | YES | MUL | NULL | |
| LOGIN_COUNT | int(11) | YES | | NULL | |
| PAY_COUNT | int(11) | YES | | NULL | |
| status | tinyint(4) | YES | | 1 | |
| experience | int(11) | YES | | 0 | |
| amount | int(11) | YES | | 0 | |
| score | int(11) | YES | | 0 | |
| is_facebook_account | tinyint(4) | YES | | 0 | |
| avatar | varchar(200) | YES | | NULL | |
| avatar_status | tinyint(1) | YES | | 0 | |
| member_authhash | varchar(50) | YES | | NULL | |
| job | varchar(50) | YES | | NULL | |
| server_ids | varchar(255) | YES | | NULL | |
+---------------------+--------------+------+-----+---------+----------------+
SELECT ID,server_ids FROM members LIMIT 10000,1;
+-------+--------------------------+
| ID | server_ids |
+-------+--------------------------+
| 20005 | 61.20.167.219, 127.0.0.1 |
+-------+--------------------------+
desc ip:
+---------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| country | char(2) | NO | | NULL | |
| type | char(4) | NO | | NULL | |
| start | varchar(15) | NO | MUL | NULL | |
| end | varchar(15) | NO | MUL | NULL | |
+---------+------------------+------+-----+---------+----------------+
SELECT * FROM ip LIMIT 100,1;
+-----+---------+------+-----------+---------------+
| id | country | type | start | end |
+-----+---------+------+-----------+---------------+
| 101 | IN | ipv4 | 1.187.0.0 | 1.187.255.255 |
+-----+---------+------+-----------+---------------+
phpMyAdmin automatically adds a LIMIT clause to the query. Because you do not have an ODER BY clause, MySQL sends the results as soon as it has found the number of records that you requested in the LIMIT clause.
Add a LIMIT clause to the query that you execute on the command line.