Is there a way to determine which day a trip occurs with GTFS? - gtfs

I am trying to make a website that can show a user the map of a route and the schedule for that day. The problem is that within the stop_times.txt file, it shows each stop time at each stop, but not which day it occurs. How would I be able to figure out which day a trip occurs?
For example, here is some data I have sorted:
trip_id,arrival_time,departure_time,stop_id,stop_sequence,stop_headsign,pickup_type,drop_off_type,shape_dist_traveled,timepoint
'16873083', '05:52:00', '05:52:00', '9257', '31', '79 Century Park', '0', '0', '9535.3', '0'
'16873084', '06:23:00', '06:23:00', '9257', '31', '79 Century Park', '0', '0', '9535.3', '0'
'16873085', '06:52:00', '06:52:00', '9257', '31', '79 Century Park', '0', '0', '9535.3', '0'
...
'16873119', '23:44:00', '23:44:00', '9257', '31', '79 Century Park', '0', '0', '9535.3', '0'
'16873120', '24:44:00', '24:44:00', '9257', '31', '79 Century Park', '0', '0', '9535.3', '0'
'16873124', '06:36:00', '06:36:00', '9257', '31', '79 Century Park', '0', '0', '9535.3', '0'
'16873125', '07:08:00', '07:08:00', '9257', '31', '79 Century Park', '0', '0', '9535.3', '0'
'16873126', '16:11:00', '16:11:00', '9257', '31', '79 Century Park', '0', '0', '9535.3', '0'
Then it repeats this once and then there are a bunch of different times for what I think is the weekend schedule:
'16861123', '05:43:00', '05:43:00', '9257', '31', '79 Century Park', '0', '0', '9535.3', '0'
'16861124', '06:43:00', '06:43:00', '9257', '31', '79 Century Park', '0', '0', '9535.3', '0'
'16861125', '07:43:00', '07:43:00', '9257', '31', '79 Century Park', '0', '0', '9535.3', '0'
...
I've separated the data from stop_times.txt into individual files for each stop. However, as you can see above, it would be very difficult to manually figure out what day each part of the file represents for over 6,000 stops/files.
Is there any way this could be done?

As can be seen in the trip diagram the stop_times trip_id references the trips table. In the trips table the service_id references the calendar table which tells you on which days the trip runs between the specified start and end dates. This can be further tweaked by specifying exceptions such as public holidays in calendar_dates. For a complete understanding the spec at
https://developers.google.com/transit/gtfs/reference
should be read.

Related

Replace ID according to value in another column in MySQL

id ver name order
'1', '0', 'John', '1'
'2', '0', 'Jill', '2'
'3', '0', 'Tania', '3'
'4', '0', 'susan', '5'
'5', '0', 'sam', '6'
'6', '0', 'Josh', '7'
'7', '0', 'Jane', '10'
'8', '0', 'Jade', '11'
'9', '0', 'Ane', '8'
'10', '0', 'Ant', '4'
Now I need to arrange in a way that "order" is sequentially arrange such as this
id ver name order
'1', '0', 'John', '1'
'2', '0', 'Jill', '2'
'3', '0', 'Tania', '3'
'4', '0', 'Ant', '4'
'5', '0', 'susan', '5'
'6', '0', 'sam', '6'
'7', '0', 'Josh', '7'
'8', '0', 'Ane', '8'
'9', '0', 'Jane', '9'
'10', '0', 'Jade', '10'
Any suggestions are welcome
I think this resolve your problem.
UPDATE table_name
SET id = order;

SQL error when trying to insert

