How to find start time of last GPS route - mysql

I have data from GPS trackers.
Let's say something like:
CREATE TABLE IF NOT EXISTS `tab_gps` (
`id` int(6) unsigned NOT NULL,
`speed` int(3) unsigned NOT NULL,
`time` time NOT NULL,
`tracker_name` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
and some sample data:
('1', '5', '07:00', 'car'),
('2', '10', '07:10', 'car'),
('3', '0', '07:20', 'bus'),
('4', '0', '07:30', 'car'),
('5', '0', '07:40', 'car'),
('6', '0', '07:50', 'car'),
('7', '20', '08:00', 'car'),
('8', '40', '08:10', 'bus'),
('9', '15', '08:15', 'car'),
('10', '0', '08:32', 'car'),
('11', '0', '08:40', 'car'),
('12', '0', '08:52', 'bus'),
('13', '12', '09:10', 'car'),
('14', '0', '09:25', 'car'),
('15', '0', '09:30', 'car'),
('16', '0', '09:35', 'car'),
('17', '10', '09:41', 'car'),
('18', '5', '09:46', 'car');
('19', '0', '09:50', 'car');
The question is how to find the time when specific 'tracker_name' started his route
So in my example algorithm in my head is:
SELECT * FROM tab_gps WHERE tracker_name='car' then
find the last position with speed=0 (which have next positions >0)
and the next position is the TIME which I am looking for
In my example, it is a position time: 09:41 (id = 17)
I am trying to use:
select max(time) from tab_gps where tracker_name='car' and speed <>0
Here is the Fiddle to better understanding: http://sqlfiddle.com/#!9/834371

Well, for the final route, you can do:
select g.*
from tab_gp2 g
where g.tracker_name = 'car' and
g.id > (select coalesce(max(g2.id), -1)
from tab_gps g2
where g2.speed = 0 and g2.tracker_name = 'car'
);
Here is a db<>fiddle illustrating the logic.

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?

Show filters from category & products, show filters relevant filter post selection (and non-relevant from active parent) & product count per filter

I posted a similar question on 10 Jan 2020 (see here) but my problem/question has expanded since then and don't want S.O. police on my back ;)
This is the criteria:
There are filter categories and there are filters belong to those categories.
Each filter displays a count of how many products belong to a filter.
The number of available filters need to reduce depending on active filter selection to not only those that still apply to the pool of products but those that are also relevant to child filters of other filter categories.
There is a caveat to the above, if only one filter category has child filters selected then all the sibling filters (filters of the same filter category are to still display) until a filter from another filter category is made active. Once a filter from another filter category/parent is selected the previous full list can reduce but previously select filters must remain active just show a count of zero if that's the case.
I think I'm really close with the SQL below but it will not correctly update the count of products belonging to each filter upon selection. The only way I can get it to work is if I change the OR to an AND in the SQL below but this then will not show the other child filters belonging to a filter category that has an active child filter.
I have provided schema and sample data here, along with a fiddle of samehttp://sqlfiddle.com/#!9/afbd4d7/1/0:
CREATE TABLE `store_categories` (
`categories_id` int(11) NOT NULL AUTO_INCREMENT,
`parent_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`categories_id`),
KEY `idx_categories_parent_id` (`parent_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `store_products` (
`products_id` int(11) NOT NULL AUTO_INCREMENT,
`products_status` tinyint(1) NOT NULL,
PRIMARY KEY (`products_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `store_products_filters` (
`pf_id` int(11) NOT NULL AUTO_INCREMENT,
`pf_name` varchar(250) COLLATE utf8_unicode_ci DEFAULT NULL,
`pf_sort` int(4) DEFAULT '0',
`pf_to_pfc_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`pf_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `store_products_filters_categories` (
`pfc_id` int(11) NOT NULL AUTO_INCREMENT,
`pfc_name` varchar(250) COLLATE utf8_unicode_ci DEFAULT NULL,
`pfc_sort` int(4) DEFAULT '0',
PRIMARY KEY (`pfc_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `store_products_filters_to_products` (
`pftp_id` int(11) NOT NULL AUTO_INCREMENT,
`pftp_pf_id` int(11) DEFAULT NULL,
`pftp_products_id` int(11) DEFAULT NULL,
PRIMARY KEY (`pftp_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `store_products_to_categories` (
`products_id` int(11) NOT NULL,
`categories_id` int(11) NOT NULL,
PRIMARY KEY (`products_id`,`categories_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `store_categories` VALUES ('1', '0');
INSERT INTO `store_categories` VALUES ('2', '1');
INSERT INTO `store_categories` VALUES ('3', '0');
INSERT INTO `store_categories` VALUES ('4', '1');
INSERT INTO `store_categories` VALUES ('5', '0');
INSERT INTO `store_categories` VALUES ('6', '0');
INSERT INTO `store_products` VALUES ('1', '1');
INSERT INTO `store_products` VALUES ('2', '1');
INSERT INTO `store_products` VALUES ('3', '1');
INSERT INTO `store_products` VALUES ('4', '1');
INSERT INTO `store_products` VALUES ('5', '0');
INSERT INTO `store_products_filters` VALUES ('1', 'Small', '0', '1');
INSERT INTO `store_products_filters` VALUES ('2', 'Medium', '0', '1');
INSERT INTO `store_products_filters` VALUES ('3', 'Large', '0', '1');
INSERT INTO `store_products_filters` VALUES ('4', 'Extra Large', '0', '1');
INSERT INTO `store_products_filters` VALUES ('5', 'Circle', '0', '2');
INSERT INTO `store_products_filters` VALUES ('6', 'Square', '0', '2');
INSERT INTO `store_products_filters` VALUES ('7', 'Triangle', '0', '2');
INSERT INTO `store_products_filters` VALUES ('8', 'Red', '5000', '3');
INSERT INTO `store_products_filters` VALUES ('9', 'Blue', '5000', '3');
INSERT INTO `store_products_filters` VALUES ('10', 'Black', '2000', '3');
INSERT INTO `store_products_filters` VALUES ('11', 'Yellow', '5000', '3');
INSERT INTO `store_products_filters_categories` VALUES ('1', 'Size', '300');
INSERT INTO `store_products_filters_categories` VALUES ('2', 'Type', '100');
INSERT INTO `store_products_filters_categories` VALUES ('3', 'Colour', '400');
INSERT INTO `store_products_filters_to_products` VALUES ('1', '1', '1');
INSERT INTO `store_products_filters_to_products` VALUES ('2', '6', '1');
INSERT INTO `store_products_filters_to_products` VALUES ('3', '9', '1');
INSERT INTO `store_products_filters_to_products` VALUES ('4', '1', '2');
INSERT INTO `store_products_filters_to_products` VALUES ('5', '5', '2');
INSERT INTO `store_products_filters_to_products` VALUES ('6', '8', '2');
INSERT INTO `store_products_filters_to_products` VALUES ('7', '2', '3');
INSERT INTO `store_products_filters_to_products` VALUES ('8', '7', '3');
INSERT INTO `store_products_filters_to_products` VALUES ('9', '11', '3');
INSERT INTO `store_products_filters_to_products` VALUES ('10', '3', '4');
INSERT INTO `store_products_filters_to_products` VALUES ('11', '5', '4');
INSERT INTO `store_products_filters_to_products` VALUES ('12', '10', '4');
INSERT INTO `store_products_to_categories` VALUES ('1', '2');
INSERT INTO `store_products_to_categories` VALUES ('2', '2');
INSERT INTO `store_products_to_categories` VALUES ('3', '2');
INSERT INTO `store_products_to_categories` VALUES ('4', '2');
INSERT INTO `store_products_to_categories` VALUES ('5', '2');
There are 3 active filters, Small, Medium and Circle.
There are 2 "Small" sized products
There is 1 "Medium" sized product
There are 2 products that have a "Circle" filter applied to them but only one of those is "Small" and the other is "Large"
With Small, Medium filters applied I would expect/like Circle to show 1 but it shows a count of 2
OR pf.pf_id IN (
SELECT
pf_id
FROM
store_products_filters
WHERE
pf_to_pfc_id IN (1, 2)
)
My thought so far is to remove the above SQL if filters from more than one filter category are active.
You can see an example of what I'm trying to achieve on this site https://www.ukpos.com/display-stands-frames-and-noticeboards which is powered by Magento. I'm not sure if the filters are out of the box or a added plugin.
My full SQL below.
SELECT
pfc.pfc_id,
pfc.pfc_name,
pf.pf_id,
pf.pf_name,
count(pftp.pftp_pf_id) AS products_in_filter
FROM
store_products_filters_to_products pftp
LEFT JOIN store_products_filters pf ON pftp.pftp_pf_id = pf.pf_id
LEFT JOIN store_products_filters_categories pfc ON pf.pf_to_pfc_id = pfc_id
WHERE
pftp_products_id IN (
SELECT
ptc.products_id
FROM
store_products_to_categories ptc
LEFT JOIN store_products p ON ptc.products_id = p.products_id
WHERE
p.products_status = 1
AND (
ptc.categories_id = 1
OR ptc.categories_id IN (
SELECT
categories_id
FROM
store_categories
WHERE
parent_id = '1'
)
)
)
AND (
pf.pf_id IN (
SELECT
pftp_pf_id
FROM
store_products_filters_categories
WHERE
pftp_products_id IN (
SELECT
pftp.pftp_products_id
FROM
store_products_filters_to_products pftp
JOIN store_products_filters pf ON pf.pf_id = pftp.pftp_pf_id
GROUP BY
pftp.pftp_products_id
HAVING
(
sum(pf.pf_id = 1) > 0
OR sum(pf.pf_id = 2) > 0
)
AND (sum(pf.pf_id = 5) > 0)
)
)
OR pf.pf_id IN (
SELECT
pf_id
FROM
store_products_filters
WHERE
pf_to_pfc_id IN (1, 2)
)
)
GROUP BY
pfc.pfc_id,
pftp.pftp_pf_id
ORDER BY
pfc.pfc_sort ASC,
pfc.pfc_name ASC,
pf.pf_sort ASC,
pf.pf_name ASC

the group by in mysql5.7

I find the same sql run in mysql5.7 and mysql5.5 but the result is different.
the sql like this:
SELECT t2.* FROM (SELECT t1.* FROM t_user t1 ORDER BY t1.id desc) AS t2 GROUP BY t2.type;
The result in mysql5.7:
The result in mysql5.5:
the sql script like this:
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`phone` varchar(255) DEFAULT NULL,
`gender` varchar(255) DEFAULT NULL,
`type` varchar(255) DEFAULT NULL,
`birth` datetime DEFAULT NULL,
`is_delete` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=101 DEFAULT CHARSET=utf8;
INSERT INTO `t_user` VALUES ('1', 'James', '0594-5397864', '0', '3', '2016-01-30 19:01:09', '1');
INSERT INTO `t_user` VALUES ('2', 'Hayes', '0594-5392419', '1', '4', '2015-12-24 11:12:27', '1');
INSERT INTO `t_user` VALUES ('3', 'Diana', '0594-5393520', '1', '5', '2016-03-21 13:03:50', '0');
INSERT INTO `t_user` VALUES ('4', 'Rajah', '0594-5399812', '1', '4', '2015-11-26 02:11:35', '0');
INSERT INTO `t_user` VALUES ('5', 'Daria', '0594-5397571', '0', '4', '2016-01-18 11:01:11', '1');
INSERT INTO `t_user` VALUES ('6', 'Lee', '0594-5394539', '1', '1', '2015-10-23 08:10:23', '1');
INSERT INTO `t_user` VALUES ('7', 'Cameran', '0594-5392867', '0', '4', '2016-11-16 12:11:08', '0');
INSERT INTO `t_user` VALUES ('8', 'Wylie', '0594-5395349', '0', '5', '2017-07-06 04:07:27', '0');
INSERT INTO `t_user` VALUES ('9', 'Bertha', '0594-5395287', '1', '1', '2017-02-08 12:02:45', '1');
INSERT INTO `t_user` VALUES ('10', 'Fletcher', '0594-5399246', '0', '4', '2015-09-03 20:09:33', '0');
INSERT INTO `t_user` VALUES ('11', 'Conan', '0594-5391546', '1', '5', '2017-05-15 09:05:23', '0');
INSERT INTO `t_user` VALUES ('12', 'Raymond', '0594-5399666', '0', '3', '2015-10-20 05:10:05', '1');
INSERT INTO `t_user` VALUES ('13', 'Noel', '0594-5397392', '1', '4', '2017-05-26 03:05:56', '0');
INSERT INTO `t_user` VALUES ('14', 'Miriam', '0594-5399081', '0', '2', '2016-05-21 02:05:09', '0');
INSERT INTO `t_user` VALUES ('15', 'Maya', '0594-5397242', '0', '3', '2016-10-24 02:10:50', '1');
INSERT INTO `t_user` VALUES ('16', 'Winifred', '0594-5395142', '1', '1', '2017-03-15 02:03:43', '0');
INSERT INTO `t_user` VALUES ('17', 'Elaine', '0594-5398478', '1', '3', '2017-03-08 15:03:03', '1');
INSERT INTO `t_user` VALUES ('18', 'Robert', '0594-5397830', '0', '5', '2016-02-10 22:02:06', '0');
INSERT INTO `t_user` VALUES ('19', 'Patrick', '0594-5396516', '0', '4', '2015-09-10 07:09:51', '0');
INSERT INTO `t_user` VALUES ('20', 'Darrel', '0594-5397417', '0', '1', '2016-03-11 11:03:36', '0');
INSERT INTO `t_user` VALUES ('21', 'Salvador', '0594-5399732', '1', '3', '2016-01-01 15:01:21', '0');
INSERT INTO `t_user` VALUES ('22', 'Brandon', '0594-5396204', '1', '4', '2016-05-12 06:05:40', '1');
INSERT INTO `t_user` VALUES ('23', 'Dorothy', '0594-5396783', '0', '1', '2016-12-12 10:12:59', '1');
INSERT INTO `t_user` VALUES ('24', 'Kevyn', '0594-5398240', '0', '2', '2016-02-07 04:02:14', '1');
INSERT INTO `t_user` VALUES ('25', 'Brody', '0594-5398774', '1', '1', '2016-12-11 20:12:36', '0');
what cause this different... my English is poor,LOL...
MySQL is too liberal when it comes to data integrity and its default settings often allow you to accomplish data loss without a complaint (see reference for ONLY_FULL_GROUP_BY SQL mode). For instance, it allows incomplete GROUP BY clauses like yours and just picks an arbitrary (not even random) result.
You need to rewrite your query so all columns involved fit into one category:
Be part of an aggregate expression in the SELECT clause:
SELECT MAX(foo) AS maximum_foo
Be part of the GROUP BY clause:
SELECT foo
...
GROUP BY foo
This way you are being specific enough so the result-set is deterministic. Your current code is basically: "Give me one user of each type"—but you don't specify any criteria to pick users. Other DBMSs (Oracle, SQL Server) will complaint: "You didn't say how to choose users". MySQL will merely pick an arbitrary user (but not even a random user, because randomness would imply that chosen users follow a rule, which is not the case). The ONLY_FULL_GROUP_BY SQL mode is just a way to make MySQL behave like other engines.
You can also get rid of your subquery, it really serves no purpose.
No order on outer SELECT causes the difference. However what do you want to achieve? Take only one (which one) from each group (type) or what?
SELECT *
FROM t_user
WHERE id IN (SELECT MAX(id) FROM t_user GROUP BY type)
ORDER BY type
SQL fiddle link

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

MySQL: Cross table Referencing (primary, master, name) for categories and subcategories?

I am creating a table labeled categories where main catgory(parent column) contains 0 and where subcategory contains the ID of the parent category. I heard it is called Referencing. My question: Do i have this table strucutred properly? Or is there a better way like implementing a traversal tree or similar method?
CREATE TABLE `categories` (
`primary_id` int(11) NOT NULL auto_increment,
`master_id` int(11) NOT NULL default '0',
`name` varchar(50) NOT NULL default '',
PRIMARY KEY (`c_id`)
)
INSERT INTO `categories` (`primary_id`, `master_id`, `name`) VALUES
(1, '0', 'Games'),
(2, '0', 'Technology'),
(3, '0', 'Science'),
(4, '0', 'Pop Culture'),
(5, '0', 'Jobs/Services'),
(6, '0', 'Blogs'),
(7, '1', 'Computer'),
(8, '1', 'Game Console'),
(9, '2', 'Cellphone'),
(10, '2', 'Audio/Video/Photos'),
(11, '2', 'Laptop'),
(12, '2', 'Others >'),
(13, '3', 'Human Body'),
(14, '3', 'Ocean Life'),
(15, '3', 'Plant Life'),
(16, '3', 'Animal Life'),
(17, '4', 'Artist'),
(18, '4', 'Upcoming'),
(19, '5', 'Jobs'),
(20, '5', 'Tutoring'),
(21, '5', 'Seminars'),
(22, '5', 'Haircuts'),
(23, '9', 'Iphone'),
(24, '9', 'Android'),
(25, '9', 'Windows Mobile'),
(26, '11', 'Toshiba'),
(27, '11', 'HP'),
(28, '11', 'Apple'),
If you use the InnoDB engine, you can enforce referential integrity for your tables. That way, you can make sure you never insert a category which does not have matching parent id, cascade delete child categories when you delete the parent, etc. Change your create statement to:
CREATE TABLE `categories` (
`primary_id` int(11) NOT NULL auto_increment,
`master_id` int(11) NOT NULL default '0',
`name` varchar(50) NOT NULL default '',
PRIMARY KEY (`c_id`),
KEY `fk_master_id1` (`master_id`),
CONSTRAINT `fk_master_id1` FOREIGN KEY (`master_id`) REFERENCES `master` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
While this strategy works sufficiently well for most cases, you might also want to look at other strategies for implementing tree-like structures and the challenges that come with querying them effectively. See this answer on SO for more information on alternative designs for storing tree-like structures in MySQL.