mysql select sum if value equal set sum to 0 - mysql

I am developing wms software, I write SQL to calculate items stock in warehouse. in the table I record out and in stock values if go out then negative value if receive in the warehouse item value is positive. In the same table I record inventory control too. So, I need SQLquery with sum all items stock and if have inventory control with same id of items, then if found SUM expression an inventory control row , The SUM expression need to equal with inventory control value and the resume summing.
So when SUM expression run into control row, I need to sum from the latest control to a defined date
information:
-- Server version: 8.0.18
-- PHP Version: 7.3.11
Create table :
CREATE TABLE `magacin_zalihe` (
`magacin_zalihe_id` int(11) NOT NULL,
`magacin_zalihe_magacin` varchar(8) COLLATE utf8mb4_general_ci DEFAULT NULL,
`magacin_zalihe_materijali_id` int(11) NOT NULL,
`magacin_zalihe_materijali_sifra` varchar(100) COLLATE utf8mb4_general_ci NOT NULL,
`magacin_zalihe_materijali_naziv` varchar(100) COLLATE utf8mb4_general_ci NOT NULL,
`magacin_zalihe_materijali_kolicina` decimal(10,2) NOT NULL,
`magacin_zalihe_materijali_tip` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL,
`magacin_zalihe_materijali_tip2` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL,
`magacin_zalihe_materijali_mera` varchar(10) COLLATE utf8mb4_general_ci DEFAULT NULL,
`magacin_zalihe_materijali_pakovanje` decimal(10,2) NOT NULL,
`magacin_zalihe_materijali_paleta` decimal(10,2) NOT NULL,
`magacin_zalihe_materijali_tezina` decimal(10,2) NOT NULL,
`magacin_zalihe_materijali_opis` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`magacin_zalihe_magacioner` varchar(100) COLLATE utf8mb4_general_ci NOT NULL,
`magacin_zalihe_datum` date NOT NULL,
`magacin_zalihe_narudzba` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`magacin_zalihe_vrsta` varchar(50) COLLATE utf8mb4_general_ci DEFAULT NULL,
`magacin_zalihe_kor_modif` varchar(50) COLLATE utf8mb4_general_ci DEFAULT NULL,
`magacin_zalihe_dat_modif` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
INSERT:
INSERT INTO `magacin_zalihe` (`magacin_zalihe_id`, `magacin_zalihe_magacin`, `magacin_zalihe_materijali_id`, `magacin_zalihe_materijali_sifra`, `magacin_zalihe_materijali_naziv`, `magacin_zalihe_materijali_kolicina`, `magacin_zalihe_materijali_tip`, `magacin_zalihe_materijali_tip2`, `magacin_zalihe_materijali_mera`, `magacin_zalihe_materijali_pakovanje`, `magacin_zalihe_materijali_paleta`, `magacin_zalihe_materijali_tezina`, `magacin_zalihe_materijali_opis`, `magacin_zalihe_magacioner`, `magacin_zalihe_datum`, `magacin_zalihe_narudzba`, `magacin_zalihe_vrsta`, `magacin_zalihe_kor_modif`, `magacin_zalihe_dat_modif`) VALUES
(24, '1000', 132, '1110212612001', 'POLYSTICK IR-XE 36\" IT GRANULAR', '200.00', '2', '0', 'm2', '18.28', '20.00', '23.50', '', 'rsolar', '2021-05-20', '', 'utovar', 'rsolar', '2021-05-20 12:26:50'),
(25, '1000', 132, '1110212612001', 'POLYSTICK IR-XE 36\" IT GRANULAR', '250.00', '2', '0', 'm2', '18.28', '20.00', '23.50', '', 'rsolar', '2021-05-20', '', 'utovar', 'rsolar', '2021-05-20 12:26:50'),
(26, '1000', 132, '1110212612001', 'POLYSTICK IR-XE 36\" IT GRANULAR', '50.00', '2', '0', 'm2', '18.28', '20.00', '23.50', '', 'rsolar', '2021-05-20', '', 'utovar', 'rsolar', '2021-05-20 12:26:50'),
(27, '1000', 120, '1124314522021', 'POLIFLEX MINERAL P4,5', '-50.00', '2', '0', 'm2', '10.00', '23.00', '43.00', '', 'rsolar', '2021-05-19', '', 'istovar', 'rsolar', '2021-05-21 05:56:59'),
(28, '1000', 132, '1110212612001', 'POLYSTICK IR-XE 36\" IT GRANULAR', '-25.00', '2', '0', 'm2', '18.28', '20.00', '23.50', '', 'rsolar', '2021-05-19', '', 'istovar', 'rsolar', '2021-05-21 05:56:59'),
(29, '1000', 120, '1124314522021', 'POLIFLEX MINERAL P4,5', '100.00', '2', '0', 'm2', '10.00', '23.00', '43.00', '', 'rsolar', '2021-05-21', '', 'utovar', 'rsolar', '2021-05-21 08:03:28'),
(30, '1000', 120, '1124314522021', 'POLIFLEX MINERAL P4,5', '50.00', '2', '0', 'm2', '10.00', '23.00', '43.00', '', 'rsolar', '2021-05-22', '', 'utovar', 'rsolar', '2021-05-21 08:05:10'),
(31, '1000', 120, '1124314522021', 'POLIFLEX MINERAL P4,5', '-100.00', '2', '0', 'm2', '10.00', '23.00', '43.00', '', 'dbence', '2021-05-19', '', 'istovar', 'dbence', '2021-05-21 08:25:14'),
(32, '1000', 120, '1124314522021', 'POLIFLEX MINERAL P4,5', '-80.00', '2', '0', 'm2', '10.00', '23.00', '43.00', '', 'dbence', '2021-05-21', '', 'istovar', 'dbence', '2021-05-21 08:31:13'),
(33, '1000', 52, '1122432441001', 'FIMAL A4/P', '-80.00', '2', '0', 'm2', '7.50', '20.00', '30.00', '', 'dbence', '2021-05-21', '', 'istovar', 'dbence', '2021-05-21 08:31:13'),
(34, '100', 125, '1124331321001', 'POLIFLEX PF4/S', '-100.00', '2', '0', 'm2', '10.00', '23.00', '44.00', '', 'rsolar', '2021-05-21', '', 'istovar', 'rsolar', '2021-05-21 08:57:27'),
(35, '1000', 121, '1123320261001', 'POLIFLEX P2', '-100.00', '2', '0', 'm2', '10.00', '30.00', '28.00', '', 'rsolar', '2021-05-21', '123456-2021', 'istovar', 'rsolar', '2021-05-21 12:09:24'),
(36, '1000', 132, '1110212612001', 'POLYSTICK IR-XE 36\" IT GRANULAR', '-400.00', '2', '0', 'm2', '18.28', '20.00', '23.50', '', 'rsolar', '2021-05-21', '223564-2021', 'istovar', 'rsolar', '2021-05-21 12:10:11'),
(37, '126', 4, '1111152042050', 'BÁZIS FATA CSUPASZ', '100.00', '2', '0', 'm2', '20.00', '64.00', '9.00', '', 'rsolar', '2021-05-21', '', 'utovar', 'rsolar', '2021-05-21 12:51:55'),
(38, '100', 132, '1110212612001', 'POLYSTICK IR-XE 36\" IT GRANULAR', '10.00', '2', '0', 'm2', '18.28', '20.00', '23.50', '', 'rsolar', '2021-05-26', '', 'utovar', 'rsolar', '2021-05-31 10:18:58'),
(39, '100', 132, '1110212612001', 'POLYSTICK IR-XE 36\" IT GRANULAR', '-100.00', '2', '0', 'm2', '18.28', '20.00', '23.50', '', 'rsolar', '2021-05-31', '', 'istovar', 'rsolar', '2021-05-31 11:15:15'),
(40, '100', 24, '1110110002001', 'CARTABIT 350 1000M', '-150.00', '2', '0', 'm2', '1050.00', '1.00', '380.00', '', 'rsolar', '2021-05-31', '', 'istovar', 'rsolar', '2021-05-31 11:16:30'),
(41, '100', 131, '1111311852001', 'POLYESTER 260 P11', '10.00', '2', '0', 'm2', '13.50', '36.00', '25.90', '', 'rsolar', '2021-06-23', '', 'utovar', 'rsolar', '2021-07-05 06:12:45'),
(42, '100', 132, '1110212612001', 'POLYSTICK IR-XE 36\" IT GRANULAR', '200.00', '2', '0', 'm2', '18.28', '20.00', '23.50', '', 'rsolar', '2021-06-23', 'INV - 2021-06-23', 'inventar', 'rsolar', '2021-07-05 06:12:45'),
(43, '100', 31, '1111141002007', 'CARTONFELTRO BITUMATO T350 2x1000M', '100.00', '2', '0', 'm2', '1050.00', '2.00', '370.00', '', 'rsolar', '2021-06-23', '', 'utovar', 'rsolar', '2021-07-05 06:12:45'),
(44, '100', 132, '1110212612001', 'POLYSTICK IR-XE 36\" IT GRANULAR', '-85.00', '2', '0', 'm2', '18.28', '20.00', '23.50', '', 'rsolar', '2021-05-31', '', 'istovar', 'rsolar', '2021-07-05 06:13:07'),
(45, '100', 29, '1111141002005', 'CARTONFELTRO BITUMATO T 300 MARCEGAGLIA', '-1.00', '2', '0', 'm2', '1020.00', '1.00', '346.00', '', 'rsolar', '2021-05-31', '', 'istovar', 'rsolar', '2021-07-05 06:13:07'),
(46, '100', 120, '1124314522021', 'POLIFLEX MINERAL P4,5', '100.00', '2', '0', 'm2', '10.00', '23.00', '43.00', '', 'rsolar', '2021-07-05', '', 'utovar', 'rsolar', '2021-07-06 07:49:55');
First column as primary key and auto increment
needed output:
Output
in output needed 200 because the last row is control row and contains stock value of 200
my sql query:
SELECT SUM( t1.magacin_zalihe_materijali_kolicina ) AS temp,t1.* FROM magacin_zalihe AS t1 WHERE t1.magacin_zalihe_magacin='100' AND t1.magacin_zalihe_datum <= '2021-07-07' GROUP BY t1.magacin_zalihe_materijali_id
t1.magacin_zalihe_vrsta contains 1 row with value 'inventar' this is control for warehouse. From this value needed to sum stock values. If have more than 1 row of control we needed to sum from latest control row.
my sql query to try but not works:
SELECT SUM(IF( t1.magacin_zalihe_vrsta != 'inventar', t1.magacin_zalihe_materijali_kolicina, #temp := t1.magacin_zalihe_materijali_kolicina )) AS temp,t1.* FROM magacin_zalihe AS t1 WHERE t1.magacin_zalihe_magacin='100' AND t1.magacin_zalihe_datum <= '2021-07-07' GROUP BY t1.magacin_zalihe_materijali_id
Thanks,
Robert

SELECT IF (
p1.temp = 0,
p1.magacin_zalihe_materijali_kolicina,
p1.temp
) as temp_delta
FROM (
SELECT SUM(
IF(
t1.magacin_zalihe_vrsta != 'inventar',
0,
#temp := t1.magacin_zalihe_materijali_kolicina
)
) AS temp,
t1.*
FROM magacin_zalihe AS t1
WHERE t1.magacin_zalihe_magacin = '100'
AND t1.magacin_zalihe_datum <= '2021-07-07'
GROUP BY t1.magacin_zalihe_materijali_id
) as p1;

Related

Absenteeism query showing date as employee is absent

I'm trying to generate an absenteeism query with the date employees were absent.
Table "ci_admin":
DROP TABLE IF EXISTS `ci_admin`;
CREATE TABLE `ci_admin` (
`admin_id` int(11) NOT NULL AUTO_INCREMENT,
`admin_role_id` int(11) NOT NULL,
`department_id` int(11) NOT NULL,
`firstname` varchar(255) NOT NULL,
`lastname` varchar(255) NOT NULL,
`emp_photo` varchar(255) NOT NULL,
PRIMARY KEY (`admin_id`)
);
INSERT INTO `ci_admin` VALUES ('1', '20170079320', '1', '1', 'AHMED', 'MAKOKHA', '125.jpg'), ('2', '20170079321', '1', '1', 'MARIAM', 'AHMED', '2.png'), ('3', '20170080091', '2', '3', 'SALAHUDEEN', 'AHMED', '3.png'), ('4', '20130080083', '2', '2', 'NURU', 'ABBAS', '4.png'), ('5', '20170080092', '5', '1', 'HIRBO', 'ALI', '5.png');
Table "ci_department":
DROP TABLE IF EXISTS `ci_department`;
CREATE TABLE `ci_department` (
`department_id` int(11) NOT NULL AUTO_INCREMENT,
`admin_id` int(11) NOT NULL,
`department_name` varchar(225) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`department_id`)
);
INSERT INTO `ci_department` VALUES ('1', 'INFORMATION COMMUNICATION TECHNOLOGY'), ('2', 'HUMAN RESOURCE'), ('3', 'PROCUREMENT');
Table "ci_admnin_roles":
DROP TABLE IF EXISTS `ci_admin_roles`;
CREATE TABLE `ci_admin_roles` (
`admin_role_id` int(11) NOT NULL AUTO_INCREMENT,
`admin_role_title` varchar(30) CHARACTER SET utf8 NOT NULL,
PRIMARY KEY (`admin_role_id`)
);
INSERT INTO `ci_admin_roles` VALUES ('1', 'Super Admin'), ('2', 'ADMIN'), ('3', 'ACCOUNTANT'), ('4', 'OPERATOR'), ('5', 'IT OFFICER'), ('6', 'APPLICANT');
Table "ci_attendance":
DROP TABLE IF EXISTS `ci_attendance`;
CREATE TABLE `ci_attendance` (
`attendance_id` int(11) NOT NULL AUTO_INCREMENT,
`attendance_date` date NOT NULL,
`admin_id` int(11) NOT NULL,
`check_in` time NOT NULL,
`check_out` time NOT NULL,
PRIMARY KEY (`attendance_id`)
);
INSERT INTO `ci_attendance` VALUES ('1', '2022-12-22', '1', '08:15:00', '18:30:00'), ('2', '2022-12-22', '2', '09:00:00', '18:30:00'), ('3', '2022-12-22', '3', '10:00:00', '19:30:00'), ('4', '2020-02-21', '1', '10:30:00', '20:30:00'), ('5', '2023-12-12', '2', '10:00:00', '21:30:00'), ('6', '2023-12-23', '3', '10:00:00', '22:30:00'), ('7', '2023-12-22', '3', '08:00:00', '17:30:00'), ('8', '2021-02-21', '2', '09:00:00', '18:30:00'), ('9', '2024-12-12', '3', '10:00:00', '19:30:00'), ('10', '2024-12-23', '1', '10:00:00', '20:30:00'), ('11', '2024-12-22', '2', '10:00:00', '21:30:00'), ('12', '2022-02-21', '3', '10:00:00', '22:30:00'), ('13', '2022-12-12', '1', '08:00:00', '17:30:00'), ('14', '2022-12-23', '3', '09:00:00', '18:30:00'), ('15', '2022-12-02', '2', '10:00:00', '19:30:00'), ('16', '2020-02-21', '1', '09:45:00', '22:00:00'), ('17', '2023-12-12', '2', '10:00:00', '21:30:00'), ('18', '2020-12-23', '3', '10:00:00', '22:30:00'), ('19', '2023-12-22', '1', '08:00:00', '17:30:00'), ('20', '2021-02-21', '2', '09:00:00', '18:30:00'), ('21', '2024-12-03', '2', '10:00:00', '19:30:00'), ('22', '2021-12-23', '1', '10:00:00', '20:30:00'), ('23', '2024-10-22', '3', '10:00:00', '21:30:00'), ('24', '2022-02-21', '3', '10:00:00', '22:30:00'), ('25', '2022-12-12', '1', '08:00:00', '17:30:00'), ('26', '2022-09-23', '2', '09:00:00', '18:30:00'), ('27', '2022-12-22', '4', '10:00:00', '19:30:00'), ('28', '2020-02-21', '1', '10:00:00', '20:30:00'), ('29', '2023-04-12', '5', '10:00:00', '21:30:00'), ('30', '2023-12-03', '4', '10:00:00', '22:30:00'), ('31', '2023-12-22', '1', '08:00:00', '17:30:00'), ('32', '2021-02-21', '5', '09:00:00', '18:30:00'), ('33', '2024-12-12', '4', '10:00:00', '19:30:00');
My current MySQL query which displays employees who are absent does not include the date when the employee is absent:
SELECT ci_admin.emp_no,
ci_admin.firstname,
ci_admin.lastname,
ci_admin.emp_photo,
ci_department.department_name,
ci_admin_roles.admin_role_title
FROM ci_admin
INNER JOIN ci_department ON ci_department.department_id = ci_admin.department_id
INNER JOIN ci_admin_roles on ci_admin_roles.admin_role_id=ci_admin.admin_role_id
WHERE ci_admin.is_deleted = 0
AND ci_admin.admin_id NOT IN (SELECT ci_attendance.admin_id
FROM ci_attendance
WHERE ci_attendance.attendance_date BETWEEN '2023-01-01' AND '2023-01-31')
ORDER BY ci_admin.department_id
I want the specific date that an employee is absent to be included in my query. How can I modify my query?

Mysql tree structure get count of childs with level using recursive query

I am using WITH RECURSIVE using UNION ALL mysql query to get level of each node and it is working fine. Now I just want to get count of childs for each node.
WITH RECURSIVE generation AS
(SELECT id, name, parent_id, 0 AS generation_number FROM users WHERE parent_id IS NULL
UNION ALL
SELECT
child.name,
child.parent_id
generation_number+1 AS generation_number
FROM users child
JOIN generation g ON g.id = child.parent_id )
SELECT id, name, parent_id, generation_number FROM generation;
Above MySql query returning below table as result excepting last column i.e. "Count of child". What need to do in same query to get count of (Union All) childs.
id
name
parent_id
generation/level
Count of child
1
A
0
0
2
2
B
1
1
1
3
C
1
1
0
4
D
2
2
2
5
E
4
3
0
6
F
4
3
0
Here is source data for creating table
CREATE TABLE `users` (
`id` bigint(20) UNSIGNED NOT NULL,
`sponsor_id` bigint(20) DEFAULT NULL,
`parent_id` bigint(20) DEFAULT NULL,
`user_id` bigint(20) DEFAULT NULL,
`name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`phone` varchar(15) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`image` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`status` enum('Active','Inactive','Deleted') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'Active',
`amount` decimal(8,2) NOT NULL DEFAULT 0.00,
`created_at` timestamp NOT NULL DEFAULT current_timestamp(),
`updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`id`, `sponsor_id`, `parent_id`, `user_id`, `name`, `email`, `phone`, `password`, `image`, `status`, `amount`, `created_at`, `updated_at`) VALUES
(1, NULL, NULL, NULL, 'a', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:42:09', '2022-10-12 04:42:09'),
(2, NULL, 1, NULL, 'b', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:43:34', '2022-10-12 04:43:34'),
(3, NULL, 1, NULL, 'c', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:43:34', '2022-10-12 04:43:34'),
(4, NULL, 1, NULL, 'e', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:47:31', '2022-10-12 04:47:31'),
(5, NULL, 2, NULL, 'f', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:47:31', '2022-10-12 04:47:31'),
(6, NULL, 2, NULL, 'g', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:47:31', '2022-10-12 04:47:31'),
(7, NULL, 2, NULL, 'h', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:47:31', '2022-10-12 04:47:31'),
(8, NULL, 3, NULL, 'i', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:47:31', '2022-10-12 04:47:31'),
(9, NULL, 3, NULL, 'j', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:47:31', '2022-10-12 04:47:31'),
(10, NULL, 3, NULL, 'k', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:47:31', '2022-10-12 04:47:31'),
(11, NULL, 4, NULL, 'l', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(12, NULL, 4, NULL, 'm', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(13, NULL, 4, NULL, 'n', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(14, NULL, 5, NULL, 'o', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(15, NULL, 5, NULL, 'p', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(16, NULL, 6, NULL, 'q', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(17, NULL, 6, NULL, 'r', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(18, NULL, 7, NULL, 's', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(19, NULL, 8, NULL, 't', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(20, NULL, 8, NULL, 'u', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(21, NULL, 8, NULL, 'v', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(22, NULL, 9, NULL, 'w', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(23, NULL, 9, NULL, 'x', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(24, NULL, 10, NULL, 'y', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23'),
(25, NULL, 10, NULL, 'z', '', '', '', NULL, 'Active', '0.00', '2022-10-12 04:50:23', '2022-10-12 04:50:23');
I think you can simply use COUNT and JOIN.
SELECT g1.id, g1.name, g1.parent_id, g1.generation_number,COALESCE(g2.CountOfChild,0)
FROM generation g1 LEFT JOIN
(SELECT parent_id,COUNT(*) CountOfChild FROM generation GROUP BY parent_id) g2
ON g1.id=g2.parent_id;

What should I change not to get error 1136 in MySQL?

CREATE TABLE IF NOT EXISTS `mushu`.`episode` (
`id_episode` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`number` VARCHAR(5) NOT NULL,
`name` VARCHAR(50) NOT NULL,
`episode_length` VARCHAR(10) NOT NULL,
`location` VARCHAR(150) NOT NULL,
`season_id_season` INT UNSIGNED NOT NULL,
`season_tv_show_id_tv_show` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id_episode`, `season_id_season`, `season_tv_show_id_tv_show`),
INDEX `fk_episode_season1_idx` (`season_id_season` ASC,
`season_tv_show_id_tv_show` ASC) VISIBLE,
CONSTRAINT `fk_episode_season1`
FOREIGN KEY (`season_id_season` , `season_tv_show_id_tv_show`)
REFERENCES `mushu`.`season` (`id_season` , `tv_show_id_tv_show`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
INSERT INTO episode VALUES
(1, '1', 'Pilot', '45 min', 'E:\MuShu', '1', '1'),
(2, '2', 'Another Trip Around the Sun', '45 min', 'E:\MuShu', '1', '1'),
(3, '1', 'Pilot', '43 min', 'E:\MuShu', '1', '2'),
(4, '2', 'A Stray Howl', '43 min', 'E:\MuShu', '1', '2'),
(5, '3', 'Eight Slim Grins', '43 min', 'E:\MuShu', '1', '2'),
(6, '1', 'Burnt Food', '42 min', 'E:\MuShu', '1', '3'),
(7, '2', 'Mount Rushmore', '42 min', 'E:\MuShu', '1', '3'),
(8, '1', 'Impact', '56 min', 'E:\MuShu', '1', '4'),
(9, '1', 'Pilot', '44 min', 'E:\MuShu', '1', '5'),
(10, '2', 'The Lorelais First Day at Chilton', '44 min', 'E:\MuShu', '1', '5'),
(11, '1', 'Pilot', '42 min', 'E:\MuShu', '1', '6'),
(12, '2', 'Independence Day', '42 min', 'E:\MuShu', '1', '6'),
(13, '3', 'Comrades in Arms', '42 min', 'E:\MuShu', '1', '6'),
(14, '1', 'Pilot', '22 min', 'E:\MuShu', '1', '7'),
(15, '2', 'Rockets, Communists and the Dewey Decimal System', '22 min', 'E:\MuShu', '1', '7'),
(16, '1', 'Always and Forever', '45 min', '1', '8'),
(17, '1', 'Pilot', '43 min', '1', '9'),
(18, '2', 'Its All Her Fault', '1', '9'),
(19, '1', 'Pilot', '41 min', '1', '10'),
(20, '2', 'The Jenna Thing', '41 min', '1', '10');
Error Code: 1136. Column count doesn't match value count at row 16.
How do I solve this error?
Some values a 7 long others are 6. They all must be the same (7). If you look at row 16 like the error says you'll see.

View returning 0 rows but same query returns 63 rows

I am having strange results in mysql. I have a view which when executed returns 0 rows but taking the query out of the VIEW and executing it as it is returns 63 rows.
This was working properly but ever since I have migrated the code to AWS RDS I am having this error. I am not able to make any sense why this is happening?
I don't even get any error just the 0 rows being returned.
From here on I am just writing random words because I am not able to post the question as the line of code is more. THis is really annnoying ahh darn I made a typo it must be annoying and not annnoying. My bad I hope you all arent mad at this, I am at my wits end how much more should I type?
I am unable to create a sqlfiddle as the request is too large, I am creating a fiddle like dump if you guys want to recreate it on on your end.
I have also discovered that removing the left join from the VIEW returns results.
The fiddle like dump below returns 3 rows when running the select but the VIEW returns 0 rows
CREATE TABLE `users` (
`id` int(11) NOT NULL,
`user_type` enum('admin','landlord','renter') NOT NULL,
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`email_id` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`company_name` varchar(255) NOT NULL,
`street_address` varchar(255) NOT NULL,
`city` varchar(100) NOT NULL,
`state` varchar(100) NOT NULL,
`zip` varchar(100) NOT NULL,
`phone_no` varchar(50) NOT NULL,
`bank_name` varchar(100) NOT NULL,
`billing_address` varchar(255) NOT NULL,
`name_on_card` varchar(255) NOT NULL,
`bank_account_number` varchar(100) NOT NULL,
`bank_routing_no` varchar(100) NOT NULL,
`has_payzang_account` int(11) NOT NULL DEFAULT '0',
`payzang_api` varchar(255) DEFAULT '',
`payzang_gateway_url` varchar(255) NOT NULL,
`pending_amount` decimal(18,2) DEFAULT NULL COMMENT 'pending key bank(rent deduct) amount for landlord',
`is_active` int(11) NOT NULL COMMENT '0-inactive/1-active',
`is_assigned` int(11) NOT NULL DEFAULT '0' COMMENT 'Is this user assigned to any property',
`assigned_to` int(11) NOT NULL COMMENT 'assigned to which landlord id',
`is_set_up_new_password` enum('0','1') NOT NULL DEFAULT '0' COMMENT 'When admin/landlord adds renter or admin adds landlord an email with id+password is sent to the user. The first time they sign in, it should prompt them to change password page. So this field indicating whether user set up their own password',
`created_at` datetime NOT NULL,
`modified_at` datetime NOT NULL,
`created_by` int(11) NOT NULL,
`modified_by` int(11) NOT NULL,
`is_deleted` int(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `users` (`id`, `user_type`, `first_name`, `last_name`, `email_id`, `password`, `company_name`, `street_address`, `city`, `state`, `zip`, `phone_no`, `bank_name`, `billing_address`, `name_on_card`, `bank_account_number`, `bank_routing_no`, `has_payzang_account`, `payzang_api`, `payzang_gateway_url`, `pending_amount`, `is_active`, `is_assigned`, `assigned_to`, `is_set_up_new_password`, `created_at`, `modified_at`, `created_by`, `modified_by`, `is_deleted`) VALUES
(1, 'admin', 'Super', 'Admin', 'email#email.com', '$2y$10$NwcR0ObYeUM.OfjgMOaajeH6bjjuMTpU2GnF0h623SER37AFk8K9i', 'UIGJ', 'DJ Road', 'Kolkatr', '20', '522236', '(987) 963-9658', '', '', '', '', '', 1, 'xxxxxxxxxxxxxxxxxxxxx', 'https://payzang.transactiongateway.com/api/v2/three-step', '0.00', 1, 0, 0, '1', '2017-06-02 13:56:34', '2017-06-02 13:56:34', 0, 0, 0),
(2, 'landlord', 'Denise', 'Supplee', 'email1#email.com', '$2y$10$PATxnED1/lqr1t7epLJiwu0uVztuXJB97q/XQv7xLc/iCv/m4NK2G', '', '', '', '', '', '2156755615', '', '', '', '', '', 1, 'xxxxxxxxxxxxxxxxxxxxx', 'https://payzang.transactiongateway.com/api/v2/three-step', '230.77', 1, 0, 0, '0', '2017-09-06 16:31:23', '2017-09-10 05:48:45', 0, 1, 0),
(3, 'renter', 'Amanda', 'Scott', 'email2#email.com', '$2y$10$oBU3tbcVT63Asw6CDLCGB.SNAvSpJaOcLBx4NqodFNMwvlSRLc.tO', '', '', '', '', '', '(215) 672-3778', '', '', '', '', '', 0, '', '', NULL, 0, 0, 2, '1', '2017-09-06 17:54:02', '2017-09-06 17:54:02', 2, 0, 1),
(4, 'renter', 'Megan', 'Ridgell', 'email3#email.com', '$2y$10$mPR7U/ri/rpW2n5f6X4mV.6jCycvLTJ/dvttR8seMFSGgRR/tQh1.', '', '', '', '', '', '2154852222', '', '', '', '', '', 0, '', '', NULL, 0, 0, 11, '1', '2017-09-06 18:30:01', '2017-09-12 06:22:23', 0, 2, 1),
(5, 'landlord', 'Brian', 'Davis', 'email4#email.com', '$2y$10$NwcR0ObYeUM.OfjgMOaajeH6bjjuMTpU2GnF0h623SER37AFk8K9i', '', 'PO Box 123', 'Baltimore', '21', '21231', '4104999026', '', '', '', '', '', 0, '', '', '0.00', 1, 0, 0, '0', '2017-09-07 05:19:46', '2017-09-07 05:19:46', 0, 0, 0),
(6, 'renter', 'Greg', 'Davis', 'email5#email.com', '$2y$10$Bd6S4KMGec2NzqKcvsJ1huwmbfACu8oOn56JLFSVQFt6ANg0Vr.ZW', '', '', '', '', '', '4104999026', '', '', '', '', '', 0, '', '', NULL, 1, 0, 5, '1', '2017-09-07 05:30:58', '2017-09-17 04:22:30', 0, 2, 0),
(7, 'renter', 'Maddie', 'Evans', 'email6#email.com', '$2y$10$ry9DBBFJHRhCsV7wpU9s9OI9WQ8BWqCTdk0K9KyYw/VD10mb/yC3O', '', '', '', '', '', '4104999026', '', '', '', '', '', 0, '', '', NULL, 1, 0, 0, '0', '2017-09-07 06:07:10', '2017-09-17 04:14:48', 0, 5, 0),
(8, 'renter', 'Arijita', 'Dey', 'email7#email.com', '$2y$10$NwcR0ObYeUM.OfjgMOaajeH6bjjuMTpU2GnF0h623SER37AFk8K9i', '', '', '', '', '', '3433453535', '', '', '', '', '', 0, '', '', NULL, 0, 0, 2, '0', '2017-09-07 08:08:21', '2017-09-10 05:42:58', 0, 2, 1),
(9, 'renter', 'Arijita', 'Dey', 'email8#email.com', '$2y$10$NwcR0ObYeUM.OfjgMOaajeH6bjjuMTpU2GnF0h623SER37AFk8K9i', '', '', '', '', '', '(564) 564-5645', '', '', '', '', '', 0, '', '', NULL, 0, 0, 5, '0', '2017-09-07 11:05:33', '2017-09-07 11:05:33', 5, 0, 1),
(10, 'renter', 'Rick', 'Roy', 'email9#email.com', '$2y$10$PG/8SkAwOZsSkD.px9FGveFIbriavOTX6vdBWHHmdjQeKgbwr0/wu', '', '', '', '', '', '(533) 453-4534', '', '', '', '', '', 0, '', '', NULL, 0, 0, 5, '0', '2017-09-07 11:11:49', '2017-09-07 11:11:49', 5, 0, 1),
(11, 'landlord', 'Denise', 'Supplee', 'email0#email.com', '$2y$10$52H0QZMFK9ApK7JwaDV9qOlqyOxcyTH3QikQqXgAcLD.zrb.ggoQG', '', '', '', '', '', '2156755615', '', '', '', '', '', 1, 'xxxxxxxxxxxxxxxxxxxxx', 'https://payzang.transactiongateway.com/api/v2/three-step', '0.00', 1, 0, 0, '0', '2017-09-11 14:09:03', '2017-09-18 23:22:57', 0, 1, 0);
CREATE TABLE `lease_with_max_id` (
`id` int(11)
,`lease_id` int(11)
,`renter_id` int(11)
,`landlord_id` int(11)
,`property_id` int(11)
,`is_rent_deducted_form_filled_up` enum('0','1')
,`status` int(11)
,`is_deleted` enum('0','1')
,`is_lease_ended` enum('0','1')
);
CREATE TABLE `properties` (
`id` int(11) NOT NULL,
`user_id` int(11) NOT NULL COMMENT 'landlord''s id',
`property_name` varchar(255) NOT NULL,
`street_address` varchar(255) NOT NULL,
`street_address_2` varchar(255) DEFAULT NULL,
`city` varchar(100) NOT NULL,
`state` varchar(100) NOT NULL,
`zip` varchar(100) NOT NULL,
`rent_amount` decimal(18,2) DEFAULT '0.00',
`is_active` enum('0','1') NOT NULL,
`is_assigned` enum('0','1') NOT NULL DEFAULT '0' COMMENT 'Is assigned to some renter or not',
`created_at` datetime NOT NULL,
`modified_at` datetime NOT NULL,
`is_deleted` int(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `properties` (`id`, `user_id`, `property_name`, `street_address`, `street_address_2`, `city`, `state`, `zip`, `rent_amount`, `is_active`, `is_assigned`, `created_at`, `modified_at`, `is_deleted`) VALUES
(1, 2, 'Test Property', '330 Crooked Billet Rd', '', 'Hatboro', '39', '19040', '0.00', '0', '0', '2017-09-10 05:38:12', '2017-09-10 05:38:12', 0),
(2, 2, '', '515 Main St', 'Apt. 1809', 'Horsham', '39', '19044', '0.00', '1', '1', '2017-10-09 04:24:50', '2017-10-09 04:24:50', 1),
(3, 5, 'Test 1', '123 Brian\'s Test St.', 'Apt. 2', 'Baltimore', '21', '21231', '0.00', '1', '0', '2017-09-07 05:21:10', '2017-09-07 05:21:10', 1),
(4, 5, 'Test 1', '123 Brian\'s Test St.', 'Apt. 2', 'Baltimore', '21', '21231', '0.00', '1', '0', '2017-09-07 05:21:18', '2017-09-07 05:21:18', 1),
(5, 5, 'Test 1', '123 Brian\'s Test St.', 'Apt. 1503', 'Baltimore', '21', '21231', '0.00', '0', '0', '2017-09-11 13:49:11', '2017-09-11 13:49:11', 1),
(6, 5, 'Test 2', '123 Brian\'s Test St.', 'Apt. 1', 'Baltimore', '21', '21231', '0.00', '1', '1', '2017-10-03 04:31:18', '2017-10-03 04:31:18', 0);
CREATE TABLE `geo_states` (
`id` int(11) NOT NULL,
`name` varchar(50) NOT NULL DEFAULT '',
`abv` char(2) NOT NULL DEFAULT '',
`country` char(2) NOT NULL,
`is_state` char(1) DEFAULT NULL,
`is_lower48` char(1) DEFAULT NULL,
`slug` varchar(50) NOT NULL,
`latitude` float(9,6) DEFAULT NULL,
`longitude` float(9,6) DEFAULT NULL,
`population` bigint(20) UNSIGNED DEFAULT NULL,
`area` float(8,2) UNSIGNED DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE VIEW `test` AS SELECT
`users`.`id` AS `id`,
`users`.`user_type` AS `user_type`,
`users`.`first_name` AS `first_name`,
`users`.`last_name` AS `last_name`,
CONCAT(
`users`.`first_name`,
' ',
`users`.`last_name`
) AS `landlord_name`,
`users`.`email_id` AS `email_id`,
`users`.`password` AS `password`,
`users`.`company_name` AS `company_name`,
`users`.`street_address` AS `street_address`,
`users`.`city` AS `city`,
`users`.`state` AS `state`,
`users`.`zip` AS `zip`,
`users`.`phone_no` AS `phone_no`,
`users`.`bank_name` AS `bank_name`,
`users`.`billing_address` AS `billing_address`,
`users`.`name_on_card` AS `name_on_card`,
`users`.`bank_account_number` AS `bank_account_number`,
`users`.`bank_routing_no` AS `bank_routing_no`,
`users`.`has_payzang_account` AS `has_payzang_account`,
(
CASE WHEN(`users`.`has_payzang_account` = 0) THEN 'No' WHEN(`users`.`has_payzang_account` = 1) THEN 'Yes'
END
) AS `has_payzang_account_custom`,
`users`.`payzang_api` AS `payzang_api`,
`users`.`payzang_gateway_url` AS `payzang_gateway_url`,
`users`.`pending_amount` AS `pending_amount`,
`users`.`is_active` AS `is_active`,
(
CASE WHEN(`users`.`is_active` = '0') THEN 'Inactive' WHEN(`users`.`is_active` = '1') THEN 'Active'
END
) AS `is_active_custom`,
`users`.`is_assigned` AS `is_assigned`,
`users`.`assigned_to` AS `assigned_to`,
`users`.`is_set_up_new_password` AS `is_set_up_new_password`,
`users`.`created_at` AS `created_at`,
`users`.`modified_at` AS `modified_at`,
`users`.`created_by` AS `created_by`,
`users`.`modified_by` AS `modified_by`,
`users`.`is_deleted` AS `is_deleted`,
`lease_with_max_id`.`id` AS `lease_with_max_id_id`,
`lease_with_max_id`.`lease_id` AS `lease_with_max_id_lease_id`,
`lease_with_max_id`.`renter_id` AS `lease_with_max_id_renter_id`,
`lease_with_max_id`.`landlord_id` AS `lease_with_max_id_landlord_id`,
`lease_with_max_id`.`property_id` AS `lease_with_max_id_property_id`,
`lease_with_max_id`.`is_rent_deducted_form_filled_up` AS `lease_with_max_id_is_rent_deducted_form_filled_up`,
`lease_with_max_id`.`status` AS `lease_with_max_id_status`,
`lease_with_max_id`.`is_deleted` AS `lease_with_max_id_is_deleted`,
`properties`.`id` AS `property_id`,
`properties`.`user_id` AS `property_user_id`,
`properties`.`property_name` AS `property_property_name`,
`properties`.`street_address` AS `property_street_address`,
`properties`.`street_address_2` AS `property_street_address_2`,
`properties`.`city` AS `property_city`,
`properties`.`state` AS `property_state`,
`properties`.`zip` AS `property_zip`,
`properties`.`rent_amount` AS `property_rent_amount`,
`properties`.`is_active` AS `property_is_active`,
`properties`.`is_assigned` AS `property_is_assigned`,
`properties`.`created_at` AS `property_created_at`,
`properties`.`modified_at` AS `property_modified_at`,
`properties`.`is_deleted` AS `property_is_deleted`,
`geo_states`.`name` AS `geo_states_name`
FROM
(
(
(
`users`
LEFT JOIN `lease_with_max_id` ON
(
(
`lease_with_max_id`.`renter_id` = `users`.`id`
)
)
)
LEFT JOIN `properties` ON
(
(
`properties`.`id` = `lease_with_max_id`.`property_id`
)
)
)
LEFT JOIN `geo_states` ON
(
(
`geo_states`.`id` = `properties`.`state`
)
)
)
WHERE
(
(`users`.`user_type` = 'landlord') AND(`users`.`is_deleted` = 0)
)
ORDER BY
`users`.`id`
DESC
;

Optimizing query mysql

I have 3 tables in which i store some values. Everything is working fine except my query is taking too long to execute. I have around 500.000 rows in table "tickets" at the moment. I would like to know what would be the best way to optimize this query to execute SELECT faster.
One more thing: I would like to know is there a way to update all rows through query (not using my C# app). In this case i need to update column "wonamount" which is in tickets with value that i get by multiplying row "coefficient" with row "uplata" and update column status with value "2".
Here are my tables and sql:
CREATE TABLE IF NOT EXISTS `coefficients` (`number` int(11) DEFAULT NULL,`coefficient` int(11) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `coefficients` (`number`, `coefficient`) VALUES
(1, 0),
(2, 0),
(3, 0),
(4, 0),
(5, 0),
(6, 10000),
(7, 7500),
(8, 5000),
(9, 2500),
(10, 1000),
(11, 500),
(12, 300),
(13, 200),
(14, 120),
(15, 80),
(16, 70),
(17, 60),
(18, 50),
(19, 40),
(20, 35),
(21, 30),
(22, 25),
(23, 20),
(24, 15),
(25, 12),
(26, 10),
(27, 9),
(28, 8),
(29, 7),
(30, 6),
(31, 5),
(32, 4),
(33, 3),
(34, 2),
(35, 1);
CREATE TABLE IF NOT EXISTS `draws` (
`iddraws` int(11) NOT NULL AUTO_INCREMENT,
`1` varchar(255) DEFAULT NULL,
`2` varchar(45) DEFAULT NULL,
`3` varchar(45) DEFAULT NULL,
`4` varchar(45) DEFAULT NULL,
`5` varchar(45) DEFAULT NULL,
`6` varchar(45) DEFAULT NULL,
`7` varchar(45) DEFAULT NULL,
`8` varchar(45) DEFAULT NULL,
`9` varchar(45) DEFAULT NULL,
`10` varchar(45) DEFAULT NULL,
`11` varchar(45) DEFAULT NULL,
`12` varchar(45) DEFAULT NULL,
`13` varchar(45) DEFAULT NULL,
`14` varchar(45) DEFAULT NULL,
`15` varchar(45) DEFAULT NULL,
`16` varchar(45) DEFAULT NULL,
`17` varchar(45) DEFAULT NULL,
`18` varchar(45) DEFAULT NULL,
`19` varchar(45) DEFAULT NULL,
`20` varchar(45) DEFAULT NULL,
`21` varchar(45) DEFAULT NULL,
`22` varchar(45) DEFAULT NULL,
`23` varchar(45) DEFAULT NULL,
`24` varchar(45) DEFAULT NULL,
`25` varchar(45) DEFAULT NULL,
`26` varchar(45) DEFAULT NULL,
`27` varchar(45) DEFAULT NULL,
`28` varchar(45) DEFAULT NULL,
`29` varchar(45) DEFAULT NULL,
`30` varchar(45) DEFAULT NULL,
`31` varchar(45) DEFAULT NULL,
`32` varchar(45) DEFAULT NULL,
`33` varchar(45) DEFAULT NULL,
`34` varchar(45) DEFAULT NULL,
`35` varchar(45) DEFAULT NULL,
`datetime` varchar(255) DEFAULT NULL,
PRIMARY KEY (`iddraws`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=162 ;
INSERT INTO `draws` (`iddraws`, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `11`, `12`, `13`, `14`, `15`, `16`, `17`, `18`, `19`, `20`, `21`, `22`, `23`, `24`, `25`, `26`, `27`, `28`, `29`, `30`, `31`, `32`, `33`, `34`, `35`, `datetime`) VALUES
(1, '17', '46', '27', '30', '8', '11', '4', '40', '37', '36', '22', '14', '35', '47', '24', '20', '23', '10', '2', '42', '41', '43', '9', '19', '7', '48', '3', '38', '29', '44', '16', '12', '26', '13', '5', '1391130262'),
(2, '45', '2', '1', '24', '30', '4', '10', '11', '22', '3', '38', '33', '35', '14', '48', '28', '42', '27', '43', '9', '15', '29', '36', '41', '26', '23', '13', '5', '16', '20', '12', '6', '32', '37', '19', '1391134904'),
(3, '12', '46', '32', '15', '14', '41', '45', '6', '9', '20', '26', '2', '47', '37', '33', '39', '34', '17', '16', '23', '35', '29', '44', '36', '18', '40', '22', '4', '27', '30', '38', '21', '3', '43', '24', '1391135221');
CREATE TABLE IF NOT EXISTS `tickets` (
`id_tiketa` int(11) NOT NULL AUTO_INCREMENT,
`idtickets` varchar(45) DEFAULT NULL,
`b1` varchar(45) DEFAULT NULL,
`b2` varchar(45) DEFAULT NULL,
`b3` varchar(45) DEFAULT NULL,
`b4` varchar(45) DEFAULT NULL,
`b5` varchar(45) DEFAULT NULL,
`b6` varchar(45) DEFAULT NULL,
`shop` varchar(45) DEFAULT NULL,
`user` varchar(45) DEFAULT NULL,
`time` varchar(255) DEFAULT NULL,
`status` varchar(45) DEFAULT NULL,
`uplata` varchar(45) DEFAULT NULL,
`draw` varchar(45) DEFAULT NULL,
`qt` varchar(45) DEFAULT NULL,
`wonamount` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id_tiketa`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=138 ;
INSERT INTO `tickets` (`id_tiketa`, `idtickets`, `b1`, `b2`, `b3`, `b4`, `b5`, `b6`, `shop`, `user`, `time`, `status`, `uplata`, `draw`, `qt`, `wonamount`) VALUES
(75, '4-1-170-1367', '41', '47', '17', '24', '15', '44', '170', 'w1', '1391149398', '1', '1', '1', '', ''),
(76, '4-1-170-20104', '23', '27', '13', '7', '14', '42', '170', 'w1', '1391149398', '1', '1', '1', '', ''),
(91, '4-2-170-13887', '16', '4', '13', '35', '30', '9', '170', 'w1', '1391149462', '1', '1', '2', '', ''),
(92, '4-2-170-9701', '2', '32', '7', '15', '5', '34', '170', 'w1', '1391149463', '1', '1', '2', '', ''),
(93, '4-2-170-45661', '23', '24', '22', '27', '48', '6', '170', 'w1', '1391149463', '1', '1', '2', '', ''),
(98, '4-2-170-45503', '36', '13', '33', '10', '29', '9', '170', 'w1', '1391149463', '1', '1', '2', '', ''),
(99, '4-2-170-24095', '19', '35', '11', '36', '46', '40', '170', 'w1', '1391149463', '1', '1', '2', '', ''),
(100, '4-2-170-42832', '27', '32', '17', '29', '7', '21', '170', 'w1', '1391149463', '1', '1', '2', '', ''),
(101, '4-2-170-13570', '22', '23', '32', '6', '1', '28', '170', 'w1', '1391149463', '1', '1', '2', '', ''),
(103, '4-2-170-28122', '15', '10', '11', '9', '14', '48', '170', 'w1', '1391149463', '1', '1', '2', '', ''),
(116, '4-2-170-13095', '28', '20', '33', '42', '26', '14', '170', 'w1', '1391149464', '1', '1', '2', '', ''),
(118, '4-2-170-27646', '23', '14', '37', '27', '24', '19', '170', 'w1', '1391149464', '1', '1', '2', '', ''),
(124, '4-2-170-23302', '20', '23', '15', '38', '4', '45', '170', 'w1', '1391149465', '1', '1', '2', '', '');
SELECT t.idtickets,
t.uplata,
c.coefficient
FROM tickets t
INNER JOIN draws d ON(FIELD(t.b1,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)>0)
AND (FIELD(t.b2,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)>0)
AND (FIELD(t.b3,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)>0)
AND (FIELD(t.b4,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)>0)
AND (FIELD(t.b5,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)>0)
AND (FIELD(t.b6,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35)>0)
INNER JOIN coefficients c ON c.number = GREATEST(FIELD(t.b1,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35), FIELD(t.b2,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35), FIELD(t.b3,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35), FIELD(t.b4,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35), FIELD(t.b5,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35), FIELD(t.b6,d.1,d.2,d.3,d.4,d.5,d.6,d.7,d.8,d.9,d.10,d.11,d.12,d.13,d.14,d.15,d.16,d.17,d.18,d.19,d.20,d.21,d.22,d.23,d.24,d.25,d.26,d.27,d.28,d.29,d.30,d.31,d.32,d.33,d.34,d.35))
WHERE t.draw='1'
AND t.status = '1'
AND d.iddraws='1'
And yes, i need to do that for each t.draw and d.iddraws which will be same values.
While my answer is fairly general, I am assuming you are using MySQL.
Short Answer:
Do the following things one by one in the order mentioned while measuring performance improvement with each step.
Add Indexes on tickets.draw and tickets.status. Also add an index (primary key will be even better) on coefficients.number.
Use int instead of varchar wherever possible.
Convert query to stored procedure to save values of FIELD calls and reuse these values in GREATEST instead of calling FIELD again with same values.
Move calls to FIELD at INSERT/UPDATE time instead of SELECT.
Yes, you can update all rows through query. Use your SELECT query and make the following changes to it:
Replace SELECT t.idtickets,t.uplata, c.coefficient FROM by UPDATE
Add SET t.wonamount = c.coefficient*t.uplata, t.status='2' before WHERE ...
(Really) Long Answer:
Your question is a very good case for discussing SQL optimization as there are many optimization techniques that can be applied here. Let me discuss them in increasing order of complexity, so that you can implement them one by one till you are happy with the results. I will also generalize every point for community's benefit while giving precise suggestions to you. Let's start:
All SQL optimization starts with EXPLAIN. It's a sort of black magic that tells what's wrong with your query. Simply add the EXPLAIN keyword before the SELECT keyword in your query and you get a wealth of information on how your query is executed behind the scene. Here is the EXPLAIN output of your query (some fields removed for sake of brevity):
+-------+-------+---------------+---------+-------+------+-----------------+
| table | type | possible_keys | key | ref | rows | Extra |
+-------+-------+---------------+---------+-------+------+-----------------+
| d | const | PRIMARY | PRIMARY | const | 1 | |
| t | ALL | NULL | NULL | NULL | 13 | Using where |
| c | ALL | NULL | NULL | NULL | 35 | Using where;... |
+-------+-------+---------------+---------+-------+------+-----------------+
Each row covers a table involved in your query. Two important fields to look at here are key and rows. rows tells the number of rows of that table scanned for the query. The more this number, the more data MySQL has to scan, and therefore the slower your query. key tells if MySQL is using any shortcut to reduce rows. In the absence of any key, MySQL has to scan all rows of that table. So, we need to supply keys (also called indexes) to MySQL so that it can reduce rows and execute queries fast.
Here, table t (i.e. tickets) is not using any key and therefore scanning all rows (there are 13 rows in the sample data you provided in your fiddle, and 500,000 of them in the real data). So, we add keys (or indexes) to those fields of tickets table that are involved in decision making in this query. These fields are draw and status (... WHERE t.draw='1' AND t.status = '1'...).
mysql> ALTER TABLE tickets ADD INDEX idx_draw(draw);
mysql> ALTER TABLE tickets ADD INDEX idx_status(status);
Similarly, coefficients will benefit by index on number. A PRIMARY KEY on number will be even better if numbers are unique.
Integer data types (short, int, long, etc.) are significantly faster than character data types (char, varchar, etc.). So, avoid using character data types for integer data. In your data, all fields in draws table, and almost all fields in tickets table contain numeric data. (Booleans can be stored as byte instead of varchar. Also consider storing timestamps as int or long instead of varchar.)
FIELD is a costly call, especially if given a lot of arguments, as has to do a lot of work. In your query there are six distinct FIELD calls, and each is repeated in the call to GREATEST function, making 12 calls in total. Consider using stored procedures which allow you to save results of function calls in variables and reuse them later.
Performing validations during INSERT/UPDATE is better than performing them during SELECT. Consider validating your tickets.b1-b6 against draws.1-35 while inserting/updating instead of querying and your SELECT query will be much simpler and faster. The result of GREATEST can also be calculated at insert/update time and saved in an extra field in the tickets table to avoid recalculation every time during SELECT.
As with all queries, your query may need more optimizations when your data grows 100-1000 times its current size, but these should be enough for now.
does your db have indexes?
MYSQL indexes