Related
I want to get past 30 days' records in SQL using the timestamp column, not the date column my query
SELECT sum(gasFeeInUSD) as total, STR_TO_DATE(created, '%Y-%m-%d') as t_date
FROM `transactions`
WHERE STR_TO_DATE(created, '%Y-%m-%d') >= CURRENT_DATE - INTERVAL 30 DAY AND CURDATE()
GROUP BY DATE(STR_TO_DATE(created, '%Y-%m-%d'))
This query works fine with created column but I want to use it with the timestamp column.
Here is DB structure
CREATE TABLE IF NOT EXISTS `transactions` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`transferType` varchar(256) DEFAULT NULL,
`from` varchar(256) DEFAULT NULL,
`to` varchar(256) DEFAULT NULL,
`sendFrom` varchar(256) DEFAULT NULL,
`sendTo` varchar(256) DEFAULT NULL,
`amount` float DEFAULT NULL,
`currency` varchar(256) DEFAULT NULL,
`id_` varchar(256) DEFAULT NULL,
`timestamp` varchar(256) DEFAULT NULL,
`othersNumioId` varchar(256) DEFAULT NULL,
`othersName` varchar(256) DEFAULT NULL,
`message` varchar(256) DEFAULT NULL,
`transactionHash` varchar(256) DEFAULT NULL,
`totalTokenAmountInUSD` varchar(256) DEFAULT NULL,
`gasFeeInUSD` varchar(256) DEFAULT NULL,
`filterId` varchar(256) DEFAULT NULL,
`status` varchar(256) DEFAULT NULL,
`walletNo` varchar(256) DEFAULT NULL,
`import_id` int(11) DEFAULT NULL,
`day` varchar(256) DEFAULT NULL,
`date` varchar(256) DEFAULT NULL,
`month` varchar(256) DEFAULT NULL,
`year` varchar(256) DEFAULT NULL,
`created` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`modified` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
COMMIT;
-- Dumping data for table `transactions`
INSERT INTO `transactions` (`id`, `transferType`, `from`, `to`, `sendFrom`, `sendTo`, `amount`, `currency`, `id_`, `timestamp`, `othersNumioId`, `othersName`, `message`, `transactionHash`, `totalTokenAmountInUSD`, `gasFeeInUSD`, `filterId`, `status`, `walletNo`, `import_id`, `day`, `date`, `month`, `year`, `created`, `modified`) VALUES
(1, 'Send', '0x11F83064t5C59eBCDE1842FE28EB0ee6E77cbD3E', '0xFEe0F95a9AD7918A8trtc8F526d7a5315060050', 'Standard', 'Standard', 0.002263, 'ETH', '609212edbbe53e0019c78e42', '1621743092.497', 'timios1', 'Tim Allard', 'Hey', '0x8434d39cbe7db1afd95arta718881bbd78ccdf8684173a523b7ecd0afe3926e', '5.20462844', '1.69', '_1xdnjc6ja', 'Success', 't', 359, 'sun', '23', 'may', '2021', '2021-05-23 20:12:40', '2021-05-23 20:12:40'),
(2, 'Send', '0x22D227caCd975gAF0E326471976A818ef00F97889', '0x8b429171e521F2c83d6r4dAfe402fdB17eF3b3397B', 'Standard', 'Standard', 0.01, 'ETH', '609acbacde0cad0012e9afa7', '1621732567.403', 'timios', 'Tim Allard', 'Test from android how long can I make this message before it messes up the app?', '0xd1da2ef9e6ce3974746a40f242e22749fc4d27a1ed4e59340446da7ccf37e4b0', '23.5907', '1.68', '_dmzg4yswb', 'Success', 't', 358, 'sun', '23', 'may', '2021', '2021-05-23 20:12:40', '2021-05-23 20:12:40'),
(3, 'Internal Transfer', 'Numio Wallet', 'Standard Wallet', 'Numio', 'Standard', 10, 'USDC', '60a92c9d11afcse1200b257', '1621700163.749', NULL, NULL, NULL, 'sync-tx:e2878a8c3fad61057bdsd3e658d8e63f7b826e0d1b4c1d43b9ed113194367749', '9.991', '0.017', '_y2tn0rzjy', 'Success', NULL, 355, 'sat', '22', 'may', '2021', '2021-05-23 20:12:40', '2021-05-23 20:12:40'),
(4, 'Internal Transfer', 'Standard Wallet', 'Numio Wallet', 'Standard', 'Numio', 0.1, 'ETH', '60a92c9d11afdsdc0001200b257', '1621700005.226', NULL, NULL, NULL, '0x4b5350ddac9d6a1364986e49aa96522dsd321e1c42a64f38a765078add239dbc9af', '234.379', '7.83', '_tudolue4d', 'Success', NULL, 354, 'sat', '22', 'may', '2021', '2021-05-23 20:12:40', '2021-05-23 20:12:40'),
(5, 'Internal Transfer', 'Standard Wallet', 'Numio Wallet', 'Standard', 'Numio', 100, 'USDC', '60a92c9d11afdc0001200b257', '1621699943.112', NULL, NULL, NULL, '0xae27a438390esdd11f936a49dsbc1bf58a44b53cc00b27d8c84cfe0cb1ce2479c3276', '99.91', '21.82', '_vzvcawog0', 'Success', NULL, 353, 'sat', '22', 'may', '2021', '2021-05-23 20:12:40', '2021-05-23 20:12:40');
COMMIT;
If what I'm guessing is correct, your varchar(256) timestamp stores a Unix Timestamp in milliseconds, you need only convert it after dividing it by 10000, like so:
WHERE FROM_UNIXTIME(timestamp / 1000) >= CURRENT_DATE - INTERVAL 30 DAY
Or you could convert the right part of the equation:
WHERE (timestamp / 1000) >= UNIX_TIMESTAMP(CURRENT_DATE - INTERVAL 30 DAY)
Plus I think you may want to switch CURRENT_DATE to CURRENT_TIME
Your timestamp is looks like a type of Unix timestamp measured in milliseconds since 1970-01-01.
If you want exactly 30 days before the timestamp, just use arithmetic:
where timestamp < (unix_timestamp() * 1000) - (24 * 60 * 60 * 1000)
http://sqlfiddle.com/#!9/708aef/8/0
I have an table called mmsusbribers where i store information from users.
Then i have 3 other tables: driftinfo_subscriber_operator_lookup, driftinfo_subscriber_reseller_lookup and driftinfo_subscriber_server_lookup.
I want to join the tables together all of them in one select.
Tables look like this:
CREATE TABLE IF NOT EXISTS `mmsubscribers` (
`id` int(11) NOT NULL,
`email` varchar(50) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
`hash` varchar(100) DEFAULT NULL,
`status` varchar(50) DEFAULT NULL,
`listid` int(11) DEFAULT NULL,
`phone` varchar(50) DEFAULT NULL,
`login` varchar(50) DEFAULT NULL,
`pass` varchar(50) DEFAULT NULL,
`language` varchar(50) DEFAULT NULL,
`orgname` varchar(75) DEFAULT NULL,
`orgadmin` int(11) DEFAULT NULL,
`domain` varchar(150) DEFAULT NULL,
`organizationId` int(11) DEFAULT NULL,
`number` varchar(15) DEFAULT NULL,
`department` varchar(50) DEFAULT NULL,
`server` varchar(20) DEFAULT NULL,
`reseller` varchar(75) DEFAULT NULL,
`operators` varchar(50) DEFAULT NULL,
`subscriber_type` int(11) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=166629 DEFAULT CHARSET=utf8;
INSERT INTO `mmsubscribers` (`id`, `email`, `name`, `hash`, `status`,
`listid`, `phone`, `login`, `pass`, `language`, `orgname`, `orgadmin`,
`domain`, `organizationId`, `number`, `department`, `server`, `reseller`,
`operators`, `subscriber_type`) VALUES
(13, 'nils#gmail.com', 'Nils Nissesson', NULL, 'inactive', 2, '+4612312313',
'user1', 'user1', 'SV', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'Reseller1',
NULL, NULL),
(15, 'nils#hotmail.com', 'Nils Karlsson', 'b5q8gelmyvp4gu9sgntgvzkha0u', 'active', 2, '+47741741109874', NULL, NULL, 'EN', '', NULL, NULL, NULL, NULL, NULL, '', '', '', NULL),
(17, 'test#testing.com', 'Test Karlsson', 'mdpte4uhkvqzew4n2megoa4qk7k', 'active', 2, '+4482798273798', NULL, NULL, 'SV', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL),
(18, 'test123#testdomain.se', 'Herr Vincze', 'mhw81k96liwlfj8lkyvu2rdr9y', 'inactive', 2, '', NULL, NULL, 'SV', '', NULL, '', NULL, '', '', '', '', '', NULL),
(146996, 'nils.nils#nils.se', 'Nils Dolk', 'dd07c78e2fa7487b283f6c4dbff7ec0fc448a4', 'active', 3, '', NULL, NULL, '', 'Test', 1, 'nils.se', 6721, '468123123454', 'Sälj', '185.XX.124.162', 'Reseller56', 'STH-UNO', 4),
(149277, 'bo#bosse.com', 'Bo Holgersson', 'c557202473aef551d410a00d2b1be3075e8d7e1fe', 'active', 3, '', NULL, NULL, '', 'Test', 1, 'bo.se', 6578, '4653643232436', 'Företagsförsäljning', '185.39.124.154', 'Reseller1', 'STB-uno', 4),
(149824, 'support#nisse.no', 'testar', '9c5f161459236d5d216c48a47d0c2aecf1', 'active', 3, '', NULL, NULL, '', 'Test', 1, 'bo.se', 6578, '4654170399', '', '185.XX.124.162', 'Reseller1', 'Tele2', 4);
First table witch i want to do the join to:
CREATE TABLE IF NOT EXISTS `driftinfo_subscriber_operator_lookup` (
`id` int(11) NOT NULL,
`drifinfo_problem_id` int(11) DEFAULT NULL,
`operator` varchar(75) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
--
-- Dumpning av Data i tabell `driftinfo_subscriber_operator_lookup`
--
INSERT INTO `driftinfo_subscriber_operator_lookup` (`id`, `drifinfo_problem_id`, `operator`) VALUES
(1, 4, 'Weblink'),
(4, 10, 'Tele2');
Second table:
CREATE TABLE IF NOT EXISTS `driftinfo_subscriber_reseller_lookup` (
`id` int(11) NOT NULL,
`drifinfo_problem_id` int(11) DEFAULT NULL,
`reseller` varchar(75) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
--
-- Dumpning av Data i tabell `driftinfo_subscriber_reseller_lookup`
--
INSERT INTO `driftinfo_subscriber_reseller_lookup` (`id`, `drifinfo_problem_id`, `reseller`) VALUES
(1, 4, 'Reseller1'),
(2, 4, 'Reseller2'),
(5, 10, 'Reseller3'),
(6, 10, 'BestReseller'),
(7, 10, 'BadReseller');
Third table:
CREATE TABLE IF NOT EXISTS `driftinfo_subscriber_server_lookup` (
`id` int(11) NOT NULL,
`drifinfo_problem_id` int(11) DEFAULT NULL,
`server` varchar(50) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
--
-- Dumpning av Data i tabell `driftinfo_subscriber_server_lookup`
--
INSERT INTO `driftinfo_subscriber_server_lookup` (`id`, `drifinfo_problem_id`, `server`) VALUES
(1, 4, '185.XX.124.162'),
(8, 10, '172.XX.129.20'),
(9, 10, '172.XX.129.21');
My last table:
CREATE TABLE IF NOT EXISTS `driftinfo_problem` (
`id` int(11) NOT NULL,
`active` varchar(11) DEFAULT NULL,
`date` date DEFAULT NULL,
`omrade` varchar(75) DEFAULT NULL,
`soluno_staging_message` varchar(750) DEFAULT NULL,
`heading` varchar(100) DEFAULT NULL,
`ingress` varchar(750) DEFAULT NULL,
`image` varchar(1500) DEFAULT NULL,
`text` varchar(3000) DEFAULT NULL,
`beskrivning` varchar(250) DEFAULT NULL,
`user_owner` varchar(75) DEFAULT NULL,
`estimate_finished_date` date DEFAULT NULL,
`language` varchar(50) DEFAULT NULL,
`time` time DEFAULT NULL,
`estimate_finished_time` time DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;
--
-- Dumpning av Data i tabell `driftinfo_problem`
--
INSERT INTO `driftinfo_problem` (`id`, `active`, `date`, `omrade`, `soluno_staging_message`, `heading`, `ingress`, `image`, `text`, `beskrivning`, `user_owner`, `estimate_finished_date`, `language`, `time`, `estimate_finished_time`) VALUES
(2, 'No', '2018-04-18', 'Test Problem', NULL, NULL, NULL, NULL, NULL, NULL, NULL, '2018-04-18', NULL, '09:04:00', '16:00:00'),
(3, 'No', '2018-04-27', 'Operatör tysta samtal', NULL, NULL, NULL, NULL, NULL, NULL, NULL, '2018-04-27', NULL, '11:35:00', '14:30:00'),
(4, 'No', '2018-05-16', 'Operator driftstörning', NULL, NULL, NULL, NULL, NULL, NULL, NULL, '2018-05-16', NULL, '11:20:00', '14:00:00'),
(5, 'No', '2018-05-17', 'Operator driftstörning', NULL, NULL, NULL, NULL, NULL, NULL, NULL, '2018-05-17', NULL, '14:30:00', '17:00:00'),
(6, 'No', '2018-05-18', 'Internet problem', NULL, NULL, NULL, NULL, NULL, NULL, NULL, '2018-05-18', NULL, '16:00:00', '18:00:00'),
(7, 'No', '2018-05-25', 'Operator driftstörning', NULL, NULL, NULL, NULL, NULL, NULL, NULL, '2018-05-25', NULL, '14:30:00', '17:00:00'),
(8, 'No', '2018-05-28', 'Operator routing problem', NULL, NULL, NULL, NULL, NULL, NULL, NULL, '2018-05-29', NULL, '16:00:00', '12:00:00'),
(10, 'Yes', '2018-05-31', 'Operator Business störning', NULL, NULL, NULL, NULL, NULL, NULL, NULL, '2018-05-31', NULL, '10:00:00', '14:00:00');
SQL Fiddle
I need to do an combination of these 3 selects into 1 select if possible.
Select1 operator:
SELECT
mmsubscribers.email,
driftinfo_problem.active,
driftinfo_problem.id,
mmsubscribers.reseller,
mmsubscribers.operators,
mmsubscribers.server
FROM mmsubscribers
INNER JOIN driftinfo_subscriber_operator_lookup ON mmsubscribers.operators = driftinfo_subscriber_operator_lookup.`operator`
INNER JOIN driftinfo_problem ON driftinfo_subscriber_operator_lookup.drifinfo_problem_id = driftinfo_problem.id
WHERE driftinfo_problem.active = 'Yes'
Select2 reseller:
SELECT
mmsubscribers.email,
driftinfo_problem.active,
driftinfo_problem.id,
mmsubscribers.reseller,
mmsubscribers.operators,
mmsubscribers.server
FROM mmsubscribers
INNER JOIN driftinfo_subscriber_reseller_lookup ON mmsubscribers.reseller = driftinfo_subscriber_reseller_lookup.reseller
INNER JOIN driftinfo_problem ON driftinfo_subscriber_reseller_lookup.drifinfo_problem_id = driftinfo_problem.id
Select3 server:
SELECT
mmsubscribers.email,
driftinfo_problem.active,
driftinfo_problem.id,
mmsubscribers.reseller,
mmsubscribers.operators,
mmsubscribers.server
FROM mmsubscribers
INNER JOIN driftinfo_subscriber_server_lookup ON mmsubscribers.server = driftinfo_subscriber_server_lookup.server
INNER JOIN driftinfo_problem ON driftinfo_subscriber_server_lookup.drifinfo_problem_id = driftinfo_problem.id
Expected output that i woul like to get is:
email active id reseller operators server
nils#gmail.com No 4 Reseller1 (null) (null)
bo#bosse.com No 4 Reseller1 STB-uno 185.39.124.154
support#nisse.no No 4 Reseller1 Tele2 185.XX.124.162
nils.nils#nils.se No 4 Reseller56 STH-UNO 185.XX.124.162
support#nisse.no No 4 Reseller1 Tele2 185.XX.124.162
support#nisse.no Yes 10 Reseller1 Tele2 185.XX.124.162
SELECT
mmsubscribers.email,
driftinfo_problem.active,
driftinfo_problem.id,
mmsubscribers.reseller,
mmsubscribers.operators,
mmsubscribers.server
FROM mmsubscribers
INNER JOIN driftinfo_subscriber_operator_lookup ON mmsubscribers.operators = driftinfo_subscriber_operator_lookup.`operator`
INNER JOIN driftinfo_problem ON driftinfo_subscriber_operator_lookup.drifinfo_problem_id = driftinfo_problem.id
WHERE driftinfo_problem.active = 'Yes'
union all
SELECT
mmsubscribers.email,
driftinfo_problem.active,
driftinfo_problem.id,
mmsubscribers.reseller,
mmsubscribers.operators,
mmsubscribers.server
FROM mmsubscribers
INNER JOIN driftinfo_subscriber_reseller_lookup ON mmsubscribers.reseller = driftinfo_subscriber_reseller_lookup.reseller
INNER JOIN driftinfo_problem ON driftinfo_subscriber_reseller_lookup.drifinfo_problem_id = driftinfo_problem.id
union all
SELECT
mmsubscribers.email,
driftinfo_problem.active,
driftinfo_problem.id,
mmsubscribers.reseller,
mmsubscribers.operators,
mmsubscribers.server
FROM mmsubscribers
INNER JOIN driftinfo_subscriber_server_lookup ON mmsubscribers.server = driftinfo_subscriber_server_lookup.server
INNER JOIN driftinfo_problem ON driftinfo_subscriber_server_lookup.drifinfo_problem_id = driftinfo_problem.id;
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.
Fiddle Here:
http://sqlfiddle.com/#!2/2f85f4/1
I am trying to create an 'inventory demand' report essentially; what products were sold between X and Y and how much of them.
The closest I can get is this query, but this doesn't return the correct math.
Can someone please offer some guidance?
SELECT orders_products.op_products_name
, orders_products.op_products_id
, SUM(orders_products.op_products_qty) AS TotalSold
FROM orders_products
, orders
WHERE orders.orders_date_purchased
AND orders.orders_date_purchased BETWEEN '2012-11-05 00:00:00' AND '2012-11-10 00:00:00'
GROUP
BY orders_products.op_products_id
ORDER
BY TotalSold DESC
The schema is available here:
--
-- Table structure for table orders
CREATE TABLE IF NOT EXISTS orders (
orders_ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
orders_date_purchased timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
orders_delivery_name varchar(100) NOT NULL,
orders_delivery_company_name varchar(100) NOT NULL,
orders_delivery_address varchar(255) NOT NULL,
orders_delivery_city varchar(100) NOT NULL,
orders_delivery_state char(2) NOT NULL,
orders_delivery_zipcode char(5) NOT NULL,
orders_delivery_phone char(10) NOT NULL,
orders_delivery_email varchar(100) NOT NULL,
orders_billing_name varchar(120) DEFAULT NULL,
orders_billing_address varchar(255) DEFAULT NULL,
orders_billing_city varchar(100) DEFAULT NULL,
orders_billing_state char(2) DEFAULT NULL,
orders_billing_zipcode char(5) DEFAULT NULL,
orders_billing_phone char(10) DEFAULT NULL,
orders_billing_email varchar(100) DEFAULT NULL,
orders_users_ID bigint(20) NOT NULL,
orders_distributor_ID bigint(20) DEFAULT NULL,
orders_affiliate_ID bigint(20) DEFAULT NULL,
orders_sales_tax decimal(11,2) NOT NULL,
orders_discount_applied decimal(11,2) DEFAULT NULL,
orders_ip_address char(15) NOT NULL,
orders_shipping_method varchar(255) NOT NULL,
orders_payment_method int(10) unsigned NOT NULL,
orders_order_total decimal(11,2) NOT NULL,
orders_shipping_cost decimal(11,2) DEFAULT NULL,
orders_total_saved decimal(11,2) NOT NULL,
orders_placed_by bigint(20) NOT NULL,
notes blob,
orders_inv_status int(11) DEFAULT NULL,
orders_date_modified timestamp NULL DEFAULT NULL,
orders_process_status int(11) DEFAULT '1',
PRIMARY KEY (orders_ID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2379 ;
--
-- Dumping data for table orders
INSERT INTO orders (orders_ID, orders_date_purchased, orders_delivery_name, orders_delivery_company_name, orders_delivery_address, orders_delivery_city, orders_delivery_state, orders_delivery_zipcode, orders_delivery_phone, orders_delivery_email, orders_billing_name, orders_billing_address, orders_billing_city, orders_billing_state, orders_billing_zipcode, orders_billing_phone, orders_billing_email, orders_users_ID, orders_distributor_ID, orders_affiliate_ID, orders_sales_tax, orders_discount_applied, orders_ip_address, orders_shipping_method, orders_payment_method, orders_order_total, orders_shipping_cost, orders_total_saved, orders_placed_by, notes, orders_inv_status, orders_date_modified, orders_process_status) VALUES
(1, '2012-11-05 19:58:12', 'John Smith', '', '123 Manatee Street', 'Navarre', 'FL', '32566', '8508675309', 'email#email.com', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 83, NULL, NULL, 0.06, NULL, '00.000.000.00', 'in_person_delivery', 9, 61.59, 0.00, 0.00, 0, NULL, 8, NULL, 3),
(2, '2012-11-06 01:05:20', 'Judy Richards', '', '456 Devmor Ct', 'Navarre', 'FL', '32566', '8508675309', 'email#email.com', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 87, NULL, NULL, 0.06, NULL, '00.000.000.00', 'in_person_delivery', 9, 158.97, 0.00, 0.00, 0, NULL, 8, NULL, 3),
(4, '2012-11-08 04:32:23', 'John Smith', '', '123 Manatee Street', 'Navarre', 'FL', '32578', '8508675309', 'email#email.com', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 92, NULL, NULL, 0.06, NULL, '00.000.000.00', 'in_person_delivery', 9, 52.99, NULL, 0.00, 0, NULL, 8, '0000-00-00 00:00:00', 3),
(5, '2012-11-09 00:11:54', 'Adam Davis', '', '4307 D134 Legendary Dr.', 'Navarre', 'FL', '32541', '8508675309', 'email#email.com', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 89, 84, 0, 0.06, NULL, '00.000.000.00', 'in_person_delivery', 9, 160.51, 0.00, 0.00, 0, NULL, 8, NULL, 3),
(6, '2012-11-09 21:14:25', 'Judy Sterling', '', '2310 Lexington Lane', 'Navarre', 'FL', '32566', '8508675309', 'email#email.com', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 87, NULL, NULL, 0.06, NULL, '00.000.000.00', 'in_person_delivery', 9, 158.97, 0.00, 0.00, 0, NULL, 8, NULL, 3);
--
-- Table structure for table orders_products
CREATE TABLE IF NOT EXISTS orders_products (
orders_products_ID bigint(20) NOT NULL AUTO_INCREMENT,
op_order_id bigint(20) unsigned NOT NULL,
op_products_id bigint(20) unsigned NOT NULL,
op_products_mfr_part_number varchar(65) NOT NULL,
op_products_name varchar(15) NOT NULL,
op_products_qty bigint(10) unsigned NOT NULL,
PRIMARY KEY (orders_products_ID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5923 ;
--
-- Dumping data for table orders_products
INSERT INTO orders_products (orders_products_ID, op_order_id, op_products_id, op_products_mfr_part_number, op_products_name, op_products_qty) VALUES
(1, 1, 491, 'CWP8489A001BA', 'X25', 1),
(2, 2, 2134, 'Professional Fee', 'Professional Fe', 3),
(3, 3, 473, 'CWPDR360', 'DR-360', 1),
(4, 4, 2134, 'Professional Fee', 'Professional Fe', 1),
(5, 5, 362, 'CWPDR360', 'DR-360', 1);
--
-- Table structure for table products
CREATE TABLE IF NOT EXISTS products (
products_id bigint(20) NOT NULL AUTO_INCREMENT,
products_brand varchar(200) NOT NULL,
products_brand_type varchar(200) NOT NULL,
products_mfr_part_number varchar(65) NOT NULL,
products_common_name varchar(15) NOT NULL,
products_msrp decimal(11,2) NOT NULL,
products_price decimal(11,2) NOT NULL,
products_description varchar(255) NOT NULL,
products_weight decimal(11,2) NOT NULL DEFAULT '1.00',
products_length decimal(3,2) DEFAULT NULL,
products_width decimal(3,2) DEFAULT NULL,
products_height decimal(3,2) DEFAULT NULL,
products_tax_exempt int(11) DEFAULT NULL,
PRIMARY KEY (products_id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2558 ;
--
-- Dumping data for table products
INSERT INTO products (products_id, products_brand, products_brand_type, products_mfr_part_number, products_common_name, products_msrp, products_price, products_description, products_weight, products_length, products_width, products_height, products_tax_exempt) VALUES
(1, 'HP', 'Original', '92298A ', '98A', 132.99, 131.66, 'Genuine HP® LaserJet 98A Black Toner Cartridge (92298A) ', 4.20, 9.99, 6.14, 9.72, NULL),
(2, 'HP', 'Original', 'C3903A ', '03A', 112.99, 111.86, 'Genuine HP® LaserJet 03A Black Toner Cartridge (C3903A) ', 3.00, 9.99, 4.80, 7.72, NULL),
(3, 'HP', 'Original', 'C3906A ', '06A', 87.99, 87.11, 'Genuine HP® LaserJet 06A Black Toner Cartridge (C3906A)', 2.13, 9.99, 5.16, 6.50, NULL),
(4, 'HP', 'Original', 'C3909A', '09A', 254.99, 252.44, 'Genuine HP® LaserJet 09A Black Toner Cartridge (C3909A)', 3.31, 9.99, 4.72, 8.07, NULL),
(5, 'HP', 'Original', 'C4092A ', '92A', 75.99, 75.23, 'Genuine HP® LaserJet 92A Black Toner Cartridge (C4092A) ', 2.20, 9.99, 5.16, 6.50, NULL);
The major problem is that you need a join condition between orders_products and orders. You have a , which is equivalent to cross join. As a simple rule: never use commas in the from clause.
In addition, the where clause was awkwardly phrased. Here is a rewrite of your query that also uses table aliases for readability:
SELECT op.op_products_name, op.op_products_id, SUM(op.op_products_qty) AS TotalSold
FROM orders_products op JOIN
orders o
ON op.op_order_id = o.orders_id
WHERE o.orders_date_purchased BETWEEN '2012-11-05 00:00:00' AND '2012-11-10 00:00:00'
GROUP BY op.op_products_id
ORDER BY TotalSold DESC;
I need to get the name of aircraft (aircraft_type), sum of time on individual aircraft (flag A on aircraft_master), sum of amount fee paid for individual aircraft (ac_id under fee table)
Using query as
select
t1.aircraft_type,
sec_to_time(sum(time_to_sec(t2.ft_duration))),
sum(t3.fee_amount)
from
aircraft_master t1
left join flying_test t2 on t2.ac_id = t1.ac_id
left join fees_detail t3 on t3.ac_id = t1.ac_id
where
t2.per_detail_id = '12'
and t3.per_detail_id = '12'
group by
t1.ac_id]
Without join for flying_test I get the correct flight time; without join for fee I get the correct amount paid for individual aircraft. Using both I get multiple of time and fee.
SQL table and data:
-- phpMyAdmin SQL Dump
-- version 3.4.5
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jul 11, 2012 at 11:07 PM
-- Server version: 5.5.16
-- PHP Version: 5.3.8
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `aviation`
--
-- --------------------------------------------------------
--
-- Table structure for table `aircraft_master`
--
CREATE TABLE IF NOT EXISTS `aircraft_master` (
`ac_id` int(10) NOT NULL AUTO_INCREMENT,
`aircraft_type` varchar(50) DEFAULT NULL,
`aircraft_engine` varchar(150) DEFAULT NULL,
`flag` varchar(1) DEFAULT NULL,
PRIMARY KEY (`ac_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=22 ;
--
-- Dumping data for table `aircraft_master`
--
INSERT INTO `aircraft_master` (`ac_id`, `aircraft_type`, `aircraft_engine`, `flag`) VALUES
(1, NULL, 'Single Engine', 'E'),
(2, NULL, 'Multi Engine', 'E'),
(3, 'CH2T', '1', 'A'),
(4, 'C172', '1', 'A'),
(5, 'C152', '1', 'A'),
(6, 'VT-SMT', '3', 'R'),
(7, 'VT-MDU', '3', 'R'),
(8, 'VT-RIT', '3', 'R'),
(9, 'VT-AFA', '4', 'R'),
(10, 'VT-TSM', '4', 'R'),
(11, 'VT-AFR', '4', 'R'),
(12, 'VT-RRM', '4', 'R'),
(13, 'VT-JSN', '4', 'R'),
(14, 'VT-AKN', '5', 'R'),
(15, 'VT-PBA', '5', 'R'),
(16, 'VT-AKA', '5', 'R'),
(17, 'VT-AFN', '5', 'R'),
(18, 'VT-AFN', '5', 'R'),
(19, 'VT-EMO', '5', 'R'),
(20, 'VT-EMP', '5', 'R'),
(21, 'VT-RJT', '3', 'R');
-- --------------------------------------------------------
--
-- Table structure for table `fees_detail`
--
CREATE TABLE IF NOT EXISTS `fees_detail` (
`fee_id` int(11) NOT NULL AUTO_INCREMENT,
`per_detail_id` int(10) NOT NULL,
`fee_detail_id` varchar(10) NOT NULL,
`fee_date` date DEFAULT NULL,
`mode_id` int(11) DEFAULT NULL,
`fee_amount` int(10) DEFAULT NULL,
`bank_id` int(20) DEFAULT NULL,
`bank_branch` varchar(100) DEFAULT NULL,
`chqdd_no` int(20) DEFAULT NULL,
`fee_comment` varchar(150) DEFAULT NULL,
`fee_posting_time` datetime NOT NULL,
`ac_id` int(10) DEFAULT NULL,
PRIMARY KEY (`fee_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=19 ;
--
-- Dumping data for table `fees_detail`
--
INSERT INTO `fees_detail` (`fee_id`, `per_detail_id`, `fee_detail_id`, `fee_date`, `mode_id`, `fee_amount`, `bank_id`, `bank_branch`, `chqdd_no`, `fee_comment`, `fee_posting_time`, `ac_id`) VALUES
(16, 12, '9', '2012-06-13', 87, 70000, NULL, NULL, NULL, 'c172', '2012-06-16 17:20:34', 4),
(17, 12, '10', NULL, NULL, 10000, NULL, NULL, NULL, NULL, '0000-00-00 00:00:00', 3),
(18, 12, '10', NULL, NULL, 10000, NULL, NULL, NULL, NULL, '2012-07-10 12:00:05', 3);
-- --------------------------------------------------------
--
-- Table structure for table `flying_test`
--
CREATE TABLE IF NOT EXISTS `flying_test` (
`ft_id` int(10) NOT NULL AUTO_INCREMENT,
`ft_from` varchar(10) DEFAULT NULL,
`ft_from_time` time DEFAULT NULL,
`ft_to` varchar(10) DEFAULT NULL,
`ft_to_time` time DEFAULT NULL,
`ft_pic` varchar(50) DEFAULT NULL,
`ft_engine` varchar(10) DEFAULT NULL,
`per_detail_id` int(10) NOT NULL,
`ft_remark` varchar(512) DEFAULT NULL,
`ft_type` int(3) DEFAULT NULL,
`ft_ftftpr` int(10) DEFAULT NULL,
`ft_nature_exercise` int(3) DEFAULT NULL,
`ftpr_ut` varchar(150) DEFAULT NULL,
`ftpr_co_pilot` varchar(150) DEFAULT NULL,
`ftpr_pi_us` varchar(150) DEFAULT NULL,
`ft_status` int(3) DEFAULT NULL,
`ft_aircraft` int(5) DEFAULT NULL,
`ft_regno` int(3) DEFAULT NULL,
`ft_landings` int(3) DEFAULT NULL,
`ft_duration` time DEFAULT NULL,
`fee_detail_id` int(10) DEFAULT NULL,
`ft_act` time DEFAULT NULL,
`ft_sim` time DEFAULT NULL,
`ft_simulator` time DEFAULT NULL,
`ft_dh` int(3) DEFAULT NULL,
`ft_exercise` int(3) DEFAULT NULL,
`ft_date` date DEFAULT NULL,
PRIMARY KEY (`ft_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=83 ;
--
-- Dumping data for table `flying_test`
--
INSERT INTO `flying_test` (`ft_id`, `ft_from`, `ft_from_time`, `ft_to`, `ft_to_time`, `ft_pic`, `ft_engine`, `per_detail_id`, `ft_remark`, `ft_type`, `ft_ftftpr`, `ft_nature_exercise`, `ftpr_ut`, `ftpr_co_pilot`, `ftpr_pi_us`, `ft_status`, `ft_aircraft`, `ft_regno`, `ft_landings`, `ft_duration`, `fee_detail_id`, `ft_act`, `ft_sim`, `ft_simulator`, `ft_dh`, `ft_exercise`, `ft_date`) VALUES
(79, 'VISL', '10:00:00', 'VISL', '23:00:00', 'vcvc', '1', 12, 'dss', NULL, 16, 101, NULL, NULL, NULL, 20, 3, 8, 1, '15:30:00', 10, '00:00:00', '00:00:00', '00:00:00', 18, 38, '2012-07-07'),
(80, NULL, NULL, NULL, NULL, NULL, NULL, 12, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 4, NULL, NULL, '02:30:00', 9, NULL, NULL, NULL, NULL, NULL, '2012-07-25'),
(81, NULL, NULL, NULL, NULL, NULL, NULL, 12, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 3, NULL, NULL, '02:30:00', 10, NULL, NULL, NULL, NULL, NULL, '2012-07-25'),
(82, 'VISL', '15:30:00', 'VISL', '15:45:00', 'none', '1', 12, 'none', NULL, 16, 100, NULL, NULL, NULL, 20, 3, 6, 1, '00:15:00', NULL, '00:00:00', '00:00:00', '00:00:00', 18, 38, '2012-07-11');
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;
You have a classic problem where you are joining one table to several others, but need partial aggregations.
You need to solve this by joining subqueries:
select t1.aircraft_type,
s1.val,
sus2.fees
from (select t1.aircraft_type, sec_to_time(sum(time_to_sec(t2.ft_duration))) as val
from aircraft_master t1 left join
flying_test t2
on t2.ac_id = t1.ac_id
where t2.per_detail_id = '12'
group by t1.aircraft_type
) s1 left outer join
(select ac_id, sum(t3.fee_amount) as fees
from fees_detail
where t3.per_detail_id = '13'
group by ac_id
) s2
on s2.ac_id = s1.ac_id
I apologize if this is not quite correct. I am having problems with my browser, so I hope you get the idea.