Been trying to make a huge SQL (around 200 queries) in order to fast insert the data into the database however for some reason i'm getting SQL error but not sure what is causing it.
The Error:
SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '6', '1', '21', '4128', '8388608', '1', '0', '0', '0', '0', '0', '0', '0', '0', at line 8
Here is a part of my code:
SET #ID = 2710601;
SET #DID = 420591;
INSERT INTO item_template (entry, class, subclass, name, displayid, Quality, BuyCount, InventoryType, Flags, FlagsExtra, maxcount, ContainerSlots, BuyPrice, SellPrice, dmg_type1, dmg_type2, dmg_min1, dmg_min2, dmg_max1, dmg_max2, delay, ammo_type, RangedModRange, itemset, bonding, block, MaxDurability, sheath, holy_res, frost_res, fire_res, shadow_res, nature_res, arcane_res, socketColor_1, socketColor_2, socketColor_3, socketContent_1, socketContent_2, socketContent_3, socketBonus, GemProperties, spellid_1, spellid_2, spellid_3, spellid_4, spellid_5, spelltrigger_1, spelltrigger_2, spelltrigger_3, spelltrigger_4, spelltrigger_5, spellcharges_1, spellcharges_2, spellcharges_3, spellcharges_4, spellcharges_5, spellppmRate_1, spellppmRate_2, spellppmRate_3, spellppmRate_4, spellppmRate_5, spellcooldown_1, spellcooldown_2, spellcooldown_3, spellcooldown_4, spellcooldown_5, spellcategory_1, spellcategory_2, spellcategory_3, spellcategory_4, spellcategory_5, spellcategorycooldown_1, spellcategorycooldown_2, spellcategorycooldown_3, spellcategorycooldown_4, spellcategorycooldown_5, startquest, material, randomproperty, randomsuffix, area, map, disenchantid, pagetext, languageid, pagematerial, foodtype, lockid, holidayid, BagFamily, ArmorDamageModifier, duration, ItemLimitCategory, minMoneyLoot, maxMoneyLoot, flagscustom, TotemCategory, AllowableRace, AllowableClass, ItemLevel, RequiredLevel, RequiredSkill, RequiredSkillRank, requiredspell, requiredhonorrank, RequiredCityRank, RequiredReputationFaction, RequiredReputationRank, RequiredDisenchantSkill, StatsCount, stat_type1, stat_type2, stat_type3, stat_type4, stat_type5, stat_type6, stat_type7, stat_type8, stat_type9, stat_type10, stat_value1, stat_value2, stat_value3, stat_value4, stat_value5, stat_value6, stat_value7, stat_value8, stat_value9, stat_value10, ScalingStatDistribution, ScalingStatValue) VALUES
(#ID+13, '2', '10', 'Artifact Weapon', #DID+13, '6', '1', '21', '4128', '8388608', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1000', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '-1', '-1', '-1', '-1', '-1', '0', '0', '0', '0', '0', '-1', '-1', '-1', '-1', '-1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '-1', '-1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '-1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0')
My full SQL you could find on PasteBin.
No idea what is causing it as the '6' after is perfectly fine (maybe something to do with the #Defines? I did try with them being := (VALUE) but had the same outcome.
Any help is appreciated.
And have you tried to reduce it to version where is only line for which you are getting error.
SET #ID = 2710601;
SET #DID = 420591;
INSERT INTO item_template (entry, class, subclass, name, displayid, Quality, BuyCount, InventoryType, Flags, FlagsExtra, maxcount, ContainerSlots, BuyPrice, SellPrice, dmg_type1, dmg_type2, dmg_min1, dmg_min2, dmg_max1, dmg_max2, delay, ammo_type, RangedModRange, itemset, bonding, block, MaxDurability, sheath, holy_res, frost_res, fire_res, shadow_res, nature_res, arcane_res, socketColor_1, socketColor_2, socketColor_3, socketContent_1, socketContent_2, socketContent_3, socketBonus, GemProperties, spellid_1, spellid_2, spellid_3, spellid_4, spellid_5, spelltrigger_1, spelltrigger_2, spelltrigger_3, spelltrigger_4, spelltrigger_5, spellcharges_1, spellcharges_2, spellcharges_3, spellcharges_4, spellcharges_5, spellppmRate_1, spellppmRate_2, spellppmRate_3, spellppmRate_4, spellppmRate_5, spellcooldown_1, spellcooldown_2, spellcooldown_3, spellcooldown_4, spellcooldown_5, spellcategory_1, spellcategory_2, spellcategory_3, spellcategory_4, spellcategory_5, spellcategorycooldown_1, spellcategorycooldown_2, spellcategorycooldown_3, spellcategorycooldown_4, spellcategorycooldown_5, startquest, material, randomproperty, randomsuffix, area, map, disenchantid, pagetext, languageid, pagematerial, foodtype, lockid, holidayid, BagFamily, ArmorDamageModifier, duration, ItemLimitCategory, minMoneyLoot, maxMoneyLoot, flagscustom, TotemCategory, AllowableRace, AllowableClass, ItemLevel, RequiredLevel, RequiredSkill, RequiredSkillRank, requiredspell, requiredhonorrank, RequiredCityRank, RequiredReputationFaction, RequiredReputationRank, RequiredDisenchantSkill, StatsCount, stat_type1, stat_type2, stat_type3, stat_type4, stat_type5, stat_type6, stat_type7, stat_type8, stat_type9, stat_type10, stat_value1, stat_value2, stat_value3, stat_value4, stat_value5, stat_value6, stat_value7, stat_value8, stat_value9, stat_value10, ScalingStatDistribution, ScalingStatValue) VALUES
(#ID+7, '2', '10', 'Artifact Weapon', #DID+7 '6', '1', '21', '4128', '8388608', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1000', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '-1', '-1', '-1', '-1', '-1', '0', '0', '0', '0', '0', '-1', '-1', '-1', '-1', '-1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '-1', '-1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '-1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0');
EDIT : You are missing coma between #DID+7 and '6':
#DID+7 '6'
EDIT2: How can you solve this problem by yourself? Use bisection method to isolate data in question.

Get last messages from table ordered by created_at

Maybe it's something continuous question, but I need little help.
I have a table
CREATE TABLE `ticket_message` (
`id` int(11) NOT NULL,
`ticket_id` int(11) NOT NULL,
`message` text COLLATE utf8_unicode_ci NOT NULL,
`created_at` int(11) NOT NULL,
`updated_at` int(11) NOT NULL,
`user_flag` tinyint(2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
I just need to get last messages,
example:
Here it is:
'57', '26', 'Hello\r\n', '1474019653', '1474019653', '0'
'58', '26', 'Where is my order?', '1474019672', '1474019672', '0'
'59', '27', 'Hi', '1474019824', '1474019824', '0'
'60', '28', 'Hi\r\n', '1474019921', '1474019921', '0'
'61', '28', 'Where is my item?', '1474019933', '1474019933', '0'
'62', '26', 'Hello', '1474019983', '1474019983', '1'
'63', '26', 'Hello', '1474020069', '1474020069', '0'
'64', '26', 'fdsfdsfds', '1474020191', '1474020191', '1'
'65', '26', 'fdsfdsfdsfdsfdsfdsfs', '1474020256', '1474020256', '0'
'66', '26', 'fdsfsfsfdsfs', '1474020358', '1474020358', '0'
'67', '27', 'hello', '1474020872', '1474020872', '1'
'68', '27', 'how are you', '1474020953', '1474020953', '0'
'69', '27', 'okey', '1474020963', '1474020963', '0'
'70', '29', 'This is a test message', '1474021973', '1474021973', '0'
'71', '29', 'This is also a test message', '1474023004', '1474023004', '1'
'72', '30', 'Test message', '1474025254', '1474025254', '0'
'73', '30', 'Test Reply Message', '1474025473', '1474025473', '1'
'74', '28', '123', '1475166801', '1475166801', '1'
'75', '27', 'foo\r\n', '1475167415', '1475167415', '1'
I just want a query, maybe "raw", maybe "yii2", that return me
Test Reply Message
foo
fdsfsfsfdsfs
This is also a test message
123
SELECT * FROM ticket_message
ORDER BY created_at DESC LIMIT 5;
If the framework won't let you say that, the fie on them.
(GROUP BY is not appropriate.)
(Subqueries are not required.)

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

How to insert new records to mysql with no key name assigned and with auto increasing primary key?

How to insert new records to mysql with no key name assigned and with auto increasing primary key?
refer to PHP mySQL - Insert new record into table with auto-increment on primary key
changed th first value to DEFAULT ,but still same error occurs
table structure:
DROP TABLE IF EXISTS `configuration_copy`;
CREATE TABLE `configuration_copy` (
`configuration_id` int(11) NOT NULL auto_increment,
`configuration_title` text NOT NULL,
`configuration_key` varchar(255) NOT NULL default '',
`configuration_value` text NOT NULL,
`configuration_description` text NOT NULL,
`configuration_group_id` int(11) NOT NULL default '0',
`sort_order` int(5) default NULL,
`last_modified` datetime default NULL,
`date_added` datetime NOT NULL default '0001-01-01 00:00:00',
`use_function` text,
`set_function` text,
PRIMARY KEY (`configuration_id`),
UNIQUE KEY `unq_config_key_zen` (`configuration_key`),
KEY `idx_key_value_zen` (`configuration_key`,`configuration_value`(10)),
KEY `idx_cfg_grp_id_zen` (`configuration_group_id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of configuration_copy
-- ----------------------------
INSERT INTO `configuration_copy` VALUES ('1', 'World Points', 'WORD_POINTS', '0', 'Points required upgrading to world grade', '1', '1', '0000-00-00 00:00:00', '2012-12-30 09:38:23', null, null);
INSERT INTO `configuration_copy` VALUES ('2', 'Iron Points', 'IRON_POINTS', '1000', 'Points required upgrading to iron grade', '1', '2', null, '0001-01-01 00:00:00', null, null);
INSERT INTO `configuration_copy` VALUES ('3', 'Bronze Points', 'BRONZE_POINTS', '2000', 'Points required upgrading to bronze grade', '1', '3', null, '0001-01-01 00:00:00', null, null);
INSERT INTO `configuration_copy` VALUES ('4', 'Silver Points', 'SILVER_POINTS', '3000', 'Points required upgrading to silver grade', '1', '4', null, '0001-01-01 00:00:00', null, null);
INSERT INTO `configuration_copy` VALUES ('5', 'Gold Points', 'GOLD_POINTS', '4000', 'Points required upgrading to gold grade', '1', '5', null, '0001-01-01 00:00:00', null, null);
There is an unique key unq_config_key_zen on the configuration_key column, so you cannot insert rows that have the same configuration_key value.
UNIQUE KEY `unq_config_key_zen` (`configuration_key`),