How to retrieve Data from both the tables - mysql

This is my Database structure for two tables
CREATE TABLE `futstkrprices` (
`name` varchar(50) DEFAULT NULL,
`expiry_date` varchar(25) DEFAULT NULL,
`contract_type` varchar(50) DEFAULT NULL,
`close_price` decimal(15,2) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE `historical_data` (
`symbol_name` varchar(70) DEFAULT NULL,
`open_val` decimal(15,2) DEFAULT NULL,
`high_val` decimal(15,2) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO futstkrprices (name,expiry_date,contract_type,close_price)
values ('ABIRLANUVO' ,'26-MAY-2016','FUTSTK',870.65);
INSERT INTO futstkrprices (name,expiry_date,contract_type,close_price)
values ('ABIRLANUVO' ,'28-APR-2016','FUTSTK',866.40);
INSERT INTO futstkrprices (name,expiry_date,contract_type,close_price)
values ('ABIRLANUVO' ,'30-JUN-2016','FUTSTK',875.95);
INSERT INTO historical_data (symbol_name,open_val,high_val) values ('ABIRLANUVO',872.00,878.25)
Here is the sample Fiddle
http://sqlfiddle.com/#!9/1d4f20
Could you please tell me how can i retrieve data from both the tables
I have tried it as , but its not working
select futstkrprices.name , futstkrprices.expiry_date , futstkrprices.close_price , historical_data.symbol_name ,
historical_data.open_val , historical_data.high_val from futstkrprices LEFT JOIN futstkrprices
ON futstkrprices.name=historical_data.symbol_name;
The desired output i am expecting is
name expiry_date close_price symbol_name open_val high_val
ABIRLANUVO 26-MAY-2016 870.65 ABIRLANUVO 872 878.25
ABIRLANUVO 28-APR-2016 866.4 ABIRLANUVO 872 878.25
ABIRLANUVO 30-JUN-2016 875.95 ABIRLANUVO 872 878.25

Yuou have repetead two times the same table from futstkrprices LEFT JOIN futstkrprices
try this
select
futstkrprices.name ,
futstkrprices.expiry_date ,
futstkrprices.close_price ,
historical_data.symbol_name ,
historical_data.open_val ,
historical_data.high_val
from futstkrprices
LEFT JOIN historical_data
ON futstkrprices.name=historical_data.symbol_name;

Related

mysql insert into select join - copy values from one column to another table, passing through a connecting table

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.

Getting Group by with left join to work in MySQL 5.7

Im trying to get a query to only return a single row for each unique ID.
given the following:
CREATE TABLE `groups` ( `id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) DEFAULT NULL, `type` int(1) DEFAULT NULL,
`date` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB
AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
CREATE TABLE `groupmembers` ( `id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL, `readmessage` int(1) DEFAULT
NULL, `status` int(1) DEFAULT NULL, `group_id` int(20) DEFAULT
NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT
CHARSET=latin1;
CREATE TABLE `messages` ( `id` bigint(10) NOT NULL AUTO_INCREMENT,
`send_to` varchar(50) NOT NULL DEFAULT '', `name` varchar(50) NOT
NULL DEFAULT '', `email` varchar(50) NOT NULL DEFAULT '',
`subject` varchar(100) NOT NULL DEFAULT 'No Subject', `message`
longtext NOT NULL, `timestamp` timestamp NOT NULL DEFAULT
CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `message_read`
enum('Yes','No') NOT NULL DEFAULT 'No', `send_time` datetime NOT
NULL DEFAULT '1970-01-01 00:00:00',
`hide_message` enum('0','1') NOT NULL, `grouper` int(20) DEFAULT
NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT
CHARSET=latin1;
insert into groups (type,date) values ('0',now());
insert into groupmembers (username,group_id) values ('user1',5),('user2',5);
insert into messages (send_to,name,send_time,grouper,message) values
('user1','user2','2019-10-09 19:18:12',5,'hello');
insert into messages (send_to,name,send_time,grouper,message) values
('','user2','2019-10-10 09:18:39',5,'hello');
In mysql 5.6 this query works as anticipated:
select groups.id,send_to,messages.name,max(send_time) from groups
inner join messages on groups.id=messages.grouper left join
groupmembers on groups.id=groupmembers.group_id where
groupmembers.username='user1' group by groups.id ORDER BY
MAX(send_time)
in mysql 5.7 this returns an error
Error Code: 1055. Expression #2 of SELECT list is not in GROUP BY
clause and contains nonaggregated column 'messages.send_to' which is
not functionally dependent on columns in GROUP BY clause; this is
incompatible with sql_mode=only_full_group_by
What do I need to do to get this to work in MySQL 5.7 without adjusting the SQL Mode?
See SQL Fiddle here: http://sqlfiddle.com/#!9/9061da/4
It works as expected on the SQL fiddle as that is running 5.6.
Just a guess...
SELECT DISTINCT g.id
, m.send_to
, m.name
, m.send_time
FROM groups g
JOIN messages m
ON m.grouper = g.id
JOIN
( SELECT send_to
, name
, MAX(send_time) send_time
FROM messages m
GROUP
BY send_to
, name
) x
ON x.send_to = m.send_to
AND x.name = m.name
AND x.send_time = m.send_time
JOIN groupmembers gm
ON gm.group_id = g.id
WHERE gm.username = 'user1'
ORDER
BY send_time;

How to fetch related data between these two tables

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;

selecting multiple rows issue in mysql

I am beginner in sql.
I have two tables users and installments
CREATE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(20) NOT NULL,
`password` varchar(10) NOT NULL,
`father_name` varchar(20) NOT NULL,
`phone` varchar(20) NOT NULL,
`cnic` varchar(20) NOT NULL,
`email` varchar(100) NOT NULL,
`address` varchar(100) NOT NULL,
`introducer` varchar(100) NOT NULL,
`date` date DEFAULT NULL,
`reg_number` varchar(100) DEFAULT NULL,
`installment` int(100) NOT NULL,
`user_level` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `cnic` (`cnic`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
INSERT INTO `users` (`id`, `user_name`, `password`, `father_name`, `phone`, `cnic`, `email`, `address`, `introducer`, `date`, `reg_number`, `installment`, `user_level`) VALUES
(2, 'qaser', 'Qaser1', 'zamarrud', '0312546879', '37406-3140185-1', 'tariq_kareem#yahoo.com', 'street # 6', 'rizwan', '2014-08-20', 'E-002', 3000, 0);
and
CREATE `installments` (
`installment_id` int(11) NOT NULL AUTO_INCREMENT,
`month` date DEFAULT NULL,
`prv_arrear` int(100) NOT NULL,
`amount` int(100) NOT NULL,
`total` int(100) NOT NULL,
`receive` int(100) NOT NULL,
`arrear` int(100) NOT NULL,
`fk_users_id` int(11) NOT NULL,
PRIMARY KEY (`installment_id`),
KEY `fk_users_id` (`fk_users_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
INSERT INTO `installments` (`installment_id`, `month`, `prv_arrear`, `amount`, `total`, `receive`, `arrear`) VALUES
(2, '2014-08-20', 2000, 2500, 4500, 3000, 1500);
when I run the following query I get the first table's record correctly but the second table's record showing NULL values
SELECT * FROM users
LEFT JOIN installments
ON users.id=installments.installment_id
WHERE users.cnic='37406-3140185-1';
Is there anything missing in the above query or is there another way to get the record from both tables simultaneously
id user_name password father_name phone cnic email address introducer date reg_number installment user_level installment_id month prv_arrear amount total receive arrear fk_users_id
2 qaser Qaser1 zamarrud 0312546879 37406-3140185-1 tariq_kareem#yahoo.com street # 6 rizwan 2014-08-20 s-001 3000 0 NULL NULL NULL NULL NULL NULL NULL NULL
I am also using the following query to inserting the record for getting primary key value and insert into foreign key
INSERT INTO `installments`(`id`, `month`, `prv_arrear`, `amount`, `total`, `receive`, `arrear`, fk_users_id)
SELECT NULL,now(),1000,2500,3500,3000,500, id
FROM users
WHERE cnic = '37406-3140190-1'
please help me thanks in advance and sorry if there is something wrong in my question because I am new in sql.
I suspect the right join condition is on fk_users_id. So this might do what you want:
SELECT *
FROM users u LEFT JOIN
installments i
ON u.id = i.fk_users_id
WHERE u.cnic = '37406-3140185-1';

how can i retrieve the child table data from master table in a string value

This is the master table structure
CREATE TABLE IF NOT EXISTS `gf_film` (
`film_id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` int(20) NOT NULL,
`film_name` varchar(100) DEFAULT NULL,
`film_cat` varchar(30) CHARACTER SET latin1 DEFAULT NULL,
`film_plot` longtext,
`film_release_date` date DEFAULT NULL,
`film_post_date` date DEFAULT NULL,
`film_type` enum('Movie','Tv') CHARACTER SET latin1 DEFAULT 'Movie',
`film_feature` enum('Y','N') CHARACTER SET latin1 NOT NULL DEFAULT 'N',
`film_status` enum('ACTIVE','INACTIVE') CHARACTER SET latin1 NOT NULL DEFAULT 'ACTIVE',
`film_modify` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`film_link_value` varchar(200) NOT NULL,
`film_post_link` varchar(255) NOT NULL,
PRIMARY KEY (`film_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=21435 ;
This the child table through which i map between the above gf_film table and lowest gf_actor table
CREATE TABLE IF NOT EXISTS `gf_film_actor` (
`film_id` int(20) NOT NULL,
`actor_id` int(20) NOT NULL,
KEY `film_id` (`film_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
This another table from which i have to reitrieve the actor name
CREATE TABLE IF NOT EXISTS `gf_actor` (
`actor_id` bigint(20) NOT NULL AUTO_INCREMENT,
`actor_name` varchar(100) DEFAULT NULL,
`actor_desc` longtext CHARACTER SET latin1,
PRIMARY KEY (`actor_id`),
UNIQUE KEY `actor_name` (`actor_name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=60963 ;
And this the schema for gf_film_poster
CREATE TABLE IF NOT EXISTS `gf_film_poster` (
`film_id` int(20) NOT NULL,
`website_poster_url` varchar(255) DEFAULT NULL,
`original_poster_url` varchar(255) DEFAULT NULL,
`default_poster_url` varchar(255) DEFAULT 'noposter.gif',
UNIQUE KEY `film_id` (`film_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
After execute this query
SELECT gf_film.film_id ,
film_name ,
DATE_FORMAT(film_release_date,'%d') AS DATE ,
DATE_FORMAT(film_release_date,'%m') AS month_ori,
DATE_FORMAT(film_release_date,'%M') AS MONTH ,
DATE_FORMAT(film_release_date,'%Y') AS YEAR ,
film_release_date ,
film_feature ,
film_modify ,
film_post_link ,
website_poster_url
FROM gf_film
LEFT JOIN gf_film_poster
ON gf_film.film_id=gf_film_poster.film_id
i am getting these results from database
film_id,film_name,date,month_ori,month,year,film_release_date,
film_feature,film_modify,film_post_link,website_poster_url
So i need the another column which contain the actors name and actor_id in one row related to each movies by joing the above query with gf_actor and gf_film_actor
SELECT gf_film.film_id ,
film_name ,
DATE_FORMAT(film_release_date,'%d') AS DATE ,
DATE_FORMAT(film_release_date,'%m') AS month_ori,
DATE_FORMAT(film_release_date,'%M') AS MONTH ,
DATE_FORMAT(film_release_date,'%Y') AS YEAR ,
film_release_date ,
film_feature ,
film_modify ,
film_post_link ,
website_poster_url ,
group_concat(gf_actor.actor_name) AS actors
FROM gf_film
LEFT JOIN gf_film_poster
ON gf_film.film_id=gf_film_poster.film_id
LEFT JOIN gf_film_actor
ON gf_film_actor.film_id = gf_film.film_id
LEFT JOIN gf_actor
ON gf_film_actor.actor_id = gf_actor.actor_id
GROUP BY gf_film.film_id