SQL Grouping by those that has results but keeping those without - mysql

I have a big SQL query that selects totals by month and groups by merchants. It works well and displays everything I need. The problem is I also need the merchant with no transaction to show in the list. So fare, the merchant gets a total only if he did at least one transaction. We need to calculate the monthly fees even if a merchant made 0 transaction.
My current SQL looks like this.
SELECT
t.id,
m.name AS merchant_name,
DATE_FORMAT(t.transaction_date, '%Y-%m') AS DATE,
SUM(t.amount) AS total_sales,
COUNT(t.id) AS total_transactions
FROM
merchants m
LEFT JOIN transactions t ON
t.merchant_id = m.id
WHERE
t.transaction_date LIKE '2020-08%'
GROUP BY
DATE_FORMAT(t.transaction_date, '%Y-%m'),
t.merchant_id
ORDER BY
t.transaction_date
DESC
So I was thinking about this ... and thought maybe I could do a second query for merchants with out transactions and merge both results together... but so fare I got no luck at all. My query returns no error but says no results. If there's a way of doing this in only one query it would be a lot better thought. Here's my second query in case.
SELECT
m.id,
m.name AS merchant_name,
COUNT(t.id) as transaction_count,
SUM(0) AS total_sales,
COUNT(t.id) AS total_transactions,
FROM
merchants m
LEFT JOIN transactions t ON
t.merchant_id = m.id
WHERE
t.transaction_date like '2020-08%'
HAVING transaction_count = 0
My transaction table looks like this
CREATE TABLE `transactions` (
`id` int(10) UNSIGNED NOT NULL,
`transaction_type` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`account_number` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`routing_number` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`fund_amt` decimal(15,2) DEFAULT NULL,
`batch_reference` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`batch_type` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`customer_batch_reference` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`customer_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`merchant_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`external_mid` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`store_number` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`chain` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`batch_amt` decimal(15,2) DEFAULT NULL,
`amount` decimal(15,2) DEFAULT NULL,
`surchg_amount` decimal(15,2) DEFAULT NULL,
`convnce_amt` decimal(15,2) DEFAULT NULL,
`card_type` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`charge_type` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`card_plan` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`card_no` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`chk_num` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`transaction_date` datetime DEFAULT NULL,
`settlement_date` datetime DEFAULT NULL,
`authorization_code` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`chargback_control_no` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`roc_text` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`trn_aci` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`card_scheme_ref` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`trn_ref_num` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`settlement_method` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`currency_code` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`cb_acq_ref` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`chgbk_rsn_code` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`chgbk_rsn_desc` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`mer_ref` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`purch` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`cust_cod` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`trn_arn` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`term` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`ent_num` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`charge_type_description` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`payment_reference` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`payment_date` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`i_plus_visa` decimal(9,2) DEFAULT 0.00,
`pi_visa` decimal(9,2) DEFAULT 0.00,
`i_plus_Vdebit` decimal(9,2) DEFAULT 0.00,
`pi_Vdebit` decimal(9,2) DEFAULT 0.00,
`i_plus_mc` decimal(9,2) DEFAULT 0.00,
`pi_mc` decimal(9,2) DEFAULT 0.00,
`i_plus_amex` decimal(9,2) DEFAULT 0.00,
`pi_amex` decimal(9,2) DEFAULT 0.00,
`i_plus_discover` decimal(9,2) DEFAULT 0.00,
`pi_discover` decimal(9,2) DEFAULT 0.00,
`i_plus_union` decimal(9,2) DEFAULT 0.00,
`pi_union` decimal(9,2) DEFAULT 0.00,
`i_plus_MCdebit` decimal(9,2) DEFAULT 0.00,
`pi_MCdebit` decimal(9,2) DEFAULT 0.00,
`pi_debit` decimal(9,2) DEFAULT 0.00,
`pi_flash` decimal(9,2) DEFAULT 0.00,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`customer_id` int(10) UNSIGNED DEFAULT NULL,
`merchant_id` int(10) UNSIGNED DEFAULT NULL,
`batch_id` int(10) UNSIGNED DEFAULT NULL,
`converge_res` text COLLATE utf8mb4_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
// 5 Transactions
INSERT INTO `transactions` (`id`, `transaction_type`, `account_number`, `routing_number`, `fund_amt`, `batch_reference`, `batch_type`, `customer_batch_reference`, `customer_name`, `merchant_name`, `external_mid`, `store_number`, `chain`, `batch_amt`, `amount`, `surchg_amount`, `convnce_amt`, `card_type`, `charge_type`, `card_plan`, `card_no`, `chk_num`, `transaction_date`, `settlement_date`, `authorization_code`, `chargback_control_no`, `roc_text`, `trn_aci`, `card_scheme_ref`, `trn_ref_num`, `settlement_method`, `currency_code`, `cb_acq_ref`, `chgbk_rsn_code`, `chgbk_rsn_desc`, `mer_ref`, `purch`, `cust_cod`, `trn_arn`, `term`, `ent_num`, `charge_type_description`, `payment_reference`, `payment_date`, `i_plus_visa`, `pi_visa`, `i_plus_Vdebit`, `pi_Vdebit`, `i_plus_mc`, `pi_mc`, `i_plus_amex`, `pi_amex`, `i_plus_discover`, `pi_discover`, `i_plus_union`, `pi_union`, `i_plus_MCdebit`, `pi_MCdebit`, `pi_debit`, `pi_flash`, `created_at`, `updated_at`, `customer_id`, `merchant_id`, `batch_id`, `converge_res`) VALUES
(1, '1', '***123', '00000124162', '370.90', '17290458149', 'D', '00000000043', 'name 1', NULL, '8033407712', '', '00000', '370.90', '65.00', '0.00', '0.00', '032', '02465', 'M/C', '556923******2454', '', '2019-10-16 00:00:00', '2019-10-16 00:00:00', '613342', NULL, '', '', 'MCFPHH5FS', '17290458152', 'ACH', 'CAD', NULL, '', '', '00065069751', NULL, '', '55259569290172904581529', NULL, '48627', 'MC CHIP DOM COMMERCIAL', '929000000127220', '2019-08-17 00:00:00', '0.03', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '2020-09-21 01:58:00', '2020-09-23 00:36:54', NULL, 1, 1, NULL),
(2, '1', '***123', '00000124162', '370.90', '17290458149', 'D', '00000000043', 'name 1', NULL, '8033407712', '', '00000', '370.90', '114.00', '0.00', '0.00', '049', '01810', 'INTC', '472409******9229', '', '2019-10-16 00:00:00', '2019-10-16 00:00:00', '224694', NULL, '', '', '', '17290458155', 'ACH', 'CAD', NULL, '', '', '00065069754', NULL, '', '0000000000017290458155X', NULL, '48627', 'PIN DEBIT RETAIL BASE', '929000000127220', '2019-10-17 00:00:00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.09', '0.00', '2020-09-21 01:58:00', '2020-09-23 00:31:58', NULL, 1, 1, NULL),
(3, '1', '***124', '00000124162', '323.35', '73290783323', 'D', '00000000033', 'name 2', NULL, '8033407712', '', '00000', '323.35', '103.50', '0.00', '0.00', '032', '02465', 'M/C', '556960******7782', '', '2019-10-16 00:00:00', '2019-10-16 00:00:00', '066015', NULL, '', '', 'MCFPFPTVH', '73290783327', 'ACH', 'CAD', NULL, '', '', '00064918465', NULL, '', '55259569290732907833274', NULL, '48627', 'MC CHIP DOM COMMERCIAL', '929000000127221', '2019-08-17 00:00:00', '0.05', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '2020-09-21 01:58:00', '2020-09-23 00:36:54', NULL, 2, 2, NULL),
(4, '1', '***124', '00000124162', '323.35', '73290783323', 'D', '00000000033', 'name 2', NULL, '8033407712', '', '00000', '323.35', '88.55', '0.00', '0.00', '026', '02465', 'M/C', '558700******1934', '', '2019-10-16 00:00:00', '2019-10-16 00:00:00', '034367', NULL, '', '', 'MCBQL61VH', '73290783328', 'ACH', 'CAD', NULL, '', '', '00064918466', NULL, '', '55259569290732907833282', NULL, '48627', 'MC CHIP DOM COMMERCIAL', '929000000127221', '2019-08-17 00:00:00', '0.04', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '2020-09-21 01:58:00', '2020-09-23 00:36:54', NULL, 2, 2, NULL),
(5, '1', '***125', '00000124162', '323.35', '73290783323', 'D', '00000000033', 'name 3', NULL, '8033407712', '', '00000', '323.35', '71.30', '0.00', '0.00', '026', '02465', 'M/C', '552822******0618', '', '2019-10-16 00:00:00', '2019-10-16 00:00:00', '006983', NULL, '', '', 'MCO00Q4RH', '73290783324', 'ACH', 'CAD', NULL, '', '', '00064918462', NULL, '', '55259569290732907833241', NULL, '48627', 'MC CHIP DOM COMMERCIAL', '929000000127221', '2019-08-17 00:00:00', '0.04', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '2020-09-21 01:58:00', '2020-09-23 00:36:54', NULL, 3, 2, NULL);
In the above we have 5 transaction owned by 3 merchants. Let say I have 5 merchants. How can I end up with 5 results where 2 have empty totals

Move the condition from the WHERE-clause into the LEFT JOIN so that the condition will not exclude the merchants with no transactions. The t.id in SELECT in your first query does not really make sense, maybe you meant m.id?
SELECT
m.id,
m.name AS merchant_name,
DATE_FORMAT(t.transaction_date, '%Y-%m') AS DATE,
ifnull(SUM(t.amount),0) AS total_sales,
COUNT(t.id) AS total_transactions
FROM
merchants m
LEFT JOIN transactions t ON
t.merchant_id = m.id and t.transaction_date LIKE '2020-08%'
GROUP BY
m.id,
m.name,
DATE_FORMAT(t.transaction_date, '%Y-%m')

Related

How to make slow MySQL query run faster

I have this query that often shows me "error code 2013. lost connection to mysql query" whenever I run it:
DROP TABLE IF EXISTS elogbook_get_boardid;
CREATE TABLE elogbook_get_boardid AS
(SELECT DISTINCT `LOTID`,
`Board_ID`,
`Serial_Number`,
coalesce(CASE
WHEN A.`Serial_Number` = B.`board_sn` THEN 'In Use'
ELSE A.`status`
END, '') AS `Status`,
coalesce(B.`LOT_LOCATION`, '') AS `chamber`,
coalesce(B.`created_date`, '') AS `Start Date`,
coalesce(B.`BINOUT_DUE_DATE`, '') AS `Est End`
FROM hardware_tracking_msa.HAST_Detail A
LEFT JOIN
(SELECT X.*,
Y.`BINOUT_DUE_DATE`,
Y.`LOT_LOCATION`
FROM skynet_msa.lots_to_hast_boards X
LEFT JOIN skynet_msa.labs_inventory Y ON X.`lotid` = Y.`LOTID`) B ON A.`Serial_Number` = B.`board_sn`
WHERE `LOTID` IS NOT NULL);
I would like to know what makes it take longer than 30 seconds to run and how to improve the query. Any help is appreciated thanks!
Output for hardware_tracking_msa.HAST_Detail:
Index, Board_Number, SIG_Number, Board_ID, Serial_Number, Design_ID, Package, Sockets, Socket_Number, Status, Notes, Deleted_By, Inserted_Date, Inserted_By, Updated_Date, Updated_By, Deleted_Date
'1', '2759', '594-11269', '2759-001', '605637/001', 'J4Q2', 'WFBGA, PIN COUNT: 168/529,PACKAGE SIZE: 12x12', '40', '530-10670 SINGLE BIAS (ESPEC)CANNOT USE FOR J80C/J4Q2', '', '', '', '2022-05-17 14:00:00', 'NWANGA', '2022-05-18 10:15:41', 'tmingyao', '2022-05-17 14:57:33'
'2', '2759', '594-11269', '2759-002', '605637/008', 'J4Q2', 'WFBGA, PIN COUNT: 168/529,PACKAGE SIZE: 12x12', '40', '530-10670 SINGLE BIAS (ESPEC)CANNOT USE FOR J80C/J4Q2', NULL, '', '', '2022-05-17 14:00:00', 'NWANGA', '2022-05-17 14:57:33', '', '2022-05-17 14:57:33'
'3', '2759', '594-11269', '2759-003', '608061/001', 'J4Q2', 'WFBGA, PIN COUNT: 168/529,PACKAGE SIZE: 12x12', '40', '530-10670 SINGLE BIAS (ESPEC)CANNOT USE FOR J80C/J4Q2', NULL, '', '', '2022-05-17 14:00:00', 'NWANGA', '2022-05-17 14:57:33', '', '2022-05-17 14:57:33'
Output for skynet_msa.lots_to_hast_boards:
\begin{table}[]
\begin{tabular}{lll}
lotid, & board\_sn, & created\_date \\
'CVZ2JL2.11', & '1790247/003', & '2022-07-20 '14:26:04 \\
'CV4YJL2.11', & '1317876/002', & '2022-07-20 14:26:04 \\
'CVRMHL2.11', & '1790241/014', & '2022-07-20 14:26:04
\end{tabular}
\end{table}
Output for skynet_msa.labs_inventory:
LOTID, LOCATION, ENV_TEST_INTERVAL, EST_DURATION_TIME, ENV_STRESS_VOLTAGE, ENV_STRESS_VOLT_2, ENV_STRESS_VOLT_3, PRODUCT_FAMILY, PRODUCT_TECHNOLOGY, DESIGN_ID, QA_WORK_REQUEST_NO, QA_PROCESS_TYPE, QA_PROCESS_NAME, QA_BURN_EXPERIMENT, QA_CONTACT, QA_PROCESS_LOT_NO, FABRICATION_FACILITY, ASSEMBLY_FACILITY, ELEC_TEST_FLOW, CONFIGURATION_WIDTH, NUMBER_OF_DIE_IN_PKG, CURRENT_QTY, LOT_LOCATION, LEAD_COUNT, PACKAGE_TYPE, PACKAGE_LENGTH, PACKAGE_WIDTH, PACKAGE_HEIGHT, SOAK_LEVEL, BAKE_TEMPERATURE, DRB_TEMPERATURE, ACTUAL_CURE_TIME, REFLOW_PROFILE, PINOUT_VERSION, DISPATCH_DUE_DATE, BINOUT_DUE_DATE, ROW_CREATED, ROW_MODIFIED, LOCATION_DATE, LOCATION_WW, MODULE_LOT, BURN_LOT, MONITOR_IGNORE, TICKER, PRIORITY, ASM_LOT_NUMBER, MARK_FORMAT, LOCATION_TAT, RPM_WW, QA_EVENT_ID, TC_WEIGHT, AUTOMOTIVE_LOT, CUSTOMER_OPTION, PKG_RECEIVE_DATE, CUSTOMER_GROUP, SAMPLE_PULLED_DATE, QA_SPECIAL_FLOW, QA_BLOCKS, PROBE_CUSTOM_TESTED, QA_PROGRAM_REV, NAND_FLOW_TYPE, NUM_FLASH_CE_PINS, RETICLE_WAVE_ID, MAJOR_PROBE_PROG_REV, MAJOR_TEST_PROG_REV, CYCLING_TYPE, QA_TARGET_CYCLE, LAST_TEST_INTERVAL, CYCLING_TEMPERATURE, ENV_STRESS_DURATION, FIRST_TEST_INTERVAL, DRB_TARGET_INTERVAL, LTDR_TEMPERATURE, RD_STRESS_TYPE
'1623941.001', 'TEMP CYCLE K INV', '25', '12.50', '0.000', '0.000', '0.000', 'MCP', 'ALL IN ONE MCP', 'UM181', '', 'PRODUCTION SCREEN', 'TEMP CYCLE K', '', '', '', 'MIXED', 'ASSEMBLY-MSA', '', '', '8', '2439', 'S01-AR-ASRSIN', '254/432', 'TFBGA', '13.000', '11.500', '1.100', '', '0', '0', '0', '', 'JEDEC', '1970-01-01 00:00:00', NULL, '2022-06-05 18:35:12', '2022-06-06 00:00:16', '2022-06-05 18:35:00', '202223', '0', '0', '0', '0', '4', '1623941.001', '', '1.57', '', 'QA 13', '0', '', '', '1970-01-01 00:00:00', '', '1970-01-01 00:00:00', '', '', 'MOBILE C', '', '', '4', '', '', '', '', '', '', '', '25', '', '', '', ''
'BC4WSXZ.31', 'THERMAL WARPAGE', '0', '0.00', '0.000', '0.000', '0.000', 'MCP', 'MASSFLASH/LPDDR4', 'J86L', '', 'PRODUCTION SCREEN', 'THERMAL WARPAGE', '', '', '', 'MIXED', 'PTI P3', '', '', '2', '26', '', '194/1026', 'UFBGA', '9.000', '12.500', '0.545', '', '0', '0', '0', '', 'AVALON', '1970-01-01 00:00:00', NULL, '2022-07-19 11:00:17', '2022-07-20 13:30:15', '2022-07-19 11:26:45', '202229', '0', '0', '0', '0', '4', 'PT22900.25', 'AVALON', '0.28', '', 'QA 32', '0', '', '', '1970-01-01 00:00:00', '', '2022-07-19 10:58:00', '', '', '', '', '', '1', '', '', '29', '', '', '', '', '', '', '', '', ''
'BC6VVLZ.31', 'TEMP CYCLE K INV', '25', '12.50', '0.000', '0.000', '0.000', 'MCP', 'MASSFLASH/CONTROLLER', 'J39E', '', 'PRODUCTION SCREEN', 'TEMP CYCLE K', '', '', '', 'FAB 10', 'ASSEMBLY-MSA', '', 'X4-X8', '4', '320', 'S01-REL-LAB-IN', '153/196', 'VFBGA', '13.000', '11.500', '1.000', '', '0', '0', '0', '', 'JEDEC', '1970-01-01 00:00:00', NULL, '2022-07-10 14:35:16', '2022-07-11 07:15:19', '2022-07-10 14:31:49', '202228', '0', '0', '0', '0', '4', 'BF3HFCQ.5X', '', '419.25', '', 'QA 45', '0', 'YES', 'AUTOMOTIVE', '1970-01-01 00:00:00', '', '1970-01-01 00:00:00', '', '', 'NAND AUTO', '', '', '4', 'WAVE007', '22', '', '', '', '', '', '25', '', '', '', ''
show create table hardware_tracking_msa.HAST_Detail:
CREATE TABLE `HAST_Detail` (
`Index` int NOT NULL AUTO_INCREMENT,
`Board_Number` varchar(250) DEFAULT \'\',
`SIG_Number` varchar(250) DEFAULT \'\',
`Board_ID` varchar(250) DEFAULT \'\',
`Serial_Number` varchar(250) DEFAULT \'\',
`Design_ID` varchar(150) DEFAULT \'\',
`Package` varchar(250) DEFAULT \'\',
`Sockets` int DEFAULT \'0\',
`Socket_Number` varchar(250) DEFAULT \'\',
`Status` varchar(45) DEFAULT NULL,
`Notes` varchar(1000) DEFAULT \'\',
`Deleted_By` varchar(20) DEFAULT \'\',
`Inserted_Date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`Inserted_By` varchar(20) NOT NULL DEFAULT \'\',
`Updated_Date` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`Updated_By` varchar(20) DEFAULT \'\',
`Deleted_Date` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`Index`)
) ENGINE=InnoDB AUTO_INCREMENT=1459 DEFAULT CHARSET=utf8'
show create table skynet_msa.lots_to_hast_boards:
CREATE TABLE `lots_to_hast_boards` (
`lotid` varchar(45) NOT NULL DEFAULT \'\',
`board_sn` varchar(45) NOT NULL DEFAULT \'\',
`created_date` datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`lotid`,`board_sn`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1'
show create table skynet_msa.labs_inventory:
CREATE TABLE `labs_inventory` (
`LOTID` varchar(12) NOT NULL,
`LOCATION` varchar(48) NOT NULL,
`ENV_TEST_INTERVAL` int DEFAULT \'0\',
`EST_DURATION_TIME` decimal(8,2) DEFAULT \'0.00\',
`ENV_STRESS_VOLTAGE` decimal(6,3) DEFAULT NULL,
`ENV_STRESS_VOLT_2` decimal(6,3) DEFAULT NULL,
`ENV_STRESS_VOLT_3` decimal(6,3) DEFAULT NULL,
`PRODUCT_FAMILY` varchar(45) DEFAULT NULL,
`PRODUCT_TECHNOLOGY` varchar(45) DEFAULT NULL,
`DESIGN_ID` varchar(6) DEFAULT NULL,
`QA_WORK_REQUEST_NO` varchar(100) DEFAULT NULL,
`QA_PROCESS_TYPE` varchar(45) DEFAULT NULL,
`QA_PROCESS_NAME` varchar(64) DEFAULT NULL,
`QA_BURN_EXPERIMENT` varchar(45) DEFAULT NULL,
`QA_CONTACT` varchar(45) DEFAULT NULL,
`QA_PROCESS_LOT_NO` varchar(12) DEFAULT NULL,
`FABRICATION_FACILITY` varchar(45) DEFAULT NULL,
`ASSEMBLY_FACILITY` varchar(45) DEFAULT NULL,
`ELEC_TEST_FLOW` varchar(45) DEFAULT NULL,
`CONFIGURATION_WIDTH` varchar(8) DEFAULT NULL,
`NUMBER_OF_DIE_IN_PKG` int DEFAULT NULL,
`CURRENT_QTY` int DEFAULT NULL,
`LOT_LOCATION` varchar(45) DEFAULT NULL,
`LEAD_COUNT` varchar(45) DEFAULT \'\',
`PACKAGE_TYPE` varchar(45) DEFAULT \'\',
`PACKAGE_LENGTH` decimal(6,3) DEFAULT \'0.000\',
`PACKAGE_WIDTH` decimal(6,3) DEFAULT \'0.000\',
`PACKAGE_HEIGHT` decimal(6,3) DEFAULT \'0.000\',
`SOAK_LEVEL` varchar(45) DEFAULT NULL,
`BAKE_TEMPERATURE` varchar(30) DEFAULT NULL,
`DRB_TEMPERATURE` varchar(30) DEFAULT NULL,
`ACTUAL_CURE_TIME` int DEFAULT NULL,
`REFLOW_PROFILE` varchar(45) DEFAULT NULL,
`PINOUT_VERSION` varchar(45) DEFAULT NULL,
`DISPATCH_DUE_DATE` datetime DEFAULT NULL,
`BINOUT_DUE_DATE` datetime DEFAULT NULL,
`ROW_CREATED` datetime NOT NULL,
`ROW_MODIFIED` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`LOCATION_DATE` timestamp NOT NULL DEFAULT \'1970-01-01 12:00:00\',
`LOCATION_WW` varchar(10) DEFAULT NULL,
`MODULE_LOT` int NOT NULL DEFAULT \'0\',
`BURN_LOT` int DEFAULT \'0\',
`MONITOR_IGNORE` int NOT NULL DEFAULT \'0\',
`TICKER` int NOT NULL DEFAULT \'0\',
`PRIORITY` varchar(4) DEFAULT NULL,
`ASM_LOT_NUMBER` varchar(45) DEFAULT NULL,
`MARK_FORMAT` varchar(45) DEFAULT NULL,
`LOCATION_TAT` double DEFAULT \'0\',
`RPM_WW` varchar(10) DEFAULT NULL,
`QA_EVENT_ID` varchar(15) DEFAULT NULL,
`TC_WEIGHT` double DEFAULT \'0\',
`AUTOMOTIVE_LOT` varchar(45) DEFAULT NULL,
`CUSTOMER_OPTION` varchar(45) DEFAULT NULL,
`PKG_RECEIVE_DATE` datetime DEFAULT NULL,
`CUSTOMER_GROUP` varchar(45) DEFAULT NULL,
`SAMPLE_PULLED_DATE` datetime DEFAULT NULL,
`QA_SPECIAL_FLOW` varchar(45) DEFAULT NULL,
`QA_BLOCKS` varchar(45) DEFAULT NULL,
`PROBE_CUSTOM_TESTED` varchar(45) DEFAULT NULL,
`QA_PROGRAM_REV` varchar(45) DEFAULT NULL,
`NAND_FLOW_TYPE` varchar(45) DEFAULT NULL,
`NUM_FLASH_CE_PINS` varchar(45) DEFAULT NULL,
`RETICLE_WAVE_ID` varchar(45) DEFAULT NULL,
`MAJOR_PROBE_PROG_REV` varchar(45) DEFAULT NULL,
`MAJOR_TEST_PROG_REV` varchar(45) DEFAULT NULL,
`CYCLING_TYPE` varchar(45) DEFAULT NULL,
`QA_TARGET_CYCLE` varchar(45) DEFAULT NULL,
`LAST_TEST_INTERVAL` varchar(45) DEFAULT NULL,
`CYCLING_TEMPERATURE` varchar(45) DEFAULT NULL,
`ENV_STRESS_DURATION` varchar(45) DEFAULT NULL,
`FIRST_TEST_INTERVAL` varchar(45) DEFAULT NULL,
`DRB_TARGET_INTERVAL` varchar(45) DEFAULT NULL,
`LTDR_TEMPERATURE` varchar(45) DEFAULT NULL,
`RD_STRESS_TYPE` varchar(45) DEFAULT NULL,
PRIMARY KEY (`LOTID`),
KEY `design_id` (`DESIGN_ID`),
KEY `lot_location` (`LOT_LOCATION`),
KEY `burn` (`DESIGN_ID`,`QA_BURN_EXPERIMENT`),
KEY `locations` (`LOT_LOCATION`,`LOCATION`),
KEY `all_index` (`LOCATION`,`LOT_LOCATION`,`DISPATCH_DUE_DATE`,`PRODUCT_FAMILY`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
COMMENT=\'table to store MAM data for msa labs skynet\''
EXPLAIN QUERY:
id, select_type, table, partitions, type, possible_keys, key, key_len, ref, rows, filtered, Extra
'1', 'SIMPLE', 'X', NULL, 'ALL', 'PRIMARY', NULL, NULL, NULL, '55', '90.00', 'Using where'
'1', 'SIMPLE', 'Y', NULL, 'eq_ref', 'PRIMARY', 'PRIMARY', '14', 'skynet_msa.X.lotid', '1', '100.00', 'Using where'
'1', 'SIMPLE', 'A', NULL, 'ALL', NULL, NULL, NULL, NULL, '1458', '10.00', 'Using where; Using join buffer (hash join)'
Change from MyISAM to InnoDB. (It may not matter for this query.)
Tentative index to add:
A: INDEX(Serial_Number, status)
LEFT is not needed:
LEFT JOIN ( ... ) B ON ... WHERE b.id IS NOT NULL
-->
JOIN ( ... ) B ON ...
At that point, it may be reasonable to get rid of the nested LEFT JOIN, and simply have a single level A, X, Y joined together.
The EXPLAIN shows that at least some of those simplifications were automatically figured out by the Optimizer.
A.Serial_Number = B.`board_sn
I see "utf8" and "latin1". If any query JOINs on a VARCHAR between different Character Sets, (ore Collations), an otherwise-suitable index will not be used. If that is a problem, I suggest you use ALTER .. CONVERT TO .. to change latin1 to utf8.
I see columns called "DURATION" and "INTERVAL" being declared VARCHAR. This can cause trouble if you ever do numeric operations on such columns. (I understand things like "BOARD_SN" are not really numbers.)

How to populate mysql database column to json values

I try to populate mysql database column values to json values of items without success.
Database structure:
CREATE TABLE IF NOT EXISTS `ejkb4_k2_items` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`code` varchar(11) NOT NULL,
`title` varchar(255) NOT NULL,
`alias` varchar(255) DEFAULT NULL,
`catid` int(11) NOT NULL,
`published` smallint(6) NOT NULL DEFAULT '0',
`introtext` mediumtext,
`fulltext` mediumtext,
`video` text,
`gallery` varchar(255) DEFAULT NULL,
`extra_fields` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
`extra_fields_search` text NOT NULL,
`created` datetime NOT NULL,
`created_by` int(11) NOT NULL DEFAULT '0',
`created_by_alias` varchar(255) NOT NULL,
`checked_out` int(10) unsigned NOT NULL,
`checked_out_time` datetime NOT NULL,
`modified` datetime NOT NULL,
`modified_by` int(11) NOT NULL DEFAULT '0',
`publish_up` datetime NOT NULL,
`publish_down` datetime NOT NULL,
`trash` smallint(6) NOT NULL DEFAULT '0',
`access` int(11) NOT NULL DEFAULT '0',
`ordering` int(11) NOT NULL DEFAULT '0',
`featured` smallint(6) NOT NULL DEFAULT '0',
`featured_ordering` int(11) NOT NULL DEFAULT '0',
`image_caption` text NOT NULL,
`image_credits` varchar(255) NOT NULL,
`video_caption` text NOT NULL,
`video_credits` varchar(255) NOT NULL,
`hits` int(10) unsigned NOT NULL,
`params` text NOT NULL,
`metadesc` text NOT NULL,
`metadata` text NOT NULL,
`metakey` text NOT NULL,
`plugins` varchar(1024) NOT NULL DEFAULT 'k2storeitem_price=1',
`code_search` varchar(11) NOT NULL,
`language` char(7) NOT NULL,
PRIMARY KEY (`id`),
KEY `item` (`published`,`publish_up`,`publish_down`,`trash`,`access`),
KEY `catid` (`catid`),
KEY `created_by` (`created_by`),
KEY `ordering` (`ordering`),
KEY `featured` (`featured`),
KEY `featured_ordering` (`featured_ordering`),
KEY `hits` (`hits`),
KEY `language` (`language`),
FULLTEXT KEY `search` (`title`,`introtext`,`fulltext`,`extra_fields_search`,`image_caption`,`image_credits`,`video_caption`,`video_credits`,`metadesc`,`metakey`),
FULLTEXT KEY `title` (`title`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=992 ;
One item from this database table:
INSERT INTO `ejkb4_k2_items` (`id`, `code`, `title`, `alias`, `catid`, `published`, `introtext`, `fulltext`, `video`, `gallery`, `extra_fields`, `extra_fields_search`, `created`, `created_by`, `created_by_alias`, `checked_out`, `checked_out_time`, `modified`, `modified_by`, `publish_up`, `publish_down`, `trash`, `access`, `ordering`, `featured`, `featured_ordering`, `image_caption`, `image_credits`, `video_caption`, `video_credits`, `hits`, `params`, `metadesc`, `metadata`, `metakey`, `plugins`, `code_search`, `language`) VALUES
(992, '1234567', 'Спирателен вентил ХХХ', 'спирателен-вентил-ххх', 704, 1, '<ul>\r\n<li>материал</li>\r\n<li>още нещо</li>\r\n<li>друго...</li>\r\n<li>и още</li>\r\n</ul>', '', '<iframe width="560" height="315" src="https://www.youtube.com/embed/pMPinbC1VJ4" frameborder="0" allowfullscreen></iframe>', NULL, '[{"id":"1","value":"1\\""},{"id":"9","value":"22311"},{"id":"5","value":""},{"id":"6","value":""},{"id":"7","value":""},{"id":"8","value":""},{"id":"10","value":"\\u041f\\u0440\\u043e\\u0438\\u0437\\u0432\\u043e\\u0434\\u0438\\u0442\\u0435\\u043b \\u0425"}]', '1" 22311 Производител Х ', '2017-05-30 07:27:22', 50, '', 0, '0000-00-00 00:00:00', '2017-06-06 13:20:35', 50, '2017-05-30 07:27:22', '0000-00-00 00:00:00', 0, 1, 2, 0, 0, '', '', '', '', 14, '{"catItemTitle":"","catItemTitleLinked":"","catItemFeaturedNotice":"","catItemAuthor":"","catItemDateCreated":"","catItemRating":"","catItemImage":"","catItemIntroText":"","catItemExtraFields":"","catItemHits":"","catItemCategory":"","catItemTags":"","catItemAttachments":"","catItemAttachmentsCounter":"","catItemVideo":"","catItemVideoWidth":"","catItemVideoHeight":"","catItemAudioWidth":"","catItemAudioHeight":"","catItemVideoAutoPlay":"","catItemImageGallery":"","catItemImageGalleryWidth":"","catItemImageGalleryHeight":"","catItemDateModified":"","catItemReadMore":"","catItemCommentsAnchor":"","catItemK2Plugins":"","itemDateCreated":"","itemTitle":"","itemFeaturedNotice":"","itemAuthor":"","itemFontResizer":"","itemPrintButton":"","itemEmailButton":"","itemSocialButton":"","itemVideoAnchor":"","itemImageGalleryAnchor":"","itemCommentsAnchor":"","itemRating":"","itemImage":"","itemImgSize":"","itemImageMainCaption":"","itemImageMainCredits":"","itemIntroText":"","itemFullText":"","itemExtraFields":"","itemDateModified":"","itemHits":"","itemCategory":"","itemTags":"","itemAttachments":"","itemAttachmentsCounter":"","itemVideo":"","itemVideoWidth":"","itemVideoHeight":"","itemAudioWidth":"","itemAudioHeight":"","itemVideoAutoPlay":"","itemVideoCaption":"","itemVideoCredits":"","itemImageGallery":"","itemImageGalleryWidth":"","itemImageGalleryHeight":"","itemNavigation":"","itemComments":"","itemTwitterButton":"","itemFacebookButton":"","itemGooglePlusOneButton":"","itemAuthorBlock":"","itemAuthorImage":"","itemAuthorDescription":"","itemAuthorURL":"","itemAuthorEmail":"","itemAuthorLatest":"","itemAuthorLatestLimit":"","itemRelated":"","itemRelatedLimit":"","itemRelatedTitle":"","itemRelatedCategory":"","itemRelatedImageSize":"","itemRelatedIntrotext":"","itemRelatedFulltext":"","itemRelatedAuthor":"","itemRelatedMedia":"","itemRelatedImageGallery":"","itemK2Plugins":""}', '', 'robots=\nauthor=', '', '{"k2storeitem_enabled":"1","k2storeitem_sku":"","k2storeitem_price":"","k2storespecial_price":"","k2storeitem_tax":"0","k2storeitem_shipping":"0","k2storeitem_metrics":{"item_length":"0.00000000","item_width":"0.00000000","item_height":"0.00000000","item_length_class_id":"0","item_weight":"0.00000000","item_weight_class_id":"0"},"k2storeitem_cart_text":""}', '', '*');
All values from column code must be populated to column extra_fields - as value of id:9. For example {"id":"9","value":"22311"} must going to: {"id":"9","value":"1234567"} in all database items.

Bad performance in MYSQL 5.7 after Upgrade from 5.6.27

I've got a query like the following:
SELECT * FROM table_a
LEFT JOIN table_b ON table_a.nr = table_b.a_nr
ORDER BY table_b.name
Under MYSQL 5.7.9, this query takes about 120sek.
Under 5.6.27 "only" ~25 sek. There are 500000 rows in table_a. Why is 5.7.9 that slow?
I know, Mysql can not use an index for sorting, cause of the joined column.
I made test with InnoDB and the MYISAM engine with the same result.
Any suggestions?
EDIT
CREATE TABLE `table_a` (
`NR` CHAR(60) NOT NULL DEFAULT '',
`LIEFNR` CHAR(6) NOT NULL DEFAULT '',
`BEZ1` CHAR(80) NOT NULL DEFAULT '',
`BEZ2` CHAR(40) NOT NULL DEFAULT '',
`SBEZ1` CHAR(80) NOT NULL DEFAULT '',
`SBEZ2` CHAR(40) NOT NULL DEFAULT '',
`MENGENEINH` CHAR(5) NOT NULL DEFAULT '',
`VKPREIS` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`PREISEINH` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`PREISEINH_IST` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`PREISEINHVK` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`PREISEINHVK_IST` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`EAN` CHAR(14) NOT NULL DEFAULT '',
`EANUSER` CHAR(14) NOT NULL DEFAULT '',
`EKPREIS` DECIMAL(13,5) NOT NULL DEFAULT '0.00000',
`LISTENEKPREIS` DECIMAL(13,5) NOT NULL DEFAULT '0.00000',
`BESTELLNR` CHAR(25) NOT NULL DEFAULT '',
`LANGTEXT` longtext,
`KATSEITE` CHAR(5) NOT NULL DEFAULT '',
`HERSTARTNR` CHAR(50) NOT NULL DEFAULT '',
`HWGR` CHAR(50) NOT NULL DEFAULT '',
`WGR` CHAR(50) NOT NULL DEFAULT '',
`MWST` CHAR(6) NOT NULL DEFAULT '',
`EKRABGR` CHAR(3) NOT NULL DEFAULT '',
`VKRABGR` CHAR(3) NOT NULL DEFAULT '',
`VERPAMENGE` DECIMAL(5,0) NOT NULL DEFAULT '0',
`PACKINH` DECIMAL(7,0) NOT NULL DEFAULT '0',
`MARK` CHAR(1) NOT NULL DEFAULT '',
`UEBERGABE` CHAR(1) NOT NULL DEFAULT '',
`GEWICHT` DECIMAL(10,3) NOT NULL DEFAULT '0.000',
`RABATT` DECIMAL(6,2) NOT NULL DEFAULT '0.00',
`ARTUID` CHAR(10) NOT NULL DEFAULT '',
`PREINH2` CHAR(5) NOT NULL DEFAULT '',
`NPREIS2` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`PREINH3` CHAR(5) NOT NULL DEFAULT '',
`NPREIS3` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`PREINH4` CHAR(5) NOT NULL DEFAULT '',
`NPREIS4` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`PREINH5` CHAR(5) NOT NULL DEFAULT '',
`NPREIS5` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`NPREISFAK` DECIMAL(13,5) NOT NULL DEFAULT '0.00000',
`EKSTAFM1` CHAR(5) NOT NULL DEFAULT '',
`EKSTAFP1` DECIMAL(13,5) NOT NULL DEFAULT '0.00000',
`EKSTAFM2` CHAR(5) NOT NULL DEFAULT '',
`EKSTAFP2` DECIMAL(13,5) NOT NULL DEFAULT '0.00000',
`EKSTAFM3` CHAR(5) NOT NULL DEFAULT '',
`EKSTAFP3` DECIMAL(13,5) NOT NULL DEFAULT '0.00000',
`EKSTAFM4` CHAR(5) NOT NULL DEFAULT '',
`EKSTAFP4` DECIMAL(13,5) NOT NULL DEFAULT '0.00000',
`EKSTAFM5` CHAR(5) NOT NULL DEFAULT '',
`EKSTAFP5` DECIMAL(13,5) NOT NULL DEFAULT '0.00000',
`EKSTAFFAK` DECIMAL(13,5) NOT NULL DEFAULT '0.00000',
`PREISEMPF` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`UMRFAKTOR` DECIMAL(12,5) NOT NULL DEFAULT '0.00000',
`LIEFILN` CHAR(13) NOT NULL DEFAULT '',
`MODE` CHAR(1) NOT NULL DEFAULT '',
`HERSTNAME` CHAR(50) NOT NULL DEFAULT '',
`VERWEIS` CHAR(30) NOT NULL DEFAULT '',
`MINDBEST` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`AENDDAT` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`ANLAGEDAT` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`LASTIMPORT` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`NOTUEBERN` CHAR(1) NOT NULL DEFAULT '',
`PERSBEZPREIS` CHAR(1) NOT NULL DEFAULT '',
`AKTUELLTEMP` CHAR(1) NOT NULL DEFAULT '',
`NICHTAKTUELL` CHAR(1) NOT NULL DEFAULT '',
`ZUSATZEAN1` CHAR(14) NOT NULL DEFAULT '',
`ZUSATZEAN2` CHAR(14) NOT NULL DEFAULT '',
`ZUSATZEAN3` CHAR(14) NOT NULL DEFAULT '',
`ZUSATZEAN4` CHAR(14) NOT NULL DEFAULT '',
`ZUSATZEAN5` CHAR(14) NOT NULL DEFAULT '',
`AUSLAUFARTI` CHAR(1) NOT NULL DEFAULT '',
`PAFAKTOR` DECIMAL(13,6) NOT NULL DEFAULT '0.000000',
`PREISFOKUS` CHAR(1) NOT NULL DEFAULT '',
`IMPORTKENNZ` CHAR(1) NOT NULL DEFAULT '',
`MATCHARTNR` CHAR(40) NOT NULL DEFAULT '',
`EXPORTFREIGABE` CHAR(1) NOT NULL DEFAULT '',
`MD5` CHAR(32) NOT NULL DEFAULT '',
`KEYWORDS` longtext,
`SHOPTEXT` longtext,
`BEMERKUNG` CHAR(80) NOT NULL DEFAULT '',
`SYS_UID` CHAR(10) NOT NULL DEFAULT '',
`SYS_DATE` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY `P_ARTI01` (`SYS_UID`),
KEY `P_ARTI02` (`LIEFNR`,`NR`,`MD5`),
KEY `P_ARTI03` (`LIEFNR`,`BEZ1`),
KEY `P_ARTI04` (`LIEFNR`,`HWGR`,`WGR`),
KEY `P_ARTI05` (`EAN`,`LIEFNR`),
KEY `P_ARTI06` (`AENDDAT`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
CREATE TABLE `table_b` (
`A_NR` CHAR(25) NOT NULL DEFAULT '',
`A_FNR` CHAR(25) NOT NULL DEFAULT '',
`A_NAME1` CHAR(40) NOT NULL DEFAULT '',
`A_NAME2` CHAR(40) NOT NULL DEFAULT '',
`A_EINHEIT` CHAR(2) NOT NULL DEFAULT '',
`A_MENGEING` DECIMAL(2,0) NOT NULL DEFAULT '0',
`A_VERPACK` CHAR(3) NOT NULL DEFAULT '',
`A_WGR` CHAR(3) NOT NULL DEFAULT '',
`A_WGRU` CHAR(3) NOT NULL DEFAULT '',
`A_ECLASS` CHAR(20) NOT NULL DEFAULT '',
`A_LAGORT` CHAR(10) NOT NULL DEFAULT '',
`A_MENGEINH` DECIMAL(6,0) NOT NULL DEFAULT '0',
`A_VK1` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_VK2` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_VK3` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_VK4` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_VK5` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_VK6` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_VK7` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_VK8` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_VK9` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_VKB1` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_VKB2` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_VKB3` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_VKB4` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_VKB5` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_VKB6` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_VKB7` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_VKB8` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_VKB9` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_VKBED1` CHAR(18) NOT NULL DEFAULT '',
`A_VKBED2` CHAR(18) NOT NULL DEFAULT '',
`A_VKBED3` CHAR(18) NOT NULL DEFAULT '',
`A_VKBED4` CHAR(18) NOT NULL DEFAULT '',
`A_VKBED5` CHAR(18) NOT NULL DEFAULT '',
`A_VKBED6` CHAR(18) NOT NULL DEFAULT '',
`A_VKBED7` CHAR(18) NOT NULL DEFAULT '',
`A_VKBED8` CHAR(18) NOT NULL DEFAULT '',
`A_VKBED9` CHAR(18) NOT NULL DEFAULT '',
`A_ERLDM1` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_ERLDM2` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_ERLDM3` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_ERLDM4` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_ERLDM5` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_ERLDM6` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_ERLDM7` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_ERLDM8` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_ERLDM9` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_ERLPRO1` DECIMAL(7,3) NOT NULL DEFAULT '0.000',
`A_ERLPRO2` DECIMAL(7,3) NOT NULL DEFAULT '0.000',
`A_ERLPRO3` DECIMAL(7,3) NOT NULL DEFAULT '0.000',
`A_ERLPRO4` DECIMAL(7,3) NOT NULL DEFAULT '0.000',
`A_ERLPRO5` DECIMAL(7,3) NOT NULL DEFAULT '0.000',
`A_ERLPRO6` DECIMAL(7,3) NOT NULL DEFAULT '0.000',
`A_ERLPRO7` DECIMAL(7,3) NOT NULL DEFAULT '0.000',
`A_ERLPRO8` DECIMAL(7,3) NOT NULL DEFAULT '0.000',
`A_ERLPRO9` DECIMAL(7,3) NOT NULL DEFAULT '0.000',
`A_LIEFER1` CHAR(6) NOT NULL DEFAULT '',
`A_LIEFER2` CHAR(6) NOT NULL DEFAULT '',
`A_LIEFER3` CHAR(6) NOT NULL DEFAULT '',
`A_LIEFER4` CHAR(6) NOT NULL DEFAULT '',
`A_LIEFER5` CHAR(6) NOT NULL DEFAULT '',
`A_EK1` DECIMAL(13,5) NOT NULL DEFAULT '0.00000',
`A_EK2` DECIMAL(13,5) NOT NULL DEFAULT '0.00000',
`A_EK3` DECIMAL(13,5) NOT NULL DEFAULT '0.00000',
`A_EK4` DECIMAL(13,5) NOT NULL DEFAULT '0.00000',
`A_EK5` DECIMAL(13,5) NOT NULL DEFAULT '0.00000',
`A_EKVKPROZ` DECIMAL(5,2) NOT NULL DEFAULT '0.00',
`A_BON1` DECIMAL(5,2) NOT NULL DEFAULT '0.00',
`A_BON2` DECIMAL(5,2) NOT NULL DEFAULT '0.00',
`A_BON3` DECIMAL(5,2) NOT NULL DEFAULT '0.00',
`A_BON4` DECIMAL(5,2) NOT NULL DEFAULT '0.00',
`A_BON5` DECIMAL(5,2) NOT NULL DEFAULT '0.00',
`A_RAB1` DECIMAL(5,2) NOT NULL DEFAULT '0.00',
`A_RAB2` DECIMAL(5,2) NOT NULL DEFAULT '0.00',
`A_RAB3` DECIMAL(5,2) NOT NULL DEFAULT '0.00',
`A_RAB4` DECIMAL(5,2) NOT NULL DEFAULT '0.00',
`A_RAB5` DECIMAL(5,2) NOT NULL DEFAULT '0.00',
`A_BESTNR1` CHAR(60) NOT NULL DEFAULT '',
`A_BESTNR2` CHAR(60) NOT NULL DEFAULT '',
`A_BESTNR3` CHAR(60) NOT NULL DEFAULT '',
`A_BESTNR4` CHAR(60) NOT NULL DEFAULT '',
`A_BESTNR5` CHAR(60) NOT NULL DEFAULT '',
`A_BESDAT1` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`A_BESDAT2` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`A_BESDAT3` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`A_BESDAT4` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`A_BESDAT5` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`A_EAN` CHAR(14) NOT NULL DEFAULT '',
`A_POSKEN` CHAR(4) NOT NULL DEFAULT '',
`A_BESTAND` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_MAXBE` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_MELDEBE` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_UMSASTK` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_UMSJA` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_UMSLF` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_UMSQU` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_UMSVJ` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_CNTLF` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_CNTJA` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_CNTQU` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_CNTVJ` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_EKLF` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_EKQU` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_EKJA` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_EKSLF` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_EKSQU` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_EKSJA` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_UMSDATUM` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`A_EKDATUM` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`A_ANLAGE` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`A_LTZAEND` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`A_LDAT` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`A_LZEIT` CHAR(5) NOT NULL DEFAULT '',
`A_MWSTSCHL` CHAR(2) NOT NULL DEFAULT '',
`A_VSTSCHL` CHAR(2) NOT NULL DEFAULT '',
`A_INVENTUR` CHAR(3) NOT NULL DEFAULT '',
`A_GERAET` CHAR(1) NOT NULL DEFAULT '',
`A_SERIENNR` CHAR(1) NOT NULL DEFAULT '',
`A_INOUTPUT` CHAR(1) NOT NULL DEFAULT '',
`A_FAKTURA` CHAR(1) NOT NULL DEFAULT '',
`A_RABATT` CHAR(1) NOT NULL DEFAULT '',
`A_RABGR1` DECIMAL(5,2) NOT NULL DEFAULT '0.00',
`A_RABGR2` DECIMAL(5,2) NOT NULL DEFAULT '0.00',
`A_RABGR3` DECIMAL(5,2) NOT NULL DEFAULT '0.00',
`A_RABGR4` DECIMAL(5,2) NOT NULL DEFAULT '0.00',
`A_RABGR5` DECIMAL(5,2) NOT NULL DEFAULT '0.00',
`A_ERLKTO` CHAR(3) NOT NULL DEFAULT '',
`DELETED` CHAR(1) NOT NULL DEFAULT '',
`A_SNAME1` CHAR(40) NOT NULL DEFAULT '',
`A_SNAME2` CHAR(40) NOT NULL DEFAULT '',
`A_SEINHEIT` CHAR(2) NOT NULL DEFAULT '',
`A_SPRACHE` CHAR(3) NOT NULL DEFAULT '',
`A_URLAND` CHAR(3) NOT NULL DEFAULT '',
`A_LANGTEXT` longtext,
`A_LANGTEXTANSI` longtext,
`A_GEBINDE` CHAR(3) NOT NULL DEFAULT '',
`A_POSANZ` DECIMAL(8,2) NOT NULL DEFAULT '0.00',
`A_DATANORM` CHAR(25) NOT NULL DEFAULT '',
`A_HANDGW` DECIMAL(9,4) NOT NULL DEFAULT '0.0000',
`A_ARTNR` CHAR(25) NOT NULL DEFAULT '',
`A_ARTIPOS` CHAR(6) NOT NULL DEFAULT '',
`A_LAGPOS` CHAR(8) NOT NULL DEFAULT '',
`A_SERLKTO` CHAR(3) NOT NULL DEFAULT '',
`A_EGKTO` CHAR(3) NOT NULL DEFAULT '',
`A_EKDURCH` DECIMAL(13,5) NOT NULL DEFAULT '0.00000',
`A_DBESTAND` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_RESBEST` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_KZMENGE` CHAR(1) NOT NULL DEFAULT '',
`A_KZFEST1` DECIMAL(10,3) NOT NULL DEFAULT '0.000',
`A_KZFEST2` DECIMAL(10,3) NOT NULL DEFAULT '0.000',
`A_KZFEST3` DECIMAL(10,3) NOT NULL DEFAULT '0.000',
`A_STDVERR` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_EKVORJA` DECIMAL(13,5) NOT NULL DEFAULT '0.00000',
`A_PROZFRA` DECIMAL(5,2) NOT NULL DEFAULT '0.00',
`A_BESZEIT` DECIMAL(4,0) NOT NULL DEFAULT '0',
`A_PROVSATZ` DECIMAL(5,2) NOT NULL DEFAULT '0.00',
`A_LADELIST` CHAR(1) NOT NULL DEFAULT '',
`A_PAVKN1` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_PAVKN2` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_PAVKN3` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_PAVKN4` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_PAVKN5` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_PAVKB1` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_PAVKB2` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_PAVKB3` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_PAVKB4` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_PAVKB5` DECIMAL(10,2) NOT NULL DEFAULT '0.00',
`A_PRODSC` CHAR(4) NOT NULL DEFAULT '',
`A_PRODZEIT` DECIMAL(5,2) NOT NULL DEFAULT '0.00',
`A_PRODLI` CHAR(2) NOT NULL DEFAULT '',
`A_MINPROV` DECIMAL(7,2) NOT NULL DEFAULT '0.00',
`A_TYP` CHAR(1) NOT NULL DEFAULT '',
`A_EKSTK` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_EKDM` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_STFAKTOR` DECIMAL(5,2) NOT NULL DEFAULT '0.00',
`A_ABSATZ` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_KVER` CHAR(1) NOT NULL DEFAULT '',
`A_ABESTAND` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_LOHNVJ` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_LOHNSUMM` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_SUCHBEGRIFF` CHAR(40) NOT NULL DEFAULT '',
`A_GFAKTOR` DECIMAL(8,3) NOT NULL DEFAULT '0.000',
`A_GMEINH` CHAR(2) NOT NULL DEFAULT '',
`A_IBEWERT` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_IBEWSTUND` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_LOHNSTD` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_MASCHSTD` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_SUCHTEXT` longtext,
`A_SUCHTEXTANSI` longtext,
`A_GERART` CHAR(3) NOT NULL DEFAULT '',
`A_LAGER` CHAR(1) NOT NULL DEFAULT '',
`A_BEMERKUNG` CHAR(60) NOT NULL DEFAULT '',
`A_KEINBEST` CHAR(1) NOT NULL DEFAULT '',
`A_PRODUKTION` CHAR(1) NOT NULL DEFAULT '',
`A_SKFJN` CHAR(1) NOT NULL DEFAULT '',
`A_AKLART1` CHAR(4) NOT NULL DEFAULT '',
`A_AKLART2` CHAR(4) NOT NULL DEFAULT '',
`A_AKLART3` CHAR(4) NOT NULL DEFAULT '',
`A_AKLART4` CHAR(4) NOT NULL DEFAULT '',
`A_AKLART5` CHAR(4) NOT NULL DEFAULT '',
`A_AKLART6` CHAR(4) NOT NULL DEFAULT '',
`A_AKLART7` CHAR(4) NOT NULL DEFAULT '',
`A_AKLART8` CHAR(4) NOT NULL DEFAULT '',
`A_AKLART9` CHAR(4) NOT NULL DEFAULT '',
`A_AKSATZ1` DECIMAL(8,4) NOT NULL DEFAULT '0.0000',
`A_AKSATZ2` DECIMAL(8,4) NOT NULL DEFAULT '0.0000',
`A_AKSATZ3` DECIMAL(8,4) NOT NULL DEFAULT '0.0000',
`A_AKSATZ4` DECIMAL(8,4) NOT NULL DEFAULT '0.0000',
`A_AKSATZ5` DECIMAL(8,4) NOT NULL DEFAULT '0.0000',
`A_AKSATZ6` DECIMAL(8,4) NOT NULL DEFAULT '0.0000',
`A_AKSATZ7` DECIMAL(8,4) NOT NULL DEFAULT '0.0000',
`A_AKSATZ8` DECIMAL(8,4) NOT NULL DEFAULT '0.0000',
`A_AKSATZ9` DECIMAL(8,4) NOT NULL DEFAULT '0.0000',
`A_AKSOLL1` CHAR(20) NOT NULL DEFAULT '',
`A_AKSOLL2` CHAR(20) NOT NULL DEFAULT '',
`A_AKSOLL3` CHAR(20) NOT NULL DEFAULT '',
`A_AKSOLL4` CHAR(20) NOT NULL DEFAULT '',
`A_AKSOLL5` CHAR(20) NOT NULL DEFAULT '',
`A_AKSOLL6` CHAR(20) NOT NULL DEFAULT '',
`A_AKSOLL7` CHAR(20) NOT NULL DEFAULT '',
`A_AKSOLL8` CHAR(20) NOT NULL DEFAULT '',
`A_AKSOLL9` CHAR(20) NOT NULL DEFAULT '',
`A_INAKTIV` CHAR(1) NOT NULL DEFAULT '',
`A_AUTOINAKTIV` CHAR(1) NOT NULL DEFAULT '',
`A_STCKOPT` CHAR(1) NOT NULL DEFAULT '',
`A_FILIALE` CHAR(2) NOT NULL DEFAULT '',
`A_PRODMENG` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_SONDERART` CHAR(1) NOT NULL DEFAULT '',
`A_BETONREZEPT` DECIMAL(3,0) NOT NULL DEFAULT '0',
`A_UEBERDECKUNG` DECIMAL(3,1) NOT NULL DEFAULT '0.0',
`A_UEBERDECKUNG_R` DECIMAL(3,1) NOT NULL DEFAULT '0.0',
`A_UEBERDECKUNG_O` DECIMAL(3,1) NOT NULL DEFAULT '0.0',
`A_UEBERDECKUNG_U` DECIMAL(3,1) NOT NULL DEFAULT '0.0',
`A_MENGEFORMEN` DECIMAL(3,0) NOT NULL DEFAULT '0',
`A_TRANSLAGER` CHAR(2) NOT NULL DEFAULT '',
`A_GARANTIE` DECIMAL(4,0) NOT NULL DEFAULT '0',
`A_HERSTELLERNR` CHAR(80) NOT NULL DEFAULT '',
`A_HERSTELLERNAME` CHAR(40) NOT NULL DEFAULT '',
`A_PREISFOKUS` CHAR(1) NOT NULL DEFAULT '',
`A_MINDBESTMENGE` DECIMAL(12,2) NOT NULL DEFAULT '0.00',
`A_NEUKALK` CHAR(1) NOT NULL DEFAULT '',
`A_PREISFLAG1` CHAR(1) NOT NULL DEFAULT '',
`A_PREISFLAG2` CHAR(1) NOT NULL DEFAULT '',
`A_PREISFLAG3` CHAR(1) NOT NULL DEFAULT '',
`A_KEINEXTTEXT` CHAR(1) NOT NULL DEFAULT '',
`A_ZEN_KBEZ` CHAR(1) NOT NULL DEFAULT '',
`A_SHOP` CHAR(1) NOT NULL DEFAULT '',
`A_SHOP_NOSEARCH` CHAR(1) NOT NULL DEFAULT '',
`A_SHOPTEXTKD` CHAR(1) NOT NULL DEFAULT '',
`A_SHOPTEXTKD_TITEL` CHAR(80) NOT NULL DEFAULT '',
`A_SHOPBEZ` CHAR(100) NOT NULL DEFAULT '',
`A_METATITLE` CHAR(100) NOT NULL DEFAULT '',
`A_METADESCR` CHAR(255) NOT NULL DEFAULT '',
`A_METAKEYWORDS` CHAR(255) NOT NULL DEFAULT '',
`A_GUTSCHEIN` CHAR(1) NOT NULL DEFAULT '',
`A_SHOPARTNR` CHAR(80) NOT NULL DEFAULT '',
`A_VSSF_FAKTOR` DECIMAL(5,2) NOT NULL DEFAULT '0.00',
`A_VSSF_VON` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`A_VSSF_BIS` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`A_VSSF_A_FAKTOR` DECIMAL(5,2) NOT NULL DEFAULT '0.00',
`A_VSSF_A_VON` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`A_VSSF_A_BIS` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`A_ERFASSER` CHAR(3) NOT NULL DEFAULT '',
`A_COLSTATUS` CHAR(1) NOT NULL DEFAULT '',
`SYS_ZENTRALE` CHAR(10) NOT NULL DEFAULT '',
`SYS_UID` CHAR(10) NOT NULL DEFAULT '',
`SYS_DATE` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY `ARTIKEL01` (`SYS_UID`),
UNIQUE KEY `ARTIKEL02` (`A_NR`),
KEY `ARTIKEL03` (`A_NAME1`,`A_NAME2`),
KEY `ARTIKEL04` (`A_DATANORM`),
KEY `ARTIKEL05` (`A_WGR`,`A_NAME1`,`A_NAME2`),
KEY `ARTIKEL06` (`A_LAGPOS`,`A_NAME1`,`A_NAME2`),
KEY `ARTIKEL07` (`A_ARTIPOS`),
KEY `ARTIKEL08` (`A_SUCHBEGRIFF`),
KEY `ARTIKEL09` (`A_EAN`),
KEY `ARTIKEL10` (`A_FILIALE`),
KEY `ARTIKEL11` (`A_FILIALE`,`A_NR`),
KEY `ARTIKEL12` (`A_INAKTIV`,`A_NR`),
KEY `ARTIKEL13` (`A_LAGER`),
KEY `ARTIKEL14` (`A_ARTNR`),
KEY `ARTIKEL15` (`A_HERSTELLERNR`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
Sometimes it's quite useful. if you are linking some documents to costumer informations, you'll have unlinked dokuments.
Use consistent definitions for columns being JOINed on.
Use VARCHAR, not CHAR unless the column is a constant length.
Use an extra table rather than putting "arrays" as columns: A_AKSOLL9 (etc).
Did you set innodb_buffer_pool_size to a reasonable value when testing InnoDB?

Auto increment it's not working

I have a problem on my website with auto increment and I can't find a way to fix it...
It works with this table:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(40) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`password` varchar(40) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`loginhash` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
`id_level` int(10) NOT NULL DEFAULT '1',
`random` int(10) DEFAULT '0',
`email` varchar(30) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`language` tinyint(4) NOT NULL DEFAULT '1',
`style` tinyint(4) NOT NULL DEFAULT '1',
`joined` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`lastconnect` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`lip` bigint(11) DEFAULT '0',
`downloaded` bigint(20) DEFAULT '0',
`uploaded` bigint(20) DEFAULT '0',
`avatar` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`pid` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`flag` tinyint(1) unsigned NOT NULL DEFAULT '0',
`topicsperpage` tinyint(3) unsigned NOT NULL DEFAULT '15',
`postsperpage` tinyint(3) unsigned NOT NULL DEFAULT '15',
`torrentsperpage` tinyint(3) unsigned NOT NULL DEFAULT '15',
`cip` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
`time_offset` varchar(4) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
`temp_email` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`flags` bigint(20) unsigned NOT NULL DEFAULT '7345036',
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
KEY `id_level` (`id_level`),
KEY `pid` (`pid`),
KEY `cip` (`cip`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
But it doesn't work with this table:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(40) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`password` varchar(40) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`loginhash` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
`id_level` int(10) NOT NULL DEFAULT '1',
`random` int(10) DEFAULT '0',
`email` varchar(30) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`language` tinyint(4) NOT NULL DEFAULT '1',
`style` tinyint(4) NOT NULL DEFAULT '1',
`joined` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`lastconnect` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`lip` bigint(11) DEFAULT '0',
`downloaded` bigint(20) DEFAULT '0',
`uploaded` bigint(20) DEFAULT '0',
`avatar` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`pid` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`flag` tinyint(1) unsigned NOT NULL DEFAULT '0',
`topicsperpage` tinyint(3) unsigned NOT NULL DEFAULT '15',
`postsperpage` tinyint(3) unsigned NOT NULL DEFAULT '15',
`torrentsperpage` tinyint(3) unsigned NOT NULL DEFAULT '15',
`cip` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
`time_offset` varchar(4) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
`temp_email` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`flags` bigint(20) unsigned NOT NULL DEFAULT '7345036',
`rank_switch` enum('yes','no') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'no',
`old_rank` varchar(12) COLLATE utf8_unicode_ci NOT NULL DEFAULT '3',
`timed_rank` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`seedbonus` decimal(5,1) NOT NULL,
`on_forum` int(11) NOT NULL DEFAULT '0',
`on_topic` int(11) NOT NULL DEFAULT '0',
`custom_title` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`invites` int(10) NOT NULL DEFAULT '0',
`invited_by` int(10) NOT NULL DEFAULT '0',
`invitedate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`donor` varchar(10) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'no',
`banfromforum` enum('yes','no') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'no',
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
KEY `id_level` (`id_level`),
KEY `pid` (`pid`),
KEY `cip` (`cip`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
Can anyone tell me where the problem is?

Variable or Inner Select in Like Operator?

I need to make some updates in a table and I want to update the some values based on a like statement. e.g Like '%0010030 a.jpg'
The 0010030 is an example of a product code in my database and it reaches a specific number till now.
I would like to make an for loop to make the changes of i=0010030 to the number I need, i++.
I not sure is it works but i am trying to set it like this '%Variable_Name a.jpg'
In order do it automatically.
I actually have a list of sku/ids that i need to check
Any help or ideas?
What i have:
Insert Ignore Into prefix_virtuemart_product_medias(virtuemart_product_id,virtuemart_media_id,ordering) VALUES (
(Select nprefix_virtuemart_products.virtuemart_product_id from prefix_virtuemart_products where prefix_virtuemart_products.product_sku LIKE '0010030'),
(Select prefix_virtuemart_medias.virtuemart_media_id from prefix_virtuemart_medias
where prefix_virtuemart_medias.file_url LIKE '%0010030 a.jpg' or prefix_virtuemart_medias.file_url LIKE '%0010030 A.jpg'),5);
But i need to automatically change the LIKE parameter from a column that has all of these product codes (it's the product_sku field)
Tables dump with sample data:
CREATE TABLE IF NOT EXISTS `prefix_virtuemart_product_medias` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`virtuemart_product_id` int(1) unsigned NOT NULL DEFAULT '0',
`virtuemart_media_id` int(1) unsigned NOT NULL DEFAULT '0',
`ordering` int(2) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `i_virtuemart_product_id` (`virtuemart_product_id`,`virtuemart_media_id`),
KEY `i_ordering` (`ordering`)
) ENGINE=MyISAM AUTO_INCREMENT=3443 DEFAULT CHARSET=utf8;
-- Dumping data for table virtuemart_product_medias: 1.496 rows
/*!40000 ALTER TABLE `prefix_virtuemart_product_medias` DISABLE KEYS */;
INSERT INTO `prefix_virtuemart_product_medias` (`id`, `virtuemart_product_id`, `virtuemart_media_id`, `ordering`) VALUES
(1909, 2849, 12595, 1),
(1910, 2849, 12596, 1),
(1911, 2849, 12597, 1),
-- Dumping structure for table virtuemart_medias
CREATE TABLE IF NOT EXISTS `prefix_virtuemart_medias` (
`virtuemart_media_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`virtuemart_vendor_id` smallint(1) NOT NULL DEFAULT '1',
`file_title` char(126) NOT NULL DEFAULT '',
`file_description` char(254) NOT NULL DEFAULT '',
`file_meta` char(254) NOT NULL DEFAULT '',
`file_mimetype` char(64) NOT NULL DEFAULT '',
`file_type` char(32) NOT NULL DEFAULT '',
`file_url` varchar(900) NOT NULL DEFAULT '',
`file_url_thumb` varchar(900) NOT NULL DEFAULT '',
`file_is_product_image` tinyint(1) NOT NULL DEFAULT '0',
`file_is_downloadable` tinyint(1) NOT NULL DEFAULT '0',
`file_is_forSale` tinyint(1) NOT NULL DEFAULT '0',
`file_params` varchar(17500) DEFAULT NULL,
`file_lang` varchar(500) NOT NULL,
`shared` tinyint(1) NOT NULL DEFAULT '0',
`published` tinyint(1) NOT NULL DEFAULT '1',
`created_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` int(11) NOT NULL DEFAULT '0',
`modified_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`modified_by` int(11) NOT NULL DEFAULT '0',
`locked_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`locked_by` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`virtuemart_media_id`),
KEY `i_virtuemart_vendor_id` (`virtuemart_vendor_id`),
KEY `i_published` (`published`),
KEY `i_shared` (`shared`)
) ENGINE=MyISAM AUTO_INCREMENT=16811 DEFAULT CHARSET=utf8 COMMENT='Additional Images and Files which are assigned to products';
-- Dumping data for table virtuemart_medias: 4.216 rows
/*!40000 ALTER TABLE `prefix_virtuemart_medias` DISABLE KEYS */;
INSERT INTO `prefix_virtuemart_medias` (`virtuemart_media_id`, `virtuemart_vendor_id`, `file_title`, `file_description`, `file_meta`, `file_mimetype`, `file_type`, `file_url`, `file_url_thumb`, `file_is_product_image`, `file_is_downloadable`, `file_is_forSale`, `file_params`, `file_lang`, `shared`, `published`, `created_on`, `created_by`, `modified_on`, `modified_by`, `locked_on`, `locked_by`) VALUES
(12595, 1, '0010030 MIZA DAIHATSU', '0010030 MIZA DAIHATSU', '0010030 MIZA DAIHATSU', 'image/jpeg', 'product', 'images/stories/virtuemart/product/0010030 a.jpg', '', 0, 0, 0, '', '', 0, 1, '2014-05-10 06:06:44', 466, '2014-05-23 14:09:03', 466, '0000-00-00 00:00:00', 0),
(12596, 1, '0010030 MIZA DAIHATSU', '0010030 MIZA DAIHATSU', '0010030 MIZA DAIHATSU', 'image/jpeg', 'product', 'images/stories/virtuemart/product/0010030 b.jpg', '', 0, 0, 0, '', '', 0, 1, '2014-05-10 06:06:44', 466, '2014-05-23 14:09:03', 466, '0000-00-00 00:00:00', 0),
(12597, 1, '0010030 MIZA DAIHATSU', '0010030 MIZA DAIHATSU', '0010030 MIZA DAIHATSU', 'image/jpeg', 'product', 'images/stories/virtuemart/product/0010030 c.jpg', '', 0, 0, 0, '', '', 0, 1, '2014-05-10 06:06:44', 466, '2014-05-23 14:09:03', 466, '0000-00-00 00:00:00', 0),
(12598, 1, '0010030 MIZA DAIHATSU', '0010030 MIZA DAIHATSU', '0010030 MIZA DAIHATSU', 'image/jpeg', 'product', 'images/stories/virtuemart/product/0010030 d.jpg', '', 0, 0, 0, '', '', 0, 1, '2014-05-10 06:06:44', 466, '2014-05-23 14:09:03', 466, '0000-00-00 00:00:00', 0);
-- Dumping structure for table prefix_virtuemart_products
CREATE TABLE IF NOT EXISTS `prefix_virtuemart_products` (
`virtuemart_product_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`virtuemart_vendor_id` smallint(1) unsigned NOT NULL DEFAULT '1',
`product_parent_id` int(1) unsigned NOT NULL DEFAULT '0',
`product_sku` char(64) DEFAULT NULL,
`product_gtin` char(64) DEFAULT NULL,
`product_mpn` char(64) DEFAULT NULL,
`product_weight` decimal(10,4) DEFAULT NULL,
`product_weight_uom` char(7) DEFAULT NULL,
`product_length` decimal(10,4) DEFAULT NULL,
`product_width` decimal(10,4) DEFAULT NULL,
`product_height` decimal(10,4) DEFAULT NULL,
`product_lwh_uom` char(7) DEFAULT NULL,
`product_url` char(255) DEFAULT NULL,
`product_in_stock` int(1) NOT NULL DEFAULT '0',
`product_ordered` int(1) NOT NULL DEFAULT '0',
`low_stock_notification` int(1) unsigned NOT NULL DEFAULT '0',
`product_available_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`product_availability` char(32) DEFAULT NULL,
`product_special` tinyint(1) DEFAULT NULL,
`product_sales` int(1) unsigned NOT NULL DEFAULT '0',
`product_unit` varchar(8) DEFAULT NULL,
`product_packaging` decimal(8,4) unsigned DEFAULT NULL,
`product_params` varchar(2000) DEFAULT NULL,
`hits` int(11) unsigned DEFAULT NULL,
`intnotes` varchar(18000) DEFAULT NULL,
`metarobot` varchar(400) DEFAULT NULL,
`metaauthor` varchar(400) DEFAULT NULL,
`layout` char(16) DEFAULT NULL,
`published` tinyint(1) DEFAULT NULL,
`pordering` mediumint(2) unsigned NOT NULL DEFAULT '0',
`created_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` int(11) NOT NULL DEFAULT '0',
`modified_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`modified_by` int(11) NOT NULL DEFAULT '0',
`locked_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`locked_by` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`virtuemart_product_id`),
KEY `idx_product_virtuemart_vendor_id` (`virtuemart_vendor_id`),
KEY `idx_product_product_parent_id` (`product_parent_id`),
KEY `i_product_special` (`product_special`),
KEY `i_published` (`published`),
KEY `i_pordering` (`pordering`)
) ENGINE=MyISAM AUTO_INCREMENT=3809 DEFAULT CHARSET=utf8 COMMENT='All products are stored here.';
-- Dumping data for table kontosdb.prefix_virtuemart_products: 960 rows
/*!40000 ALTER TABLE `prefix_virtuemart_products` DISABLE KEYS */;
INSERT INTO `prefix_virtuemart_products` (`virtuemart_product_id`, `virtuemart_vendor_id`, `product_parent_id`, `product_sku`, `product_gtin`, `product_mpn`, `product_weight`, `product_weight_uom`, `product_length`, `product_width`, `product_height`, `product_lwh_uom`, `product_url`, `product_in_stock`, `product_ordered`, `low_stock_notification`, `product_available_date`, `product_availability`, `product_special`, `product_sales`, `product_unit`, `product_packaging`, `product_params`, `hits`, `intnotes`, `metarobot`, `metaauthor`, `layout`, `published`, `pordering`, `created_on`, `created_by`, `modified_on`, `modified_by`, `locked_on`, `locked_by`) VALUES
(2849, 1, 0, '0100300', '', '', 0.0000, 'KG', 0.0000, 0.0000, 0.0000, 'CM', '', 0, 0, 0, '2010-05-17 00:00:00', '', 0, 0, 'KG', NULL, 'min_order_level=""|max_order_level=""|step_order_level=""|product_box=""|', NULL, '', '', '', '0', 1, 0, '2010-05-17 12:46:52', 466, '2014-05-16 06:35:16', 466, '0000-00-00 00:00:00', 0),
(2850, 1, 0, '0100623', NULL, NULL, 0.0000, 'KG', 0.0000, 0.0000, 0.0000, 'CM', '', 0, 0, 0, '2010-05-28 00:00:00', '', 0, 0, 'Τεμάχιο', NULL, 'min_order_level=null|max_order_level=null|step_order_level=null|product_box=null|', NULL, '', '', '', '', 1, 0, '2010-05-28 12:40:19', 466, '2014-05-10 06:06:44', 466, '0000-00-00 00:00:00', 0),
(2851, 1, 0, '0100630', NULL, NULL, 0.0000, 'KG', 0.0000, 0.0000, 0.0000, 'CM', '', 0, 0, 0, '2010-05-28 00:00:00', '', 0, 0, 'Τεμάχιο', NULL, 'min_order_level=null|max_order_level=null|step_order_level=null|product_box=null|', NULL, '', '', '', '', 1, 0, '2010-05-28 12:47:16', 466, '2014-05-10 06:06:44', 466, '0000-00-00 00:00:00', 0);
I'm not sure I understand what you are having trouble with. With the following give you the results you expect?
SELECT
nprefix_virtuemart_products.virtuemart_product_id,
pvm.virtuemart_media_id,
5
FROM
prefix_virtuemart_products pvp
INNER JOIN prefix_virtuemart_medias pvm ON pvm.file_url LIKE CONCAT('%', pvp.product_sku, ' a.jpg')
OR pvm.file_url LIKE CONCAT('%', pvp.product_sku, ' A.jpg');
For this to work the subquery can only return one row, I don't know what your data looks like, so that might not be the case.