Data inserted in atomic transaction persists even after rollback - mysql

I am trying to implement this atomic transaction in django and am following this example from the docs. But changes are found to persist despite rollback. I've searched for similar questions and most common cause seems to be catching integrity error inside atomic block, but I am not doing that.
Following is my django code:
def get_lead_alert_data(params):
with transaction.atomic():
with acquire_lead_lock():
caller = params['caller']
via = params['via']
called = params['called']
leadphone = LeadsPhone.objects.filter(phone_number=caller, brokerage__isnull=True).first()
if leadphone:
lead_id = leadphone.lead_id
else:
lead_id = create_lead_from_inbound_call(caller, called)
created, requirement = get_or_create_requirement_from_inbound_call(via, lead_id)
picking_agent = Users.objects.get(phone_mobile=called)
if created:
RequirementAssignment.objects.create(requirement=requirement, agent=picking_agent)
assigned_to = picking_agent.user_name
else:
assigned_requirement = RequirementAssignment.objects.filter(brokerage__active=True,
requirement=requirement).first() #There will be only one such requirement
if not assigned_requirement.agent:
assigned_requirement.agent = picking_agent
assigned_requirement.save()
assigned_to = assigned_requirement.agent.user_name if assigned_requirement else 'nobody'
return {'lead_id': lead_id, 'assigned_to': assigned_to, 'picking_by': picking_agent.user_name}
I also checked resulting logs in mysql and it is indeed calling a rollback, yet the changes persist.
3043 Connect root#localhost on reserve_db_2
3043 Query SET NAMES utf8
3043 Query set autocommit=0
3043 Query set autocommit=1
3043 Query SET SQL_AUTO_IS_NULL = 0
3043 Query set autocommit=0
3043 Query lock table person write, leads write, leads_phones write, leads_emails write, requirements write, tele_phones read
3043 Query SELECT `leads_phones`.`id`, `leads_phones`.`lead_id`, `leads_phones`.`phone_number`, `leads_phones`.`brokerage_id`, `leads_phones`.`created` FROM `leads_phones` WHERE (`leads_phones`.`phone_number` = '9899696089' AND `leads_phones`.`brokerage_id` IS NULL) ORDER BY `leads_phones`.`id` ASC LIMIT 1
3043 Query INSERT INTO `person` (`user_id`, `fullname`, `mobile_no`, `fb_location`, `fb_email`, `fb_aboutme`, `fb_avatar`, `goog_email`, `goog_avatar`, `uploaded_avatar`, `first_name`, `last_name`, `description`, `address`, `is_admin`, `reviewer_badge`, `title`, `phone_home`, `phone_work`, `phone_other`, `phone_fax`, `status`, `address_street`, `address_city`, `address_region_id`, `address_country`, `address_postalcode`, `created`, `last_updated`, `created_by`, `modified_by`, `deleted`) VALUES (NULL, '', NULL, '', '', '', '', '', '', '', '', '', '', '', NULL, '1ST TIME REVIEWER', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '2014-09-06 11:12:03', '2014-09-06 11:12:03', '', NULL, 0)
140906 16:40:05 3043 Query INSERT INTO `leads` (`id`, `date_entered`, `date_modified`, `modified_user_id`, `created_by`, `description`, `deleted`, `assigned_user_id`, `salutation`, `first_name`, `middle_name`, `last_name`, `title`, `department`, `do_not_call`, `primary_email_address`, `secondary_email_address`, `phone_home`, `phone_mobile`, `phone_work`, `phone_other`, `phone_fax`, `primary_address_street`, `primary_address_city`, `primary_address_state`, `primary_address_postalcode`, `primary_address_country`, `alt_address_street`, `alt_address_city`, `alt_address_state`, `alt_address_postalcode`, `alt_address_country`, `converted`, `refered_by`, `lead_source_description`, `status`, `status_description`, `reports_to_id`, `residence_phone`, `citizenship`, `primary_address_street_by_agent`, `office_location`, `owned_rented`, `owned_rented_by_agent`, `unique_id`, `reason_for_status_change`, `annual_income`, `annual_income_by_agent`, `designation`, `executive_level`, `executive_level_by_agent`, `present_company`, `website`, `lead_type_fav`, `lead_type_c`, `facebook_url`, `linkedin_url`, `twitter_url`, `google_plus_url`, `assigned_user_date`, `worked_by_tele`, `worked_by_sales`, `off_campaign_id`, `activity_done`, `activity_completed`, `queue_name`, `queue_description`, `history_notes`, `lead_category`, `trans_type`, `potential`, `referral_remark`, `referral_name`, `referral_no`, `referral_email`, `primary_secondary_lead`, `met_face_to_face`, `met_site_visit`, `met_final_negotiation`, `total_met`, `is_duplicate`, `is_duplicate_date`, `queue_abort_remark`, `referer_url`, `landing_url`, `leadpage_url`, `lead_projects`, `lead_projects_ids`, `lead_max_budget`, `lead_source`, `person_id`, `brokerage_id`, `lead_parent_id`) VALUES ('1440bb40-4f8a-4f87-917f-6aca0c758711', NULL, NULL, NULL, '', NULL, 0, '', NULL, NULL, NULL, '', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '', NULL, NULL, '', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 315601, NULL, NULL)
3043 Query INSERT INTO `leads_phones` (`lead_id`, `phone_number`, `brokerage_id`, `created`) VALUES ('1440bb40-4f8a-4f87-917f-6aca0c758711', '9899696089', NULL, '2014-09-06 11:12:03')
3043 Query SELECT `tele_phones`.`id`, `tele_phones`.`source_id`, `tele_phones`.`project_id`, `tele_phones`.`locality_id`, `tele_phones`.`cluster_id`, `tele_phones`.`city_id` FROM `tele_phones` WHERE `tele_phones`.`id` = '3314892' LIMIT 21
3043 Query INSERT INTO `requirements` (`id`, `req_unique_id`, `lead_id`, `user_id`, `name`, `date_entered`, `date_modified`, `created_by`, `modified_user_id`, `assigned_user_id`, `deleted`, `req_type`, `category`, `bhk`, `unit_type`, `construction_phase`, `main_entrance_facing`, `balcony_facing`, `furnish_state`, `plc`, `locality`, `cluster`, `city`, `region`, `project`, `plot_area`, `super_area`, `price_sft_syd`, `price`, `total_price`, `cash_in_hand`, `need_loan`, `description`, `is_active_req`) VALUES ('63494d0d-88f8-44f5-816c-af4bb5ec439e', NULL, '1440bb40-4f8a-4f87-917f-6aca0c758711', NULL, '', NULL, NULL, '', '', '', NULL, '', '', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1, 1, 1, NULL, 1, NULL, NULL, NULL, NULL, NULL, NULL, 1, NULL, 1)
3043 Query unlock tables
3043 Query rollback
3043 Query set autocommit=1

