I am using WITH RECURSIVE using UNION ALL mysql query to get level of each node and it is working fine. Now I just want to get count of childs for each node.
WITH RECURSIVE generation AS
(SELECT id, name, parent_id, 0 AS generation_number FROM users WHERE parent_id IS NULL
UNION ALL
SELECT
child.name,
child.parent_id
generation_number+1 AS generation_number
FROM users child
JOIN generation g ON g.id = child.parent_id )
SELECT id, name, parent_id, generation_number FROM generation;
Above MySql query returning below table as result excepting last column i.e. "Count of child". What need to do in same query to get count of (Union All) childs.
id
name
parent_id
generation/level
Count of child
1
A
0
0
2
2
B
1
1
1
3
C
1
1
0
4
D
2
2
2
5
E
4
3
0
6
F
4
3
0
Here is source data for creating table
CREATE TABLE `users` (
`id` bigint(20) UNSIGNED NOT NULL,
`sponsor_id` bigint(20) DEFAULT NULL,
`parent_id` bigint(20) DEFAULT NULL,
`user_id` bigint(20) DEFAULT NULL,
`name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`phone` varchar(15) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`image` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`status` enum('Active','Inactive','Deleted') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'Active',
`amount` decimal(8,2) NOT NULL DEFAULT 0.00,
`created_at` timestamp NOT NULL DEFAULT current_timestamp(),
`updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`id`, `sponsor_id`, `parent_id`, `user_id`, `name`, `email`, `phone`, `password`, `image`, `status`, `amount`, `created_at`, `updated_at`) VALUES
(1, NULL, NULL, NULL, 'a', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:42:09', '2022-10-12 04:42:09'),
(2, NULL, 1, NULL, 'b', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:43:34', '2022-10-12 04:43:34'),
(3, NULL, 1, NULL, 'c', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:43:34', '2022-10-12 04:43:34'),
(4, NULL, 1, NULL, 'e', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:47:31', '2022-10-12 04:47:31'),
(5, NULL, 2, NULL, 'f', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:47:31', '2022-10-12 04:47:31'),
(6, NULL, 2, NULL, 'g', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:47:31', '2022-10-12 04:47:31'),
(7, NULL, 2, NULL, 'h', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:47:31', '2022-10-12 04:47:31'),
(8, NULL, 3, NULL, 'i', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:47:31', '2022-10-12 04:47:31'),
(9, NULL, 3, NULL, 'j', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:47:31', '2022-10-12 04:47:31'),
(10, NULL, 3, NULL, 'k', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:47:31', '2022-10-12 04:47:31'),
(11, NULL, 4, NULL, 'l', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(12, NULL, 4, NULL, 'm', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(13, NULL, 4, NULL, 'n', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(14, NULL, 5, NULL, 'o', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(15, NULL, 5, NULL, 'p', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(16, NULL, 6, NULL, 'q', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(17, NULL, 6, NULL, 'r', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(18, NULL, 7, NULL, 's', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(19, NULL, 8, NULL, 't', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(20, NULL, 8, NULL, 'u', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(21, NULL, 8, NULL, 'v', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(22, NULL, 9, NULL, 'w', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(23, NULL, 9, NULL, 'x', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(24, NULL, 10, NULL, 'y', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(25, NULL, 10, NULL, 'z', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23');
I think you can simply use COUNT and JOIN.
SELECT g1.id, g1.name, g1.parent_id, g1.generation_number,COALESCE(g2.CountOfChild,0)
FROM generation g1 LEFT JOIN
(SELECT parent_id,COUNT(*) CountOfChild FROM generation GROUP BY parent_id) g2
ON g1.id=g2.parent_id;
Related
My target is if find the update to a field the trigger will insert a row to same table with new value.
My Code :
DROP TRIGGER `premier`.`create_neworg`;
CREATE TRIGGER `premier`.`create_neworg` AFTER UPDATE
ON premier.org_infor FOR EACH ROW
IF (NEW.org_customerid IS NOT NUll) THEN
INSERT INTO premier.org_infor (org_ParentID,org_emcode,org_date,org_contents) VALUES( NEW.org_customerid,OLD.org_emcode,NEW.org_modify,NEW.org_contents);
END IF;
CREATE TABLE `org_infor` (
`org_id` int(11) NOT NULL,
`org_ParentID` varchar(50) DEFAULT NULL,
`org_emcode` varchar(50) DEFAULT NULL,
`org_customerid` varchar(50) DEFAULT NULL,
`org_date` date DEFAULT NULL,
`org_contents` varchar(200) CHARACTER SET utf8 COLLATE utf8_vietnamese_ci DEFAULT NULL,
`org_workingtype` varchar(80) CHARACTER SET utf8 COLLATE utf8_vietnamese_ci DEFAULT NULL,
`org_dateend` date DEFAULT NULL,
`org_status` int(11) NOT NULL DEFAULT 0,
`org_modify` datetime NOT NULL DEFAULT current_timestamp(),
`org_username` varchar(50) DEFAULT NULL,
`org_reasonchange` varchar(300) CHARACTER SET utf8 COLLATE utf8_vietnamese_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `org_infor` (`org_id`, `org_ParentID`, `org_emcode`, `org_customerid`, `org_date`, `org_contents`, `org_workingtype`, `org_dateend`, `org_status`, `org_modify`, `org_username`, `org_reasonchange`) VALUES
(31, '1-10', '4174', NULL, '2022-11-30', 'Upload from system', '3 Ca', NULL, 0, '2022-11-30 20:21:33', NULL, NULL),
(32, '1-10', '4185', NULL, '2022-11-30', 'Upload from system', 'Ca1 ,Ca2', NULL, 0, '2022-11-30 20:21:33', NULL, NULL),
(34, '1-10', '4513', NULL, '2022-11-30', 'Upload from system', NULL, NULL, 0, '2022-11-30 20:21:33', NULL, NULL),
(35, '1-10', '4859', NULL, '2022-11-30', 'Upload from system', NULL, NULL, 0, '2022-11-30 20:21:33', NULL, NULL),
(36, '1-10', '4966', NULL, '2022-11-30', 'Upload from system1', NULL, NULL, 0, '2022-11-30 20:21:33', NULL, NULL),
(37, '1-10', '4989', NULL, '2022-11-30', 'Upload from system', NULL, NULL, 0, '2022-11-30 20:21:33', NULL, NULL),
(38, '1-10', '5091', NULL, '2022-11-30', 'Upload from system', NULL, NULL, 0, '2022-11-30 20:21:33', NULL, NULL),
(39, '1-10', '5100', NULL, '2022-11-30', 'Upload from system', NULL, NULL, 0, '2022-11-30 20:21:33', NULL, NULL),
(40, '1-10', '5320', NULL, '2022-11-30', 'Upload from system', NULL, NULL, 0, '2022-11-30 20:21:33', NULL, NULL),
(41, '1-10', '5386', NULL, '2022-11-30', 'Upload from system', NULL, NULL, 0, '2022-11-30 20:21:33', NULL, NULL),
(42, '1-10', '5405', NULL, '2022-11-30', 'Upload from system', NULL, NULL, 0, '2022-11-30 20:21:33', NULL, NULL),
(43, '1-10', '5503', NULL, '2022-11-30', 'Upload from system', NULL, NULL, 0, '2022-11-30 20:21:33', NULL, NULL),
(44, '1-10', '5526', NULL, '2022-11-30', 'Upload from system', NULL, NULL, 0, '2022-11-30 20:21:33', NULL, NULL),
(45, '1-10', '5759', NULL, '2022-11-30', 'Upload from system', NULL, NULL, 0, '2022-11-30 20:21:33', NULL, NULL),
(46, '1-10', '5824', NULL, '2022-11-30', 'Upload from system', NULL, NULL, 0, '2022-11-30 20:21:33', NULL, NULL),
(47, '1-10', '5877', NULL, '2022-11-30', 'Upload from system', NULL, NULL, 0, '2022-11-30 20:21:33', NULL, NULL),
(48, '1-10', '5895', NULL, '2022-11-30', 'Upload from system', NULL, NULL, 0, '2022-11-30 20:21:33', NULL, NULL),
(49, '1-10', '5990', NULL, '2022-11-30', 'Upload from system', NULL, NULL, 0, '2022-11-30 20:21:33', NULL, NULL),
(50, '1-10', '6305', NULL, '2022-11-30', 'Upload from system', NULL, NULL, 0, '2022-11-30 20:21:33', NULL, NULL),
(51, '1-10', '6579', NULL, '2022-11-30', 'Upload from system', NULL, NULL, 0, '2022-11-30 20:21:33', NULL, NULL),
(52, '1-10', '6602', NULL, '2022-11-30', 'Upload from system', NULL, NULL, 0, '2022-11-30 20:21:33', NULL, NULL),
(53, '1-10', '6684', NULL, '2022-11-30', 'Upload from system', NULL, NULL, 0, '2022-11-30 20:21:33', NULL, NULL)
but I Tested when update a new value to org_customerid the system error
Screen error:
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')
I am having strange results in mysql. I have a view which when executed returns 0 rows but taking the query out of the VIEW and executing it as it is returns 63 rows.
This was working properly but ever since I have migrated the code to AWS RDS I am having this error. I am not able to make any sense why this is happening?
I don't even get any error just the 0 rows being returned.
From here on I am just writing random words because I am not able to post the question as the line of code is more. THis is really annnoying ahh darn I made a typo it must be annoying and not annnoying. My bad I hope you all arent mad at this, I am at my wits end how much more should I type?
I am unable to create a sqlfiddle as the request is too large, I am creating a fiddle like dump if you guys want to recreate it on on your end.
I have also discovered that removing the left join from the VIEW returns results.
The fiddle like dump below returns 3 rows when running the select but the VIEW returns 0 rows
CREATE TABLE `users` (
`id` int(11) NOT NULL,
`user_type` enum('admin','landlord','renter') NOT NULL,
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`email_id` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`company_name` varchar(255) NOT NULL,
`street_address` varchar(255) NOT NULL,
`city` varchar(100) NOT NULL,
`state` varchar(100) NOT NULL,
`zip` varchar(100) NOT NULL,
`phone_no` varchar(50) NOT NULL,
`bank_name` varchar(100) NOT NULL,
`billing_address` varchar(255) NOT NULL,
`name_on_card` varchar(255) NOT NULL,
`bank_account_number` varchar(100) NOT NULL,
`bank_routing_no` varchar(100) NOT NULL,
`has_payzang_account` int(11) NOT NULL DEFAULT '0',
`payzang_api` varchar(255) DEFAULT '',
`payzang_gateway_url` varchar(255) NOT NULL,
`pending_amount` decimal(18,2) DEFAULT NULL COMMENT 'pending key bank(rent deduct) amount for landlord',
`is_active` int(11) NOT NULL COMMENT '0-inactive/1-active',
`is_assigned` int(11) NOT NULL DEFAULT '0' COMMENT 'Is this user assigned to any property',
`assigned_to` int(11) NOT NULL COMMENT 'assigned to which landlord id',
`is_set_up_new_password` enum('0','1') NOT NULL DEFAULT '0' COMMENT 'When admin/landlord adds renter or admin adds landlord an email with id+password is sent to the user. The first time they sign in, it should prompt them to change password page. So this field indicating whether user set up their own password',
`created_at` datetime NOT NULL,
`modified_at` datetime NOT NULL,
`created_by` int(11) NOT NULL,
`modified_by` int(11) NOT NULL,
`is_deleted` int(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `users` (`id`, `user_type`, `first_name`, `last_name`, `email_id`, `password`, `company_name`, `street_address`, `city`, `state`, `zip`, `phone_no`, `bank_name`, `billing_address`, `name_on_card`, `bank_account_number`, `bank_routing_no`, `has_payzang_account`, `payzang_api`, `payzang_gateway_url`, `pending_amount`, `is_active`, `is_assigned`, `assigned_to`, `is_set_up_new_password`, `created_at`, `modified_at`, `created_by`, `modified_by`, `is_deleted`) VALUES
(1, 'admin', 'Super', 'Admin', 'email#email.com', '$2y$10$NwcR0ObYeUM.OfjgMOaajeH6bjjuMTpU2GnF0h623SER37AFk8K9i', 'UIGJ', 'DJ Road', 'Kolkatr', '20', '522236', '(987) 963-9658', '', '', '', '', '', 1, 'xxxxxxxxxxxxxxxxxxxxx', 'https://payzang.transactiongateway.com/api/v2/three-step', '0.00', 1, 0, 0, '1', '2017-06-02 13:56:34', '2017-06-02 13:56:34', 0, 0, 0),
(2, 'landlord', 'Denise', 'Supplee', 'email1#email.com', '$2y$10$PATxnED1/lqr1t7epLJiwu0uVztuXJB97q/XQv7xLc/iCv/m4NK2G', '', '', '', '', '', '2156755615', '', '', '', '', '', 1, 'xxxxxxxxxxxxxxxxxxxxx', 'https://payzang.transactiongateway.com/api/v2/three-step', '230.77', 1, 0, 0, '0', '2017-09-06 16:31:23', '2017-09-10 05:48:45', 0, 1, 0),
(3, 'renter', 'Amanda', 'Scott', 'email2#email.com', '$2y$10$oBU3tbcVT63Asw6CDLCGB.SNAvSpJaOcLBx4NqodFNMwvlSRLc.tO', '', '', '', '', '', '(215) 672-3778', '', '', '', '', '', 0, '', '', NULL, 0, 0, 2, '1', '2017-09-06 17:54:02', '2017-09-06 17:54:02', 2, 0, 1),
(4, 'renter', 'Megan', 'Ridgell', 'email3#email.com', '$2y$10$mPR7U/ri/rpW2n5f6X4mV.6jCycvLTJ/dvttR8seMFSGgRR/tQh1.', '', '', '', '', '', '2154852222', '', '', '', '', '', 0, '', '', NULL, 0, 0, 11, '1', '2017-09-06 18:30:01', '2017-09-12 06:22:23', 0, 2, 1),
(5, 'landlord', 'Brian', 'Davis', 'email4#email.com', '$2y$10$NwcR0ObYeUM.OfjgMOaajeH6bjjuMTpU2GnF0h623SER37AFk8K9i', '', 'PO Box 123', 'Baltimore', '21', '21231', '4104999026', '', '', '', '', '', 0, '', '', '0.00', 1, 0, 0, '0', '2017-09-07 05:19:46', '2017-09-07 05:19:46', 0, 0, 0),
(6, 'renter', 'Greg', 'Davis', 'email5#email.com', '$2y$10$Bd6S4KMGec2NzqKcvsJ1huwmbfACu8oOn56JLFSVQFt6ANg0Vr.ZW', '', '', '', '', '', '4104999026', '', '', '', '', '', 0, '', '', NULL, 1, 0, 5, '1', '2017-09-07 05:30:58', '2017-09-17 04:22:30', 0, 2, 0),
(7, 'renter', 'Maddie', 'Evans', 'email6#email.com', '$2y$10$ry9DBBFJHRhCsV7wpU9s9OI9WQ8BWqCTdk0K9KyYw/VD10mb/yC3O', '', '', '', '', '', '4104999026', '', '', '', '', '', 0, '', '', NULL, 1, 0, 0, '0', '2017-09-07 06:07:10', '2017-09-17 04:14:48', 0, 5, 0),
(8, 'renter', 'Arijita', 'Dey', 'email7#email.com', '$2y$10$NwcR0ObYeUM.OfjgMOaajeH6bjjuMTpU2GnF0h623SER37AFk8K9i', '', '', '', '', '', '3433453535', '', '', '', '', '', 0, '', '', NULL, 0, 0, 2, '0', '2017-09-07 08:08:21', '2017-09-10 05:42:58', 0, 2, 1),
(9, 'renter', 'Arijita', 'Dey', 'email8#email.com', '$2y$10$NwcR0ObYeUM.OfjgMOaajeH6bjjuMTpU2GnF0h623SER37AFk8K9i', '', '', '', '', '', '(564) 564-5645', '', '', '', '', '', 0, '', '', NULL, 0, 0, 5, '0', '2017-09-07 11:05:33', '2017-09-07 11:05:33', 5, 0, 1),
(10, 'renter', 'Rick', 'Roy', 'email9#email.com', '$2y$10$PG/8SkAwOZsSkD.px9FGveFIbriavOTX6vdBWHHmdjQeKgbwr0/wu', '', '', '', '', '', '(533) 453-4534', '', '', '', '', '', 0, '', '', NULL, 0, 0, 5, '0', '2017-09-07 11:11:49', '2017-09-07 11:11:49', 5, 0, 1),
(11, 'landlord', 'Denise', 'Supplee', 'email0#email.com', '$2y$10$52H0QZMFK9ApK7JwaDV9qOlqyOxcyTH3QikQqXgAcLD.zrb.ggoQG', '', '', '', '', '', '2156755615', '', '', '', '', '', 1, 'xxxxxxxxxxxxxxxxxxxxx', 'https://payzang.transactiongateway.com/api/v2/three-step', '0.00', 1, 0, 0, '0', '2017-09-11 14:09:03', '2017-09-18 23:22:57', 0, 1, 0);
CREATE TABLE `lease_with_max_id` (
`id` int(11)
,`lease_id` int(11)
,`renter_id` int(11)
,`landlord_id` int(11)
,`property_id` int(11)
,`is_rent_deducted_form_filled_up` enum('0','1')
,`status` int(11)
,`is_deleted` enum('0','1')
,`is_lease_ended` enum('0','1')
);
CREATE TABLE `properties` (
`id` int(11) NOT NULL,
`user_id` int(11) NOT NULL COMMENT 'landlord''s id',
`property_name` varchar(255) NOT NULL,
`street_address` varchar(255) NOT NULL,
`street_address_2` varchar(255) DEFAULT NULL,
`city` varchar(100) NOT NULL,
`state` varchar(100) NOT NULL,
`zip` varchar(100) NOT NULL,
`rent_amount` decimal(18,2) DEFAULT '0.00',
`is_active` enum('0','1') NOT NULL,
`is_assigned` enum('0','1') NOT NULL DEFAULT '0' COMMENT 'Is assigned to some renter or not',
`created_at` datetime NOT NULL,
`modified_at` datetime NOT NULL,
`is_deleted` int(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `properties` (`id`, `user_id`, `property_name`, `street_address`, `street_address_2`, `city`, `state`, `zip`, `rent_amount`, `is_active`, `is_assigned`, `created_at`, `modified_at`, `is_deleted`) VALUES
(1, 2, 'Test Property', '330 Crooked Billet Rd', '', 'Hatboro', '39', '19040', '0.00', '0', '0', '2017-09-10 05:38:12', '2017-09-10 05:38:12', 0),
(2, 2, '', '515 Main St', 'Apt. 1809', 'Horsham', '39', '19044', '0.00', '1', '1', '2017-10-09 04:24:50', '2017-10-09 04:24:50', 1),
(3, 5, 'Test 1', '123 Brian\'s Test St.', 'Apt. 2', 'Baltimore', '21', '21231', '0.00', '1', '0', '2017-09-07 05:21:10', '2017-09-07 05:21:10', 1),
(4, 5, 'Test 1', '123 Brian\'s Test St.', 'Apt. 2', 'Baltimore', '21', '21231', '0.00', '1', '0', '2017-09-07 05:21:18', '2017-09-07 05:21:18', 1),
(5, 5, 'Test 1', '123 Brian\'s Test St.', 'Apt. 1503', 'Baltimore', '21', '21231', '0.00', '0', '0', '2017-09-11 13:49:11', '2017-09-11 13:49:11', 1),
(6, 5, 'Test 2', '123 Brian\'s Test St.', 'Apt. 1', 'Baltimore', '21', '21231', '0.00', '1', '1', '2017-10-03 04:31:18', '2017-10-03 04:31:18', 0);
CREATE TABLE `geo_states` (
`id` int(11) NOT NULL,
`name` varchar(50) NOT NULL DEFAULT '',
`abv` char(2) NOT NULL DEFAULT '',
`country` char(2) NOT NULL,
`is_state` char(1) DEFAULT NULL,
`is_lower48` char(1) DEFAULT NULL,
`slug` varchar(50) NOT NULL,
`latitude` float(9,6) DEFAULT NULL,
`longitude` float(9,6) DEFAULT NULL,
`population` bigint(20) UNSIGNED DEFAULT NULL,
`area` float(8,2) UNSIGNED DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE VIEW `test` AS SELECT
`users`.`id` AS `id`,
`users`.`user_type` AS `user_type`,
`users`.`first_name` AS `first_name`,
`users`.`last_name` AS `last_name`,
CONCAT(
`users`.`first_name`,
' ',
`users`.`last_name`
) AS `landlord_name`,
`users`.`email_id` AS `email_id`,
`users`.`password` AS `password`,
`users`.`company_name` AS `company_name`,
`users`.`street_address` AS `street_address`,
`users`.`city` AS `city`,
`users`.`state` AS `state`,
`users`.`zip` AS `zip`,
`users`.`phone_no` AS `phone_no`,
`users`.`bank_name` AS `bank_name`,
`users`.`billing_address` AS `billing_address`,
`users`.`name_on_card` AS `name_on_card`,
`users`.`bank_account_number` AS `bank_account_number`,
`users`.`bank_routing_no` AS `bank_routing_no`,
`users`.`has_payzang_account` AS `has_payzang_account`,
(
CASE WHEN(`users`.`has_payzang_account` = 0) THEN 'No' WHEN(`users`.`has_payzang_account` = 1) THEN 'Yes'
END
) AS `has_payzang_account_custom`,
`users`.`payzang_api` AS `payzang_api`,
`users`.`payzang_gateway_url` AS `payzang_gateway_url`,
`users`.`pending_amount` AS `pending_amount`,
`users`.`is_active` AS `is_active`,
(
CASE WHEN(`users`.`is_active` = '0') THEN 'Inactive' WHEN(`users`.`is_active` = '1') THEN 'Active'
END
) AS `is_active_custom`,
`users`.`is_assigned` AS `is_assigned`,
`users`.`assigned_to` AS `assigned_to`,
`users`.`is_set_up_new_password` AS `is_set_up_new_password`,
`users`.`created_at` AS `created_at`,
`users`.`modified_at` AS `modified_at`,
`users`.`created_by` AS `created_by`,
`users`.`modified_by` AS `modified_by`,
`users`.`is_deleted` AS `is_deleted`,
`lease_with_max_id`.`id` AS `lease_with_max_id_id`,
`lease_with_max_id`.`lease_id` AS `lease_with_max_id_lease_id`,
`lease_with_max_id`.`renter_id` AS `lease_with_max_id_renter_id`,
`lease_with_max_id`.`landlord_id` AS `lease_with_max_id_landlord_id`,
`lease_with_max_id`.`property_id` AS `lease_with_max_id_property_id`,
`lease_with_max_id`.`is_rent_deducted_form_filled_up` AS `lease_with_max_id_is_rent_deducted_form_filled_up`,
`lease_with_max_id`.`status` AS `lease_with_max_id_status`,
`lease_with_max_id`.`is_deleted` AS `lease_with_max_id_is_deleted`,
`properties`.`id` AS `property_id`,
`properties`.`user_id` AS `property_user_id`,
`properties`.`property_name` AS `property_property_name`,
`properties`.`street_address` AS `property_street_address`,
`properties`.`street_address_2` AS `property_street_address_2`,
`properties`.`city` AS `property_city`,
`properties`.`state` AS `property_state`,
`properties`.`zip` AS `property_zip`,
`properties`.`rent_amount` AS `property_rent_amount`,
`properties`.`is_active` AS `property_is_active`,
`properties`.`is_assigned` AS `property_is_assigned`,
`properties`.`created_at` AS `property_created_at`,
`properties`.`modified_at` AS `property_modified_at`,
`properties`.`is_deleted` AS `property_is_deleted`,
`geo_states`.`name` AS `geo_states_name`
FROM
(
(
(
`users`
LEFT JOIN `lease_with_max_id` ON
(
(
`lease_with_max_id`.`renter_id` = `users`.`id`
)
)
)
LEFT JOIN `properties` ON
(
(
`properties`.`id` = `lease_with_max_id`.`property_id`
)
)
)
LEFT JOIN `geo_states` ON
(
(
`geo_states`.`id` = `properties`.`state`
)
)
)
WHERE
(
(`users`.`user_type` = 'landlord') AND(`users`.`is_deleted` = 0)
)
ORDER BY
`users`.`id`
DESC
;
My query was working in MYSQL but not in MYSQLI.
I'm selecting students that have done the pre AND post test.
SELECT
studid as Username,
prepoints as 'Fitness Assessment Points Grade',
end AS 'End-of-Line Indicator'
FROM
fittest, points
WHERE
YEAR(submitted) = '2017'
AND semester = 'summer2'
GROUP BY studid
HAVING
(MAX(prepost = 'pre' ) + MAX(prepost = 'post')) = 2 AND COUNT(DISTINCT prepost) = 2
Any ideas what changes need to be made to get it working again?
Here is some sample data:
CREATE TABLE `fittest` ( `id` int(11) NOT NULL, `submitted`
datetime DEFAULT NULL, `studid` varchar(100) DEFAULT NULL,
`semester` varchar(50) DEFAULT NULL, `instructor` varchar(30)
DEFAULT NULL, `course` enum('PHED 1164') DEFAULT NULL, `section`
enum('5001','5003','5005','5007','5009','5011','5013','5015','5017','5019','5021','5023','5025','5027','5029','5031','5033','5035','5037','5039','5041','5043','5045','5047','5049','5051','5053','5055','5057','5059','5061','5063','5065','5067','5069')
DEFAULT NULL, `age` varchar(50) DEFAULT NULL, `gender`
enum('m','f') DEFAULT NULL, `ethnicity` enum('Hispanic','African
American','Asian','White-Non Hispanic','Other') DEFAULT NULL,
`height` char(4) DEFAULT NULL, `weight` int(3) DEFAULT NULL,
`flexibility` int(2) DEFAULT NULL, `crunches` int(3) DEFAULT NULL,
`pushups` int(3) DEFAULT NULL, `treadtimemin` int(2) DEFAULT NULL,
`treadtimesec` int(2) NOT NULL, `treadhr` int(3) DEFAULT NULL,
`prepost` enum('pre','post') NOT NULL, `end` char(1) NOT NULL
DEFAULT '#' ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `fittest` (`id`, `submitted`, `studid`, `semester`,
`instructor`, `course`, `section`, `age`, `gender`, `ethnicity`,
`height`, `weight`, `flexibility`, `crunches`, `pushups`,
`treadtimemin`, `treadtimesec`, `treadhr`, `prepost`, `end`) VALUES
(17, '2017-01-02 21:55:33', 'slacker', 'spring', 'Tim', 'PHED 1164',
'5001', '32', 'm', NULL, '69.5', 155, NULL, 29, 34, 22, 15, 76, 'pre',
'#'), (16, '2017-01-02 21:31:34', 'bfun', 'spring', 'Tim', 'PHED
1164', '5001', '32', 'm', NULL, '69.5', 122, NULL, 37, 36, 18, 14, 76,
'post', '#'), (15, '2017-01-02 21:31:09', 'bfun', 'spring', 'Tim',
'PHED 1164', '5001', '32', 'm', NULL, '69.5', 129, NULL, 21, 20, 23,
14, 76, 'pre', '#'),
I just figured out the issue isn't with the query. It was with the input. All the input had post for the prepost value so there were no results that were showing. Thanks for your help.
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.