there are 3 tables like this:
mysql> describe detoc;
+-----------+--------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+------------+----------------+
| registro | int(12) | NO | PRI | NULL | auto_increment |
| ordcomp | int(12) | NO | MUL | 0 | |
| requis | int(12) | YES | MUL | 0 | |
| requisnum | int(12) | YES | | 0 | |
| material | char(6) | YES | MUL | | |
| cantidad | double(18,7) | YES | | 0.0000000 | |
| unidad | char(7) | YES | | | |
| presest | double(18,7) | YES | | 0.0000000 | |
| unidadpre | char(7) | YES | | | |
| surtido | double(18,7) | YES | | 0.0000000 | |
| cerrada | tinyint(1) | YES | | 0 | |
| costo | double(18,7) | YES | | 0.0000000 | |
| impuesto | double(18,7) | YES | | 0.0000000 | |
| fechalta | date | YES | | 0000-00-00 | |
| usuarmod | char(6) | YES | | | |
| fechamod | date | YES | | 0000-00-00 | |
+-----------+--------------+------+-----+------------+----------------+
16 rows in set (0.02 sec)
mysql> describe ordenescompra;
+------------+------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------+------+-----+------------+----------------+
| registro | int(12) | NO | PRI | NULL | auto_increment |
| clave | int(12) | NO | MUL | 0 | |
| area | char(6) | NO | MUL | | |
| usuariocre | char(6) | NO | MUL | | |
| cveprov | char(6) | YES | | | |
| fechacomp | char(10) | YES | | | |
| emailconf | tinyint(1) | YES | | 0 | |
| confprov | tinyint(1) | YES | | 0 | |
| cerrada | tinyint(1) | YES | | 0 | |
| fechalta | date | YES | | 0000-00-00 | |
| usuarmod | char(6) | YES | | | |
| fechamod | date | YES | | 0000-00-00 | |
+------------+------------+------+-----+------------+----------------+
12 rows in set (0.02 sec)
mysql> describe materiales;
+-----------+--------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+------------+----------------+
| registro | int(12) | NO | PRI | NULL | auto_increment |
| clave | char(6) | NO | UNI | | |
| clave2 | char(20) | YES | MUL | | |
| descr | char(30) | YES | | | |
| descr2 | char(250) | YES | | | |
| unidad | char(7) | YES | | | |
| presest | double(18,7) | YES | | 0.0000000 | |
| unidadpre | char(7) | YES | | | |
| codbar | char(30) | YES | | | |
| impuesto | double(18,7) | YES | | 0.0000000 | |
| mantpend | tinyint(1) | YES | | 0 | |
| grupplat | char(6) | YES | MUL | | |
| fechalta | date | YES | | 0000-00-00 | |
| usuarmod | char(6) | YES | | | |
| fechamod | date | YES | | 0000-00-00 | |
+-----------+--------------+------+-----+------------+----------------+
I need to display data , but returns nothing, the syntaxis is:
SELECT * FROM DETOC INNER JOIN MATERIALES
ON DETOC.MATERIAL = MATERIALES.CLAVE
INNER JOIN ORDENESCOMPRA
ON DETOC.ORDCOMP = ORDENESCOMPRA.CLAVE
WHERE MATERIALES.CLAVE>=' '
AND MATERIALES.CLAVE<='ZZZZZZ'
AND MATERIALES.GRUPPLAT>=' '
AND MATERIALES.GRUPPLAT<='ZZZZZZ'
AND ORDENESCOMPRA.CLAVE>=' '
AND ORDENESCOMPRA.CLAVE<='999999999999'
ORDER BY ;
and i try this syntaxis too, but i have a error to show, because they're different number of columns...
(SELECT * FROM DETOC
INNER JOIN ORDENESCOMPRA
ON DETOC.ORDCOMP = ORDENESCOMPRA.CLAVE
WHERE ORDENESCOMPRA.CLAVE>="8780"
AND ORDENESCOMPRA.CLAVE<="8790"
ORDER BY CLAVE)
UNION
(SELECT * FROM DETOC
INNER JOIN MATERIALES
ON DETOC.MATERIAL = MATERIALES.CLAVE
WHERE MATERIALES.CLAVE>=""
AND MATERIALES.CLAVE<=""
AND MATERIALES.GRUPPLAT>=" "
AND MATERIALES.GRUPPLAT<=" ")
Can anybody help me ???
Please try using UPPER function while comparing MATERIALES.CLAVE and MATERIALES.GRUPPLAT with their upper bounds, like in the following query:
SELECT
*
FROM
DETOC
INNER JOIN MATERIALES
ON DETOC.MATERIAL = MATERIALES.CLAVE
INNER JOIN ORDENESCOMPRA
ON DETOC.ORDCOMP = ORDENESCOMPRA.CLAVE
WHERE
MATERIALES.CLAVE >= ' '
AND UPPER(MATERIALES.CLAVE) <= 'ZZZZZZ'
AND MATERIALES.GRUPPLAT >= ' '
AND UPPER(MATERIALES.GRUPPLAT) <= 'ZZZZZZ'
AND ORDENESCOMPRA.CLAVE >= ' '
AND ORDENESCOMPRA.CLAVE <= '999999999999';
I'm not sure whether this will solve your problem, but I'm certain that when in MATERIALES.CLAVE there would be i.e. value AABBcc the query would exclude the record from the result set.
Hope it helps some way.
Related
I have a table with the following structure:
+-----------------+--------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+-------------------+-----------------------------+
| imovel_id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | | NULL | |
| img_dest | varchar(111) | YES | | NULL | |
| data_imob | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| finalidade_imob | varchar(51) | NO | MUL | NULL | |
| status_imob | varchar(51) | NO | | NULL | |
| tipo_imob | varchar(255) | YES | | NULL | |
| uf_imob | varchar(51) | NO | MUL | NULL | |
| cidade_imob | varchar(255) | NO | | NULL | |
| bairro_imob | varchar(255) | YES | | NULL | |
| rua_imob | varchar(255) | YES | | NULL | |
| vaga_imob | varchar(255) | YES | | NULL | |
| dorms_imob | char(2) | YES | | NULL | |
| tamanho_imob | varchar(222) | YES | | NULL | |
| valor_imob | varchar(255) | YES | | 0 | |
| titulo_imob | varchar(255) | YES | | NULL | |
| descricao_imob | longtext | YES | | NULL | |
| carac_imob | varchar(255) | YES | | NULL | |
+-----------------+--------------+------+-----+-------------------+-----------------------------+
If I run this query:
SELECT * FROM form_imovel LEFT JOIN (form_user) ON (form_user.user_id = form_imovel.user_id) WHERE MATCH(finalidade_imob,status_imob,tipo_imob,uf_imob,cidade_imob,bairro_imob,dorms_imob,valor_imob) AGAINST ('Comprar' IN BOOLEAN MODE);
Where Comprar is the value from finalidade_imob column, I have the right results, that work with all columns, except uf_imobcolumn.
The uf_imobcolumns have values like: SP,MG,RJ.
If I try run the following query, it returns empty:
SELECT * FROM form_imovel LEFT JOIN (form_user) ON (form_user.user_id = form_imovel.user_id) WHERE MATCH(finalidade_imob,status_imob,tipo_imob,uf_imob,cidade_imob,bairro_imob,dorms_imob,valor_imob) AGAINST ('SP' IN BOOLEAN MODE);
Even though that column has so many SP registers.
Fiddle example
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 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 am working on a view that joins multiple table. The data will be entered on separate table based on RepairID. The view will gather the data that is linking to the RepairID. My problem is after I enter the data, the view will show all the probability of the records output. Below are my code of view:
SELECT
`new`.`SRPartsID` AS `SRPartsID`,
`new`.`RepairID` AS `RepairID`,
`new`.`SRNo` AS `SRNo`,
`new`.`DateReceived` AS `DateReceived`,
`new`.`ShipmentDate` AS `ShipmentDate`,
`tb1stdebug`.`FirstDebugTestingErrorCode` AS `FirstDebugTestingErrorCode`,
`tb1stdebug`.`FirstDebugActionTaken` AS `FirstDebugActionTaken`,
`tb1stdebug`.`FirstDebugComponentLocation` AS `FirstDebugComponentLocation`,
`tb2nddebug`.`SecondDebugTestingErrorCode` AS `SecondDebugTestingErrorCode`,
`tb2nddebug`.`SecondDebugActionTaken` AS `SecondDebugActionTaken`,
`tb2nddebug`.`SecondDebugComponentLocation` AS `SecondDebugComponentLocation`,
`tb3rddebug`.`ThirdDebugTestingErrorCode` AS `ThirdDebugTestingErrorCode`,
`tb3rddebug`.`ThirdDebugActionTaken` AS `ThirdDebugActionTaken`,
`tb3rddebug`.`ThirdDebugComponentLocation` AS `ThirdDebugComponentLocation`,
`tb1stfct`.`FirstFctTestingErrorCode` AS `FirstFctTestingErrorCode`,
`tb1stfct`.`FirstFctActionTaken` AS `FirstFctActionTaken`,
`tb1stfct`.`FirstFctComponentLocation` AS `FirstFctComponentLocation`,
`tb2ndfct`.`SecondFctTestingErrorCode` AS `SecondFctTestingErrorCode`,
`tb2ndfct`.`SecondFctActionTaken` AS `SecondFctActionTaken`,
`tb2ndfct`.`SecondFctComponentLocation` AS `SecondFctComponentLocation`,
`tb3rdfct`.`ThirdFctTestingErrorCode` AS `ThirdFctTestingErrorCode`,
`tb3rdfct`.`ThirdFctActionTaken` AS `ThirdFctActionTaken`,
`tb3rdfct`.`ThirdFctComponentLocation` AS `ThirdFctComponentLocation`
FROM
((((((`tbsrparts_new` `new`
LEFT JOIN `tb1stdebug` ON ((`new`.`RepairID` = `tb1stdebug`.`RepairID`)))
LEFT JOIN `tb2nddebug` ON ((`new`.`RepairID` = `tb2nddebug`.`RepairID`)))
LEFT JOIN `tb3rddebug` ON ((`new`.`RepairID` = `tb3rddebug`.`RepairID`)))
LEFT JOIN `tb1stfct` ON ((`new`.`RepairID` = `tb1stfct`.`RepairID`)))
LEFT JOIN `tb2ndfct` ON ((`new`.`RepairID` = `tb2ndfct`.`RepairID`)))
LEFT JOIN `tb3rdfct` ON ((`new`.`RepairID` = `tb3rdfct`.`RepairID`)))
ORDER BY `new`.`SRPartsID` DESC
This is my table structure
tbsrparts_new
+-------------------------------+--------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------------------+--------------------+------+-----+---------+----------------+
| SRPartsID | "int(10) unsigned" | NO | PRI | NULL | auto_increment |
| RepairID | varchar(200) | NO | | NULL | |
| SRNo | varchar(200) | YES | MUL | NULL | |
| DateReceived | varchar(200) | YES | | NULL | |
| ShipmentDate | varchar(200) | YES | | NULL
+-------------------------------+--------------------+------+-----+---------+----------------+
tb1stdebug
+-----------------------------+--------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------------+--------------------+------+-----+---------+----------------+
| FirstDebugID | "int(10) unsigned" | NO | PRI | NULL | auto_increment |
| RepairID | varchar(500) | YES | | NULL | |
| FirstDebugTestingErrorCode | varchar(500) | YES | MUL | NULL | |
| FirstDebugActionTaken | varchar(500) | YES | | NULL | |
| FirstDebugComponentLocation | varchar(500) | YES | | NULL | |
+-----------------------------+--------------------+------+-----+---------+----------------+
tb2nddebug
+-----------------------------+--------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------------+--------------------+------+-----+---------+----------------+
| SecondDebugID | "int(10) unsigned" | NO | PRI | NULL | auto_increment |
| RepairID | varchar(500) | YES | | NULL | |
| SecondDebugTestingErrorCode | varchar(500) | YES | MUL | NULL | |
| SecondDebugActionTaken | varchar(500) | YES | | NULL | |
| SecondDebugComponentLocation | varchar(500) | YES | | NULL | |
+-----------------------------+--------------------+------+-----+---------+----------------+
tb3rddebug
+-----------------------------+--------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------------+--------------------+------+-----+---------+----------------+
| ThirdDebugID | "int(10) unsigned" | NO | PRI | NULL | auto_increment |
| RepairID | varchar(500) | YES | | NULL | |
| ThirdDebugTestingErrorCode | varchar(500) | YES | MUL | NULL | |
| ThirdDebugActionTaken | varchar(500) | YES | | NULL | |
| ThirdDebugComponentLocation | varchar(500) | YES | | NULL | |
+-----------------------------+--------------------+------+-----+---------+----------------+
tb1stfct
+---------------------------+--------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------------+--------------------+------+-----+---------+----------------+
| FirstFctID | "int(10) unsigned" | NO | PRI | NULL | auto_increment |
| RepairID | varchar(45) | YES | | NULL | |
| FirstFctTestingErrorCode | varchar(500) | YES | MUL | NULL | |
| FirstFctActionTaken | varchar(500) | YES | | NULL | |
| FirstFctComponentLocation | varchar(500) | YES | | NULL | |
+---------------------------+--------------------+------+-----+---------+----------------+
tb2ndfct
+----------------------------+--------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------------+--------------------+------+-----+---------+----------------+
| SecondFctID | "int(10) unsigned" | NO | PRI | NULL | auto_increment |
| RepairID | varchar(45) | YES | | NULL | |
| SecondFctTestingErrorCode | varchar(500) | YES | MUL | NULL | |
| SecondFctActionTaken | varchar(500) | YES | | NULL | |
| SecondFctComponentLocation | varchar(500) | YES | | NULL | |
+----------------------------+--------------------+------+-----+---------+----------------+
tb3rdfct
+---------------------------+--------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------------+--------------------+------+-----+---------+----------------+
| ThirdFctID | "int(10) unsigned" | NO | PRI | NULL | auto_increment |
| RepairID | varchar(45) | YES | | NULL | |
| ThirdFctTestingErrorCode | varchar(500) | YES | MUL | NULL | |
| ThirdFctActionTaken | varchar(500) | YES | | NULL | |
| ThirdFctComponentLocation | varchar(500) | YES | | NULL | |
+---------------------------+--------------------+------+-----+---------+----------------+
This is my sample data inserted to the db
tbsrparts_new
+-----------+----------+-------+--------------+--------------+
| SRPartsID | RepairID | SRNo | DateReceived | ShipmentDate |
+-----------+----------+-------+--------------+--------------+
| 26050 | 26041 | test2 | 02,July,2015 | Pending |
+-----------+----------+-------+--------------+--------------+
tb1stdebug
+--------------+----------+----------------------------+-----------------------+-----------------------------+
| FirstDebugID | RepairID | FirstDebugTestingErrorCode | FirstDebugActionTaken | FirstDebugComponentLocation |
+--------------+----------+----------------------------+-----------------------+-----------------------------+
| 26048 | 26041 | T00111 | Touch Up | - |
| 26049 | 26041 | T02222 | Retest | - |
| 26053 | 26041 | T08888 | Touch Up | - |
+--------------+----------+----------------------------+-----------------------+-----------------------------+
tb2nddebug
+---------------+----------+-----------------------------+------------------------+------------------------------+
| SecondDebugID | RepairID | SecondDebugTestingErrorCode | SecondDebugActionTaken | SecondDebugComponentLocation |
+---------------+----------+-----------------------------+------------------------+------------------------------+
| 12 | 26041 | T03333 | Touch Up | - |
| 13 | 26041 | T04444 | Retest | - |
| 14 | 26041 | T08888 | Touch Up | - |
+---------------+----------+-----------------------------+------------------------+------------------------------+
tb3rddebug
+--------------+----------+----------------------------+-----------------------+-----------------------------+
| ThirdDebugID | RepairID | ThirdDebugTestingErrorCode | ThirdDebugActionTaken | ThirdDebugComponentLocation |
+--------------+----------+----------------------------+-----------------------+-----------------------------+
| 12 | 26041 | T03333 | Touch Up | - |
| 13 | 26041 | T04444 | Retest | - |
| 14 | 26041 | T08888 | Touch Up | - |
+--------------+----------+----------------------------+-----------------------+-----------------------------+
tb1stfct
+------------+----------+--------------------------+---------------------+---------------------------+
| FirstFctID | RepairID | FirstFctTestingErrorCode | FirstFctActionTaken | FirstFctComponentLocation |
+------------+----------+--------------------------+---------------------+---------------------------+
| 26052 | 26041 | T08888 | Retest | - |
+------------+----------+--------------------------+---------------------+---------------------------+
tb2ndfct
+-------------+----------+---------------------------+----------------------+----------------------------+
| SecondFctID | RepairID | SecondFctTestingErrorCode | SecondFctActionTaken | SecondFctComponentLocation |
+-------------+----------+---------------------------+----------------------+----------------------------+
| 9 | 26041 | T08888 | Touch Up | - |
| 10 | 26041 | T88889 | Retest | - |
+-------------+----------+---------------------------+----------------------+----------------------------+
tb3rdfct
+------------+----------+--------------------------+---------------------+---------------------------+
| ThirdFctID | RepairID | ThirdFctTestingErrorCode | ThirdFctActionTaken | ThirdFctComponentLocation |
+------------+----------+--------------------------+---------------------+---------------------------+
| 7 | 26041 | T08888 | Touch Up | - |
+------------+----------+--------------------------+---------------------+---------------------------+
The output on the view shows all the probability of the combination since some of the table has more than 1 records that referring to the same RepairID.Below are the output in image format. I cant paste the table because it reached the maximum characters.
This is the output i want
+-------+---------------+--------------+----------------------------+-----------------------+-----------------------------+-----------------------------+------------------------+------------------------------+----------------------------+-----------------------+-----------------------------+--------------------------+---------------------+---------------------------+---------------------------+----------------------+----------------------------+--------------------------+---------------------+---------------------------+
| SRNo | DateReceived | ShipmentDate | FirstDebugTestingErrorCode | FirstDebugActionTaken | FirstDebugComponentLocation | SecondDebugTestingErrorCode | SecondDebugActionTaken | SecondDebugComponentLocation | ThirdDebugTestingErrorCode | ThirdDebugActionTaken | ThirdDebugComponentLocation | FirstFctTestingErrorCode | FirstFctActionTaken | FirstFctComponentLocation | SecondFctTestingErrorCode | SecondFctActionTaken | SecondFctComponentLocation | ThirdFctTestingErrorCode | ThirdFctActionTaken | ThirdFctComponentLocation |
+-------+---------------+--------------+----------------------------+-----------------------+-----------------------------+-----------------------------+------------------------+------------------------------+----------------------------+-----------------------+-----------------------------+--------------------------+---------------------+---------------------------+---------------------------+----------------------+----------------------------+--------------------------+---------------------+---------------------------+
| test2 | 2, July, 2015 | Pending | T00111 | Touch Up | - | T03333 | Touch Up | - | T05555 | Touch Up | - | T08888 | Retest | - | T08888 | Touch Up | - | T08888 | Touch Up | - |
| test2 | 2, July, 2015 | Pending | T02222 | Retest | - | T04444 | Retest | - | T06666 | Retest | - | T08888 | Retest | - | T08889 | Retest | - | T08888 | Touch Up | - |
| test2 | 2, July, 2015 | Pending | T08888 | Touch Up | - | T08888 | Touch Up | - | T08888 | Touch Up | - | T08888 | Retest | - | T08889 | Retest | - | T08888 | Touch Up | - |
+-------+---------------+--------------+----------------------------+-----------------------+-----------------------------+-----------------------------+------------------------+------------------------------+----------------------------+-----------------------+-----------------------------+--------------------------+---------------------+---------------------------+---------------------------+----------------------+----------------------------+--------------------------+---------------------+---------------------------+
Hello i am trying to run this select statement using this query and it is taking over 2 hours to run. I have set up all the index's to be correct. But it still takes forever is there something i am missing or a more efficient way of joining tables together that will speed this query up?
I have indexes set up for all items being joined together and they are the same length and data type.
SELECT
p.sap_article_id,
p.numeric_line_code,
p.uag_linecode,
p.uag_partnum,
p.part_description,
p.jobber_price,
p.jobber_core,
p.discount1,
p.discount2,
p.uom,
p.product_category,
w.as400_warehouse,
w.atp_qty,
p.updated,
t.regular_discount
FROM part p
LEFT JOIN tabjbmaw t ON t.accountnum = '73050'
AND p.numeric_line_code = t.numeric_line_code
AND p.sub_code = t.sub_code
JOIN warehouse w ON w.sap_article_id = p.sap_article_id;
+----+-------------+-----------+------+--------------------------------------------------+-----------------------+---------+----------------------------------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+--------------------------------------------------+-----------------------+---------+----------------------------------+--------+-------------+
| 1 | SIMPLE | part | ALL | PRIMARY,sap_article,part_sap_article_id_fk | NULL | NULL | NULL | 389309 | |
| 1 | SIMPLE | warehouse | ref | article | article | 130 | inventory.part.sap_article_id | 5 | Using where |
| 1 | SIMPLE | tabjbmaw | ref | numeric_line_code_idx,subcode_idx,accountnum_idx | numeric_line_code_idx | 5 | inventory.part.numeric_line_code | 19 | |
+----+-------------+-----------+------+--------------------------------------------------+-----------------------+---------+----------------------------------+--------+-------------+
Thank you for your help
+-----------------------------+--------------+------+-----+---------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------------+--------------+------+-----+---------------------+-----------------------------+
| sap_article_id | varchar(24) | NO | PRI | | |
| sap_brand_id | varchar(20) | NO | | NULL | |
| uag_partnum | varchar(20) | NO | MUL | NULL | |
| uag_linecode | varchar(5) | NO | MUL | NULL | |
| cag_partnum | varchar(20) | NO | MUL | NULL | |
| cag_linecode | varchar(5) | NO | | NULL | |
| product_category_legacy | varchar(20) | NO | | NULL | |
| part_description | varchar(128) | NO | | NULL | |
| abc_indicator | varchar(8) | NO | | NULL | |
| pack_code | varchar(8) | NO | | NULL | |
| case_qty | int(11) | NO | | NULL | |
| per_car_qty | int(11) | NO | | NULL | |
| uom | varchar(6) | NO | | NULL | |
| upc_code | varchar(128) | NO | | NULL | |
| jobber_price | float(14,4) | YES | | NULL | |
| jobber_core | float(14,4) | YES | | NULL | |
| date_last_price_change | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| weight | float(14,4) | YES | | NULL | |
| weight_unit | varchar(6) | NO | | NULL | |
| dimension_type | varchar(6) | NO | | NULL | |
| length | float(14,4) | YES | | NULL | |
| width | float(14,4) | YES | | NULL | |
| height | float(14,4) | YES | | NULL | |
| updated | tinyint(1) | NO | | 0 | |
| superseded_sap_article_id | varchar(24) | YES | | NULL | |
| last_updated | timestamp | NO | | 0000-00-00 00:00:00 | |
| hour_updated | int(11) | YES | | NULL | |
| discount1 | float | YES | | NULL | |
| discount2 | float | YES | | NULL | |
| product_category | varchar(3) | YES | | NULL | |
| superseded_part_number | varchar(20) | YES | | NULL | |
| sub_code | varchar(3) | YES | MUL | NULL | |
| date_effective_price_change | date | YES | | NULL | |
| numeric_line_code | varchar(3) | YES | MUL | NULL | |
| list | float | YES | | NULL | |
+-----------------------------+--------------+------+-----+---------------------+-----------------------------+
I have indexes set up for all items being joined together
Yes, but I am guessing from the names of the indexes that each index only has one field.
Let's look at a few columns in the describe.
| table | possible_keys | key
+-----------+--------------------------------------------------+----------------
| part | PRIMARY,sap_article,part_sap_article_id_fk | NULL
| warehouse | article | article
| tabjbmaw | numeric_line_code_idx,subcode_idx,accountnum_idx | numeric_line_code_idx
It can use an index for numeric_line_code, subcode, and accountnum, but there are only three indexes each with one of the fields, and no index which has all the fields. You are making the optimizer choose one of the one field indexes, instead of providing one index it can use for all three fields.
Add an index on table tabjbmaw with the three fields numeric_line_code, subcode, and accountnum.
Extending #Sebas answer, you should select tabjbmaw first:
SELECT
p.sap_article_id,
p.numeric_line_code,
p.uag_linecode,
p.uag_partnum,
p.part_description,
p.jobber_price,
p.jobber_core,
p.discount1,
p.discount2,
p.uom,
p.product_category,
w.as400_warehouse,
w.atp_qty,
p.updated,
t.regular_discount
FROM tabjbmaw t
LEFT JOIN parts p ON p.numeric_line_code = t.numeric_line_code
AND p.sub_code = t.sub_code
JOIN warehouse w ON w.sap_article_id = p.sap_article_id
WHERE t.accountnum = '73050'
;
You could try to put your Left Join into the SELECT part as a Subselect. That 'may' speed things up a little.
Like this:
SELECT
p.sap_article_id,
p.numeric_line_code,
p.uag_linecode,
p.uag_partnum,
p.part_description,
p.jobber_price,
p.jobber_core,
p.discount1,
p.discount2,
p.uom,
p.product_category,
w.as400_warehouse,
w.atp_qty,
p.updated,
(SELECT t.regular_discount FROM tabjbmaw t WHERE t.accountnum = '73050' AND p.numeric_line_code = t.numeric_line_code AND p.sub_code = t.sub_code LIMIT 1)
FROM
part p
JOIN warehouse w ON w.sap_article_id = p.sap_article_id;