Ohk, I checked out mysql docs. Looks like unlocking tables implicitly causes a commit if there are any locked tables, which there are in my case. Will have to find a workaround.

Related

Select subscriptions based on user id

I have these MySQL tables where I want to store user subscriptions:
CREATE TABLE subscription (
`id` int(11) NOT NULL AUTO_INCREMENT,
`amount` decimal(19,2) DEFAULT NULL,
`created_at` datetime(6) DEFAULT NULL,
`currency` varchar(20) DEFAULT NULL,
`duration` bigint(20) DEFAULT NULL,
`end_at` datetime(6) DEFAULT NULL,
`error` varchar(200) DEFAULT NULL,
`order_id` int(11) DEFAULT NULL,
`product` varchar(20) DEFAULT NULL,
`run_at` datetime(6) DEFAULT NULL,
`start_at` datetime(6) DEFAULT NULL,
`status` varchar(20) DEFAULT NULL,
`updated_at` datetime(6) DEFAULT NULL,
`title` varchar(20) DEFAULT NULL,
`parent_transaction_id` int(11) DEFAULT NULL,
`parent_transactionId` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23443556 DEFAULT CHARSET=latin1;
INSERT INTO subscription (`id`, `amount`, `created_at`, `currency`, `duration`, `end_at`, `error`, `order_id`, `product`, `run_at`, `start_at`, `status`, `updated_at`, `title`, `parent_transaction_id`, `parent_transactionId`) VALUES
('122', '3333.00', '2020-10-31 19:41:16.622386', 'USD', '1000', '2020-10-31 19:41:16.622386', 'error message', '122', 'error message', '2020-10-31 19:41:16.622386', '2020-10-31 19:41:16.622386', 'active', '2020-10-31 19:41:16.622386', 'title', '443', '5544');
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(255) DEFAULT NULL,
`enabled` tinyint(4) DEFAULT NULL,
`encrypted_password` varchar(255) DEFAULT NULL,
`first_name` varchar(255) DEFAULT NULL,
`last_name` varchar(255) DEFAULT NULL,
`role` varchar(25) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=153 DEFAULT CHARSET=latin1;
INSERT INTO `users` (`id`, `email`, `enabled`, `encrypted_password`, `first_name`, `last_name`, `role`) VALUES
('192', 'test#mail.com', '1', '$2a$31$ovShEKleI3Yk2vgjAIF5mO4HkGWXcTSKPMTaj7rFN4KAtlEz0f/ay', 'test', 'test', 'ROLE_CLIENT');
CREATE TABLE `orders` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`city` varchar(30) DEFAULT NULL,
`CONTENT` text DEFAULT NULL,
`country` varchar(50) DEFAULT NULL,
`created_at` datetime(6) DEFAULT NULL,
`discount` decimal(8,2) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`first_name` varchar(50) DEFAULT NULL,
`grand_total` decimal(8,2) DEFAULT NULL,
`item_discount` decimal(8,2) DEFAULT NULL,
`last_name` varchar(50) DEFAULT NULL,
`line1` varchar(50) DEFAULT NULL,
`line2` varchar(50) DEFAULT NULL,
`middle_name` varchar(50) DEFAULT NULL,
`mobile` varchar(15) DEFAULT NULL,
`promo` varchar(100) DEFAULT NULL,
`province` varchar(50) DEFAULT NULL,
`session_id` int(11) DEFAULT NULL,
`shipping` decimal(8,2) DEFAULT NULL,
`status` varchar(100) DEFAULT NULL,
`sub_total` decimal(8,2) DEFAULT NULL,
`tax` decimal(8,2) DEFAULT NULL,
`token` varchar(100) DEFAULT NULL,
`total` decimal(8,2) DEFAULT NULL,
`updated_at` datetime(6) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL,
`phone` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=65 DEFAULT CHARSET=latin1;
INSERT INTO `orders` (`id`, `city`, `CONTENT`, `country`, `created_at`, `email`, `first_name`, `grand_total`, `item_discount`, `last_name`, `status`, `sub_total`, `tax`, `total`, `user_id`) VALUES
('122', 'city', 'test context', 'USA', '2021-05-25 12:40:23.793779', 'test#email.com', 'first name', '100', '10', 'last name', 'active', '20', '10', '200', '192');
INSERT INTO `orders` (`id`, `city`, `CONTENT`, `country`, `created_at`, `email`, `first_name`, `grand_total`, `item_discount`, `last_name`, `status`, `sub_total`, `tax`, `total`, `user_id`) VALUES
('124', 'city', 'test context', 'USA', '2021-05-25 12:40:23.793779', 'test#email.com', 'first name', '100', '10', 'last name', 'active', '20', '10', '200', '192');
CREATE TABLE `order_item` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`discount` decimal(19,2) DEFAULT NULL,
`order_id` int(11) DEFAULT NULL,
`price` decimal(19,2) DEFAULT NULL,
`product_id` int(11) DEFAULT NULL,
`quantity` int(11) DEFAULT NULL,
`sku` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
SQLFiddle: http://sqlfiddle.com/#!9/1d6ba6/1
I use this SQL query with 2 JOINS to get the subsriptions:
SELECT *
FROM subscription
INNER JOIN Orders ON subscription.order_id = Orders.id AND subscription.id = 122
INNER JOIN users ON users.id = users.id AND Orders.user_id = 192
ORDER BY subscription.id;
As you can see I use 2 params in order to make 2 JOINS. Is it possible just to send the user id as a param and based from the match Orders.id AND subscription.id to get the subscription records that are found?
Your query didn't make sense, with ON Clause that links orders and user
So you can achieve the same result, without the user
SELECT *
FROM subscription
INNER JOIN Orders ON subscription.order_id = Orders.id AND subscription.id = 122
INNER JOIN users ON users.id = Orders.user_id
ORDER BY subscription.id;
btw the user of aliases can help keep more overview
SELECT *
FROM subscription s
INNER JOIN Orders o ON s.order_id = o.id AND s.id = 122
INNER JOIN users u ON u.id = o.user_id
ORDER BY s.id;

Mysql join multiple tables with one select phrase

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;

Query was working in MYSQL but not MYSQLI

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.

SQL Query for amount of products sold within dates?

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;

mysql join to get data from 3 tables, addition of time and int getting error as multiple of summed value

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.