I have got two tables by name pricechangeimpwl and suppourtimpwl
The two tables contain name in common .
This is my schema
CREATE TABLE IF NOT EXISTS `pricechangeimpwl` (
`name` varchar(100) DEFAULT NULL,
`p1` decimal(10,2) DEFAULT NULL,
`time1` varchar(100) DEFAULT NULL,
`p2` decimal(10,2) DEFAULT NULL,
`time2` varchar(100) DEFAULT NULL,
`whatnext` varchar(50) DEFAULT NULL,
`stock_low` decimal(6,2) DEFAULT NULL,
`buy_sell_diff` varchar(100) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `pricechangeimpwl` (`name`, `p1`, `time1`, `p2`, `time2`, `whatnext`, `stock_low`, `buy_sell_diff`) VALUES
('DIVISLAB', 631.75, '2017-03-21 15:29:09', 630.85, '2017-03-21 15:40:47', 'plzcontinue', 628.60, '-417055'),
('M&M', 1297.00, '2017-03-21 15:29:09', 1299.00, '2017-03-21 15:40:47', 'plzcontinue', 1271.00, '9190');
-- Dumping structure for table trade_chit_chat.suppourtimpwl
CREATE TABLE IF NOT EXISTS `suppourtimpwl` (
`name` varchar(100) DEFAULT NULL,
`suppourt1` decimal(10,2) DEFAULT NULL,
`suppourt2` decimal(10,2) DEFAULT NULL,
`suppourt3` decimal(10,2) DEFAULT NULL,
`date` varchar(100) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `suppourtimpwl` (`name`, `suppourt1`, `suppourt2`, `suppourt3`, `date`) VALUES
('DIVISLAB', 762.54, 752.68, 739.35, '20 March 2017'),
('M&M', 1292.25, 1284.30, 1273.41, '20 March 2017');
This is my sql fiddle
http://sqlfiddle.com/#!9/0a9476/1
Can we write a select query so that it will fetch suppourt1 , suppourt2 and suppourt3 values for each symbol of pricechangeimpwl ??
select * from pricechangeimpwl p JOIN suppourtimpwl s
on p.name = s.name;
Related
I can't get this to work
CREATE TABLE `oc_tax_class` (
`tax_class_id` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`description` varchar(255) NOT NULL,
`date_added` datetime NOT NULL,
`date_modified` datetime NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `oc_tax_rate`
--
CREATE TABLE `oc_tax_rate` (
`tax_rate_id` int(11) NOT NULL,
`geo_zone_id` int(11) NOT NULL DEFAULT 0,
`name` varchar(255) NOT NULL,
`rate` decimal(15,4) NOT NULL DEFAULT 0.0000,
`type` char(1) NOT NULL,
`date_added` datetime NOT NULL,
`date_modified` datetime NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `oc_tax_rule`
--
CREATE TABLE `oc_tax_rule` (
`tax_rule_id` int(11) NOT NULL,
`tax_class_id` int(11) NOT NULL,
`tax_rate_id` int(11) NOT NULL,
`based` varchar(10) NOT NULL,
`priority` int(5) NOT NULL DEFAULT 1
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
3 tables. I want oc_tax_class.title = oc_tax_rate.name
I believe, although I'm not sure, that I should
INSERT INTO oc_tax_class(title)
or
UPDATE oc_tax_class SET title = ...
SELECT oc_tax_rate.name, oc_tax_rule.tax_class_id
JOIN oc_tax_rule ON oc_tax_rate.tax_rate_id = oc_tax_rule.tax_rate_id
And then I don't know what to do next.
I need to copy values from one column to another table, passing through a connecting table.
MySQL supports a multi-table UPDATE syntax, but the documentation (https://dev.mysql.com/doc/refman/en/update.html) has pretty sparse examples of it.
In your case, this may work:
UPDATE oc_tax_class
JOIN oc_tax_rule USING (tax_class_id)
JOIN oc_tax_rate USING (tax_rate_id)
SET oc_tax_class.title = oc_tax_rate.name;
I did not test this. I suggest you test it first on a sample of your data, to make sure it works the way you want it to.
My schema and sample data set:
CREATE TABLE `bsbi_hedge_fund` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`fund_name` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`abn_number` varchar(13) COLLATE utf8_unicode_ci DEFAULT NULL,
`parent_company_id` int(11) DEFAULT NULL,
`email` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`contact_no` varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL,
`address_one` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`address_two` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`city_name` varchar(60) COLLATE utf8_unicode_ci DEFAULT NULL,
`state_name` varchar(60) COLLATE utf8_unicode_ci DEFAULT NULL,
`zip_code` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
`country_tbl_id` int(11) DEFAULT NULL,
`status` varchar(10) COLLATE utf8_unicode_ci DEFAULT 'active',
`created_by` int(11) DEFAULT NULL,
`created_date` datetime DEFAULT NULL,
`modified_by` int(11) DEFAULT NULL,
`modified_date` datetime DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Dumping data for table `bsbi_hedge_fund`
--
INSERT INTO `bsbi_hedge_fund` (`id`, `fund_name`, `abn_number`, `parent_company_id`, `email`, `contact_no`, `address_one`, `address_two`, `city_name`, `state_name`, `zip_code`, `country_tbl_id`, `status`, `created_by`, `created_date`, `modified_by`, `modified_date`) VALUES
(1, 'iCAP Hedge Funds', '53616271062', 1, 'icaptrading#gmail.com', '0425175887', '68 Roy Marika Street', '', 'Bonner', 'ACT', '2914', 12, 'active', 1, '2020-02-20 07:02:51', NULL, NULL);
CREATE TABLE `bsbi_company` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`company_name` varchar(60) COLLATE utf8_unicode_ci DEFAULT NULL,
`abn_number` varchar(13) COLLATE utf8_unicode_ci DEFAULT NULL,
`acn_number` varchar(13) COLLATE utf8_unicode_ci DEFAULT NULL,
`email` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`contact_no` varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL,
`address_one` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`address_two` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`city_name` varchar(60) COLLATE utf8_unicode_ci DEFAULT NULL,
`state_name` varchar(60) COLLATE utf8_unicode_ci DEFAULT NULL,
`zip_code` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
`country_tbl_id` int(11) DEFAULT NULL,
`status` varchar(10) COLLATE utf8_unicode_ci DEFAULT 'active',
`created_by` int(11) DEFAULT NULL,
`created_date` datetime DEFAULT NULL,
`modified_by` int(11) DEFAULT NULL,
`modified_date` datetime DEFAULT NULL,
`allocated_money` float(17,5) DEFAULT '0.00000',
`hedge_fund_allocation` float(17,5) NOT NULL,
`investment_class_allocation` float(17,5) NOT NULL,
`hedge_fund_money` float(10,3) DEFAULT '0.000',
`inv_class_money` float(10,3) DEFAULT '0.000'
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Dumping data for table `bsbi_company`
--
INSERT INTO `bsbi_company` (`id`, `company_name`, `abn_number`, `acn_number`, `email`, `contact_no`, `address_one`, `address_two`, `city_name`, `state_name`, `zip_code`, `country_tbl_id`, `status`, `created_by`, `created_date`, `modified_by`, `modified_date`, `allocated_money`, `hedge_fund_allocation`, `investment_class_allocation`, `hedge_fund_money`, `inv_class_money`) VALUES
(1, 'Investment and Capital Growth for Australian Professional ( ', '53616271062', '2123', 'abc#gmail.com', '2343', '68 Roy Marika Street', '', 'Bonner', 'ACT', '2914', 12, 'active', 1, '2020-02-20 07:01:26', 1, '2020-03-07 12:13:53', 22847.00000, 0.00000, 0.00000, 20000.000, 19000.000);
CREATE TABLE `bsbi_hedge_fund_journal` (
`id` int(11) NOT NULL,
`company_id` int(11) DEFAULT NULL,
`hedge_fund_id` int(11) DEFAULT NULL,
`amount` float(10,3) DEFAULT NULL,
`tran_type` varchar(7) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_by` int(11) DEFAULT NULL,
`created_date` datetime DEFAULT NULL,
`modified_by` int(11) DEFAULT NULL,
`modified_date` datetime DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Dumping data for table `bsbi_hedge_fund_journal`
--
INSERT INTO `bsbi_hedge_fund_journal` (`id`, `company_id`, `hedge_fund_id`, `amount`, `tran_type`, `created_by`, `created_date`, `modified_by`, `modified_date`) VALUES
(1, 1, 1, 20000.000, 'credit', 1, '2020-03-07 11:45:40', NULL, NULL);
CREATE TABLE `bsbi_investment_allocation_class_journal` (
`id` int(11) NOT NULL,
`hedge_fund_id` int(11) DEFAULT NULL,
`investment_class_id` int(11) DEFAULT NULL,
`amount` float(10,3) DEFAULT NULL,
`tran_type` varchar(7) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_by` int(11) DEFAULT NULL,
`created_date` datetime DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Dumping data for table `bsbi_investment_allocation_class_journal`
--
INSERT INTO `bsbi_investment_allocation_class_journal` (`id`, `hedge_fund_id`, `investment_class_id`, `amount`, `tran_type`, `created_by`, `created_date`) VALUES
(1, 1, 1, 18000.000, 'credit', 1, '2020-03-07 11:46:09'),
(2, 1, 1, 2000.000, 'credit', 1, '2020-03-07 12:12:02'),
(3, 1, 1, 1000.000, 'debit', 1, '2020-03-07 12:13:53');
...and fiddle of same ( http://sqlfiddle.com/#!9/9cd46d/2 ):
I have written the following query to fetch some data from the database:
SELECT
hf.*, com.company_name company_name, com.id com_id,
IFNULL( SUM( hfc.amount ), 0 ) hedge_credit, IFNULL( SUM( hfd.amount ), 0 ) hedge_debit,
IFNULL( SUM( cjc.amount ), 0 ) class_credit, IFNULL( SUM( cjd.amount ), 0 ) class_debit
FROM bsbi_hedge_fund hf
INNER JOIN bsbi_company com ON hf.parent_company_id = com.id
LEFT JOIN bsbi_hedge_fund_journal hfc ON (hfc.hedge_fund_id = hf.id AND hfc.tran_type='credit')
LEFT JOIN bsbi_hedge_fund_journal hfd ON ( hfd.hedge_fund_id = hf.id AND hfd.tran_type = 'debit' )
LEFT JOIN bsbi_investment_allocation_class_journal cjc ON (cjc.hedge_fund_id = hf.id AND cjc.tran_type = 'credit' )
LEFT JOIN bsbi_investment_allocation_class_journal cjd ON (cjd.hedge_fund_id = hf.id AND cjd.tran_type = 'debit' )
ORDER BY hf.id ASC
Here is the output of this query:
The problem I face is: hedge_credit value is 20000 but it shows 40000! similarly class_debit is 1000 but it shows 2000
After doing some R&D what I found is: bsbi_investment_allocation_class_journal table has two entry having tran_type = credit but for some unknown reason the following line of my query fetch two rows, rather sum the values (and I fear the same thing will happen to other left join if they also have more than one record of the same type!)
LEFT JOIN bsbi_investment_allocation_class_journal cjc ON (cjc.hedge_fund_id = hf.id AND cjc.tran_type = 'credit' )
If I add Group By clause to above query then I got:
Notice that class_credit has two different values that should sum up but does not sum for an unknown reason!!
Can anyone tell me what is actually wrong in my query that raises this issue?
- Thanks
Joins generate all possible combinations of rows that meet join criteria. Because you're using only hfc.hedge_fund_id = hf.id to join tables, you are duplicating rows. If there were three transactions in total, each would be tripled..
You'll need to select more data from the tables containing transactions to keep the rows unique: timestamps or transaction ids. I would usually select the unique rows that I want first, then left join the non-unique data (name, country, etc) to each row.
I got the solution from this: https://dba.stackexchange.com/questions/154586/sum-over-distinct-rows-with-multiple-joins
If someone faces the exact type of problem then they can check this solution: https://dba.stackexchange.com/questions/154586/sum-over-distinct-rows-with-multiple-joins
I have two Table namely admin_user,admin_user_bak
the structure of the table admin_user is
CREATE TABLE IF NOT EXISTS `admin_user` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(150) NOT NULL,
`name` varchar(150) NOT NULL,
`emailid` varchar(150) NOT NULL,
`password` varchar(150) NOT NULL,
`roll` varchar(50) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`last_login` datetime NOT NULL,
`status` enum('active','inactive') NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
and structure of table admin_user_bak have all the fields of admin_user and additionally a field bak_user_id . its auto increment id..
CREATE TABLE IF NOT EXISTS `admin_user_bak` (
`bak_user_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`user_name` varchar(150) NOT NULL,
`name` varchar(150) NOT NULL,
`emailid` varchar(150) NOT NULL,
`password` varchar(150) NOT NULL,
`roll` varchar(50) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`last_login` datetime NOT NULL,
`status` enum('active','inactive') NOT NULL,
PRIMARY KEY (`bak_user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
here my trigger is
CREATE TRIGGER ins_admin_user BEFORE UPDATE ON admin_user
FOR EACH ROW
BEGIN
INSERT INTO admin_user_bak (user_id,user_name,name,emailid,password,roll,created,modified,last_login,status) VALUES ( NEW.user_id, NEW.user_name, NEW.emailid, new.password, NEW.roll, NEW.created, NEW.modified, NEW.last_login, NEW.status);
END
my purpose is i want to back up all events. insert update delete of a particular record. not all record. i write for insertion. its not working
any idea.. thanks
i want a insertion trigger but you had written update on in your trigger so it's not work.
You use INSERT ON
CREATE TRIGGER ins_admin_user BEFORE INSERT ON admin_user
FOR EACH ROW
BEGIN
INSERT INTO admin_user_bak (user_id,user_name,name,emailid,password,roll,created,modified,last_login,status) VALUES ( old.user_id, old.user_name, old.emailid, old.password, old.roll, old.created, old.modified, old.last_login, old.status);
END
I have this table which should be getting daily input regarding Vehicle mileage Readings.
CREATE TABLE `table_vehicle_info` (
`id` int(11) NOT NULL ,
`reg_no` varchar(50) NOT NULL,
`vehicle_type` int(11) NOT NULL,
`engine_no` varchar(50) NOT NULL DEFAULT "DDDD",
`chassis_no` varchar(50) ,
`model` int(11) DEFAULT NULL,
`picture` varchar(50) ,
`rent` double ,
`eng_power` int(11) DEFAULT NULL,
`color` varchar(12) DEFAULT NULL,
`rent_status` varchar(50) DEFAULT NULL,
`reg_city` varchar(50) DEFAULT NULL,
`location` varchar(50) DEFAULT NULL,
`sub_type` varchar(50) DEFAULT NULL,
`purchase_date` date DEFAULT NULL,
`purchase_amount` double DEFAULT NULL,
`sum_insured` double DEFAULT NULL,
`token_tax_amount` double DEFAULT NULL,
`token_period_from` date DEFAULT NULL,
`reg_placed_at` varchar(50) DEFAULT NULL,
`funded_by` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=99 DEFAULT CHARSET=latin1;
CREATE TABLE `tbl_readings` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`vehicle_id` int(11) NOT NULL,
`km_driven` int(11) NOT NULL,
`dt_of_reading` datetime NOT NULL,
`dt_of_entry` datetime NOT NULL,
`entry_user` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
KEY `vehicle_id` (`vehicle_id`),
CONSTRAINT `tbl_readings_ibfk_1` FOREIGN KEY (`vehicle_id`) REFERENCES `table_vehicle_info` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=latin1;
INSERT INTO table_vehicle_info (id,reg_no,vehicle_type )
VALUES (1, "B-5484", 12 );
INSERT INTO tbl_readings VALUES (NULL,1,100,'2015-6-1',NOW(), 'user');
I want to generate a report showing which vehicle reading isn't entered daily.
You can try following query, it returns all vehicle_id from tbl_readings for which there is no next day entry.
SELECT vehicle_id
FROM `tbl_readings`
WHERE DATE(DATE_ADD(dt_of_entry,INTERVAL 1 DAY)) NOT IN
(SELECT DATE(dt_of_entry) FROM `tbl_readings`)
EDIT
Try this,
SELECT t1.vehicle_id
FROM `tbl_readings` t1
LEFT JOIN (SELECT vehicle_id,
DATE(DATE_ADD(dt_of_reading,INTERVAL -1 DAY)) as reading_date
FROM `tbl_readings`) t2
ON t1.vehicle_id = t2.vehicle_id AND t1.dt_of_reading = t2.reading_date
WHERE t2.vehicle_id IS NULL
GROUP BY t1.vehicle_id
HAVING COUNT(t1.vehicle_id) > 1;
check the fiddle
DELIMITER $$
CREATE TRIGGER `msg_after_insert`
AFTER INSERT ON `meter_reading`
FOR EACH ROW
BEGIN
select meter_reading.*,a.amount as Light_Reading_Amt,b.amount as Power_Reading_Amt from meter_reading inner join master_unit a on a.min_unit<=msg1 and msg1<=a.max_unit and doc>=a.date_of_enter_value_fm inner join master_unit b on b.min_unit<=msg2 and msg2<=b.max_unit and doc<=b.date_of_enter_value_to;
INSERT INTO `smsd`.`outbox` (DestinationNumber,TextDecoded) VALUES (NEW.mobno, CONCAT_WS(NEW.msg1,NEW.Light_Reading_Amt,' ',NEW.msg2,NEW.Light_Reading_Amt));
END $$
DELIMITER ;
My databases are
1. mr_sms (tables are : 1.meter_reading 2.master_unit)
--meter_reading table--
CREATE TABLE IF NOT EXISTS `meter_reading` (
`serno` int(50) NOT NULL AUTO_INCREMENT,
`cno` varchar(50) DEFAULT NULL,
`accn_no` varchar(20) DEFAULT NULL,
`loc` varchar(20) DEFAULT NULL,
`type_of_accn` varchar(50) NOT NULL,
`rank` varchar(25) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`mobno` varchar(15) DEFAULT NULL,
`msg1` int(100) DEFAULT NULL,
`msg2` int(250) NOT NULL,
`status` varchar(30) NOT NULL,
`doc` date NOT NULL,
`remarks` varchar(100) DEFAULT NULL,
PRIMARY KEY (`serno`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=22 ;
--master_unit table--
CREATE TABLE IF NOT EXISTS `master_unit` (
`u_id` int(11) NOT NULL AUTO_INCREMENT,
`min_unit` int(150) NOT NULL,
`max_unit` int(200) NOT NULL,
`rate` varchar(20) NOT NULL,
`amount` varchar(20) NOT NULL,
`duty` varchar(20) NOT NULL,
`dutyamount` varchar(20) NOT NULL,
`date_of_enter_value_fm` date NOT NULL,
`date_of_enter_value_to` date NOT NULL,
PRIMARY KEY (`u_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
2. smsd (table is : outbox)
--
-- Table structure for table `outbox`
--
CREATE TABLE IF NOT EXISTS `outbox` (
`UpdatedInDB` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`InsertIntoDB` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`SendingDateTime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`SendBefore` time NOT NULL DEFAULT '23:59:59',
`SendAfter` time NOT NULL DEFAULT '00:00:00',
`Text` text,
`DestinationNumber` varchar(20) NOT NULL DEFAULT '',
`Coding` enum('Default_No_Compression','Unicode_No_Compression','8bit','Default_Compression','Unicode_Compression') NOT NULL DEFAULT 'Default_No_Compression',
`UDH` text,
`Class` int(11) DEFAULT '-1',
`TextDecoded` text NOT NULL,
`ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`MultiPart` enum('false','true') DEFAULT 'false',
`RelativeValidity` int(11) DEFAULT '-1',
`SenderID` varchar(255) DEFAULT NULL,
`SendingTimeOut` timestamp NULL DEFAULT '0000-00-00 00:00:00',
`DeliveryReport` enum('default','yes','no') DEFAULT 'default',
`CreatorID` text NOT NULL,
PRIMARY KEY (`ID`),
KEY `outbox_date` (`SendingDateTime`,`SendingTimeOut`),
KEY `outbox_sender` (`SenderID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `outbox`
--