I have this query which gets me some data from a few tables. The query works perfectly, but it takes the server a long time to get the results.
$query_serial = "SELECT serienummers.serienummer, salesregels.aantalartikelen FROM serienummers
LEFT JOIN
(
SELECT aantalartikelen, artikelcode, salescode FROM salesregels
)salesregels ON salesregels.salescode = serienummers.salescode
LEFT JOIN
(
SELECT factuurnummer, code FROM sales
)sales ON sales.code = serienummers.salescode
WHERE serienummer = '$serialnumber' AND salesregels.artikelcode = serienummers.artikelcode AND sales.factuurnummer != '-1'
";
I've read a few thing online about indexing the tables, but im not quite sure how to apply this. Could anyone point me in the right direction, please?
My tables:
Serienummers:
+---------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| code | int(11) | NO | PRI | 0 | |
| serienummer | varchar(100) | NO | | | |
| artikelcode | int(11) | YES | | 0 | |
| datum | int(11) | NO | | 0 | |
| usercode | int(11) | NO | | 0 | |
| salescode | int(11) | YES | | 0 | |
| reparatiecode | int(11) | YES | | 0 | |
| actie | varchar(250) | NO | | | |
| rmacode | int(11) | NO | | 0 | |
+---------------+--------------+------+-----+---------+-------+
Salesregels
+---------------------------+-------------------------------------------------+-
-----+-----+-----------+-------+
| Field | Type |
Null | Key | Default | Extra |
+---------------------------+-------------------------------------------------+-
-----+-----+-----------+-------+
| code | int(11) |
NO | PRI | 0 | |
| salescode | int(11) |
NO | | 0 | |
| aantalartikelen | double |
NO | | 0 | |
| artikelcode | int(11) |
NO | | 0 | |
| prijsperstukinclbtw_old | varchar(20) |
NO | | 0 | |
| btwtypecode | int(11) |
NO | | 0 | |
| artikeltekst | varchar(250) |
NO | | | |
| procentbtw_old | varchar(10) |
NO | | 0,00 | |
| isprijsonderdrukt | int(11) |
NO | | 0 | |
| volgordenummer | int(11) |
NO | | 0 | |
| prijsperstukinclbtw | double |
NO | | 0 | |
| procentbtw | double |
NO | | 0 | |
| korting | double |
NO | | 0 | |
| kortingtype | enum('procenten','eurosinclbtw','eurosexclbtw') |
NO | | procenten | |
| inkoopprijsperstukexclbtw | double |
NO | | 0 | |
| inkoopprijsprocentbtw | double |
NO | | 0 | |
| inkoopprijsbtwtypecode | int(11) |
NO | | 0 | |
| dagengarantie | int(11) |
NO | | 0 | |
+---------------------------+-------------------------------------------------+-
Sales
+-------------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------------+--------------+------+-----+---------+-------+
| code | int(11) | NO | PRI | 0 | |
| salestypecode | int(11) | NO | | 0 | |
| websitesalesidcode | int(11) | NO | | 0 | |
| datum | int(11) | NO | | 0 | |
| datumbetaald | int(11) | NO | | 0 | |
| datumlaatstgewijzigd | int(11) | NO | | 0 | |
| usercodelaatstgewijzigd | int(11) | NO | | 0 | |
| adrescode | int(11) | NO | | 0 | |
| usercode | int(11) | NO | | 0 | |
| bedragvoldaan1_old | varchar(15) | NO | | 0 | |
| bedragvoldaan1 | double | NO | | 0 | |
| betalingswijzecode1 | int(11) | NO | | 0 | |
| bedragvoldaan2_old | varchar(15) | NO | | 0 | |
| bedragvoldaan2 | double | NO | | 0 | |
| betalingswijzecode2 | int(11) | NO | | 0 | |
| opmerkingen | longtext | NO | | NULL | |
| opmerkingenextern | longtext | YES | | NULL | |
| voorraadlokatiecode | int(11) | NO | | 0 | |
| computernaam | varchar(255) | YES | | NULL | |
| factuurnummer | bigint(20) | NO | | 0 | |
| betaald | int(11) | NO | | 1 | |
| status | varchar(255) | YES | | NULL | |
| dagenbetalingstermijn | int(11) | NO | | 8 | |
| externordernummer | varchar(255) | YES | | NULL | |
| externfactuurnummer | varchar(255) | YES | | NULL | |
| isdeleted | int(11) | NO | | 0 | |
+-------------------------+--------------+------+-----+---------+-------+
You do not need selecting everything using inner query before doing the join.
The joins could be done directly.
select
sr.serienummer,
sg.aantalartikelen
from serienummers sr
left join salesregels sg on sg.salescode = sr.salescode and sg.artikelcode = sr.artikelcode
left join sales s on s.code = sr.salescode
where
sr.serienummer = '$serialnumber'
and s.factuurnummer != '-1' ;
Now to speed up you need indexes on the joining keys and keys used in where clause.
alter table serienummers add index sales_kel_code(salescode,artikelcode);
alter table serienummers add index serienummer_idx(serienummer);
alter table salesregels add index salescode_idx(salescode);
alter table sales add index factuurnummer_idx(factuurnummer);
Related
I'm a pretty green novice with SQL still and have been tasked with something that looks incredibly tedious to accomplish. I need to move 58 of 61 columns from tableA to tableB. tableA has 61 columns and only 15 rows, tableB has 58 columns and zero rows right now. I've read multiple sources on how to achieve this (moving data from one table to a related one), but none specifically talk about this particular case I'm dealing with.
To further complicate things, the second table does not have the columns in the same order as the first, so this poses a problem as well. This SO post seemed promising, but it is working with a substantially smaller dataset: SQL: Move column data to other table in same relation . This one is also similar MYSQL - Move data from one table to a related one? . The problem is they both are moving to a larger table from a smaller table, not vis a vis.
Here's my initial query for attempting to make this transition happen (and was unsuccessful as the included error indicates):
INSERT INTO tableB SELECT * FROM tableA;
ERROR 1136 (21S01): Column count doesn't match value count at row 1
tableA
+-------------------------------+------------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------------------+------------------+------+-----+-------------------+-----------------------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| staff_fk | int(11) unsigned | NO | | NULL | |
| hire_date | date | NO | | NULL | |
| position | varchar(75) | NO | | NULL | |
| manager | varchar(75) | NO | | NULL | |
| employment_application | char(1) | NO | | i | |
| resume | char(1) | NO | | i | |
| references_checked | char(1) | NO | | i | |
| new_hire_letter | char(1) | NO | | i | |
| clinical_training_initiated | char(1) | NO | | i | |
| name_badge_ordered | char(1) | NO | | i | |
| keycode_access | char(1) | NO | | i | |
| tf_up_setup | char(1) | NO | | i | |
| un_pw_setup | char(1) | NO | | i | |
| drug_screen | char(1) | NO | | i | |
| background_investigation | char(1) | NO | | i | |
| licenses_education_verified | char(1) | NO | | i | |
| cpr | char(1) | NO | | i | |
| clinic_tour_introductions | char(1) | NO | | i | |
| sick_tardy_phone_notification | char(1) | NO | | i | |
| work_sched_start_time | char(1) | NO | | i | |
| paydays | char(1) | NO | | i | |
| ds_pto | char(1) | NO | | i | |
| attendance_tardiness | char(1) | NO | | i | |
| evacuation_plan | char(1) | NO | | i | |
| osha | char(1) | NO | | i | |
| emp_handbook_receipt | char(1) | NO | | i | |
| internet_email_phone_usage | char(1) | NO | | i | |
| parking_building_access | char(1) | NO | | i | |
| dress_grooming | char(1) | NO | | i | |
| inclement_weather | char(1) | NO | | i | |
| mileage | char(1) | NO | | i | |
| hipaa_training | char(1) | NO | | i | |
| direct_deposit_auth | char(1) | NO | | i | |
| i9 | char(1) | NO | | i | |
| w4 | char(1) | NO | | i | |
| valid_id | char(1) | NO | | i | |
| update_ext_list | char(1) | NO | | i | |
| emerg_contact_form | char(1) | NO | | i | |
| med_dent_life_vision | char(1) | NO | | i | |
| 401k_pen_sh | char(1) | NO | | i | |
| centricity_access | char(1) | NO | | i | |
| centricity_training | char(1) | NO | | i | |
| phone_training | char(1) | NO | | i | |
| create_active_dir_account | char(1) | NO | | i | |
| email_access | char(1) | NO | | i | |
| drive_access | char(1) | NO | | i | |
| mcafee | char(1) | NO | | i | |
| hr_enrollment_forms | char(1) | NO | | i | |
| ds_signoff | char(1) | NO | | i | |
| ds_clincial_signoff | char(1) | NO | | i | |
| completed | datetime | YES | | NULL | |
| created | datetime | NO | | NULL | |
| last_update | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| portal_access | char(1) | NO | | i | |
| harvest | char(1) | NO | | i | |
| imms_link | char(1) | NO | | i | |
| eprescribe | char(1) | NO | | i | |
| state_alert_access | char(1) | NO | | i | |
| phone_agent | char(1) | NO | | i | |
| pc_tablet_setup | char(1) | NO | | i | |
+-------------------------------+------------------+------+-----+-------------------+-----------------------------+
61 rows in set (0.00 sec)
tableB:
+-------------------------------+------------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------------------+------------------+------+-----+-------------------+-----------------------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| staff_fk | int(11) unsigned | NO | | NULL | |
| employment_application | char(1) | NO | | i | |
| resume | char(1) | NO | | i | |
| references_checked | char(1) | NO | | i | |
| new_hire_letter | char(1) | NO | | i | |
| clinical_training_initiated | char(1) | NO | | i | |
| name_badge_ordered | char(1) | NO | | i | |
| keycode_access | char(1) | NO | | i | |
| tf_up_setup | char(1) | NO | | i | |
| un_pw_setup | char(1) | NO | | i | |
| drug_screen | char(1) | NO | | i | |
| background_investigation | char(1) | NO | | i | |
| licenses_education_verified | char(1) | NO | | i | |
| cpr | char(1) | NO | | i | |
| clinic_tour_introductions | char(1) | NO | | i | |
| sick_tardy_phone_notification | char(1) | NO | | i | |
| work_sched_start_time | char(1) | NO | | i | |
| paydays | char(1) | NO | | i | |
| ds_pto | char(1) | NO | | i | |
| attendance_tardiness | char(1) | NO | | i | |
| evacuation_plan | char(1) | NO | | i | |
| osha | char(1) | NO | | i | |
| emp_handbook_receipt | char(1) | NO | | i | |
| internet_email_phone_usage | char(1) | NO | | i | |
| parking_building_access | char(1) | NO | | i | |
| dress_grooming | char(1) | NO | | i | |
| inclement_weather | char(1) | NO | | i | |
| mileage | char(1) | NO | | i | |
| hipaa_training | char(1) | NO | | i | |
| direct_deposit_auth | char(1) | NO | | i | |
| i9 | char(1) | NO | | i | |
| w4 | char(1) | NO | | i | |
| valid_id | char(1) | NO | | i | |
| update_ext_list | char(1) | NO | | i | |
| emerg_contact_form | char(1) | NO | | i | |
| med_dent_life_vision | char(1) | NO | | i | |
| 401k_pen_sh | char(1) | NO | | i | |
| centricity_access | char(1) | NO | | i | |
| centricity_training | char(1) | NO | | i | |
| phone_training | char(1) | NO | | i | |
| create_active_dir_account | char(1) | NO | | i | |
| email_access | char(1) | NO | | i | |
| drive_access | char(1) | NO | | i | |
| mcafee | char(1) | NO | | i | |
| portal_access | char(1) | NO | | i | |
| harvest | char(1) | NO | | i | |
| imms_link | char(1) | NO | | i | |
| eprescribe | char(1) | NO | | i | |
| state_alert_access | char(1) | NO | | i | |
| phone_agent | char(1) | NO | | i | |
| pc_tablet_setup | char(1) | NO | | i | |
| hr_enrollment_forms | char(1) | NO | | i | |
| ds_signoff | char(1) | NO | | i | |
| ds_clincial_signoff | char(1) | NO | | i | |
| completed | datetime | YES | | NULL | |
| created | timestamp | NO | | CURRENT_TIMESTAMP | |
| last_update | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------------------------+------------------+------+-----+-------------------+-----------------------------+
58 rows in set (0.01 sec)
I'll include the schemas if anyone would like to see them.
I'm wondering if there's a way to do this without explicitly labeling each column in the INSERT INTO statement. Any help will be greatly appreciated!
No, there is no way to do this without labeling each column. There is no syntax in SQL for SELECT *_except_for_a_few_columns
You can generate the list of columns out of the INFORMATION_SCHEMA so you can cut down on tedious typing:
SELECT GROUP_CONCAT(column_name ORDER BY ordinal_position) AS _cols
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'mydatabase' AND table_name = 'tableA'
AND column_name NOT IN ('do', 'not', 'want')
To further complicate things, the second table does not have the columns in the same order as the first...
If the columns at least have the same names in both tables, you can use a column list in your INSERT statement regardless of the natural ordinal position of those columns defined in the table. In other words, the following works:
INSERT INTO tableB (col1, col4, col2, col6, col9)
SELECT col1, col4, col2, col6, col9 FROM tableA;
If the columns differ in number, order, and name, I'd recommend that you double-check that you're inserting into the correct table! :-)
Yes and that error is pretty straight forward and is saying that you can't use * and need to specify the column names explicitly like
INSERT INTO tableB
SELECT col1, col2, col3, ..., col58 FROM tableA;
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 | - |
+-------+---------------+--------------+----------------------------+-----------------------+-----------------------------+-----------------------------+------------------------+------------------------------+----------------------------+-----------------------+-----------------------------+--------------------------+---------------------+---------------------------+---------------------------+----------------------+----------------------------+--------------------------+---------------------+---------------------------+
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.
I have a members_info table that has many members in it, but what I need to do is update member B's info with member A's info.
Here are the members user id's:
Member A = 98089 (member_info_id)
Member B = 236505 (member_info_id)
The two members have nothing in common, so I can't do join. What can I do to update Member B's info?
Here is the table's structure, but I doubt it is useful
mysql> describe member_info;
+-------------------------------+--------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------------------+--------------+------+-----+-------------------+----------------+
| member_info_id | int(11) | NO | PRI | NULL | auto_increment |
| member_id | int(11) | NO | UNI | 0 | |
| street | varchar(100) | NO | | NULL | |
| street2 | varchar(50) | NO | | | |
| city | varchar(50) | NO | | | |
| state | varchar(50) | NO | | | |
| country_id | int(11) | NO | | 0 | |
| postal | varchar(20) | NO | | | |
| phone | varchar(20) | NO | | | |
| street_ntv | varchar(100) | NO | | | |
| street2_ntv | varchar(50) | NO | | | |
| city_ntv | varchar(50) | NO | | | |
| state_ntv | varchar(50) | NO | | | |
| card_ship_addr | varchar(250) | NO | | NULL | |
| tax_id | varchar(20) | NO | | | |
| c_type | varchar(20) | NO | | | |
| c_number | varchar(20) | NO | | | |
| c_code | varchar(5) | NO | | | |
| c_date | varchar(10) | NO | | | |
| c_name | varchar(50) | NO | | | |
| street_b | varchar(100) | NO | | NULL | |
| street_b2 | varchar(50) | NO | | | |
| city_b | varchar(50) | NO | | | |
| state_b | varchar(50) | NO | | | |
| country_b_id | int(11) | NO | | 0 | |
| postal_b | varchar(20) | NO | | | |
| new_data | text | NO | | NULL | |
| new_data_code | varchar(10) | NO | | | |
| new_data_date | int(11) | NO | | 0 | |
| picture_id | int(1) | NO | | 0 | |
| gender | tinyint(4) | NO | | 0 | |
| date_birth | date | YES | | NULL | |
| education | tinyint(4) | NO | | 0 | |
| income_range | tinyint(4) | NO | | 0 | |
| li_first_name | varchar(200) | NO | | | |
| li_last_name | varchar(200) | NO | | | |
| li_street | varchar(200) | NO | | | |
| li_street2 | varchar(200) | NO | | | |
| li_city | varchar(200) | NO | | | |
| li_state | varchar(200) | NO | | | |
| li_country_id | varchar(200) | NO | | | |
| li_postal | varchar(200) | NO | | | |
| li_phone | varchar(200) | NO | | | |
| li_governmentid | varchar(200) | NO | | | |
| li_gov_filename | varchar(50) | NO | | | |
| li_gov_id_number | varchar(30) | NO | | | |
| li_gov_driver_state | varchar(10) | NO | | | |
| li_gov_country | varchar(50) | NO | | | |
| li_gov_expdate | varchar(50) | NO | | | |
| li_governmentdate | int(11) | NO | | 0 | |
| li_staff_id | int(11) | NO | | 0 | |
| li_verify_date | int(11) | NO | | 0 | |
| private_key | varchar(100) | NO | | | |
| private_key_enable | int(11) | NO | | 0 | |
| show_displayname | tinyint(1) | YES | | 0 | |
| legal_status | int(11) | NO | MUL | NULL | |
| legal_change_reason | text | NO | | NULL | |
| legal_update_time | timestamp | NO | MUL | CURRENT_TIMESTAMP | |
| legal_expire_date | date | NO | | 0000-00-00 | |
| legal_wf_update_id | int(11) | YES | MUL | NULL | |
| legal_gov_update_id | int(11) | YES | MUL | NULL | |
| legal_info_update_id | int(11) | YES | MUL | NULL | |
| legal_verifications_update_id | int(11) | YES | MUL | NULL | |
| ewallet_log_update_id | int(11) | YES | MUL | NULL | |
| legal_lang | tinyint(4) | YES | MUL | NULL | |
+-------------------------------+--------------+------+-----+-------------------+----------------+
65 rows in set (0.00 sec)
Even though they aren't directly related, you can still perform a JOIN:
UPDATE
members_info mb JOIN
members_info ma ON ma.member_info_id = 98089
SET
mb.Name = ma.Name,
mb.Info = ma.Info
-- Etc...
WHERE
mb.member_info_id = 236505
If you are only looking to update a single member (B) with A's information, then...
UPDATE members_info member_B, (SELECT DISTINCT ID, Column_1, Column_2, Column_X
FROM members_info
WHERE id = '98089') member_A
SET member_B.Column_1 = member_A.Column_1, member_B.Column_2 = member_A.Column_2
WHERE member_B.id = '236505'
**Note: Untested.