Populating sentences from large corpus table - mysql

I have five tables containing lexical data. I want to display sentences from corpus to the given Icelandic lemma (including all word forms). Using following approach, it takes 2 seconds to find 5 sentences. I am looking for a solution that can display all sentences available.
The expected result:
The list of sentences that contain all word forms of given lemma specified in the query.
Current result:
Current results returns only the sentences that match the word with the keyword in basic form:
word_form w_id s_id pos sentence
hest 11484 794930 1 Sentence 1. .....
hest 11484 795623 12 Sentence 2 .....
Expected result:
word_form w_id s_id pos sentence
hest 11484 794930 1 Sentence 1. .....
hest 11484 795623 12 Sentence 2 .....
...
hestur .. .. .. Sentence 13.
hestur .. .. .. Sentence 14.
...
hesti .. .. .. Sentence 21.
...
Proposed query with changes, but ends with error.
SELECT w0.keyword, w.word_form, w3.w_id, w4.s_id, w4.pos, s.sentence
FROM `1_headword` w0
INNER JOIN `2_wordform` w ON w.keyword = w0.keyword
INNER JOIN `3_words` w3 ON w3.word = w.word_form
INNER JOIN `4_inv_w` w4 ON w4.w_id = w3.w_id
INNER JOIN `5_sentences` s
ON s.s_id = w4.s_id WHERE w0.keyword like 'hestur' group by w4.s_id
Notes:
Keyword is one, the basic form - in this case "hestur". The word forms are in this case - "hest", "hesti", "hestar" (see the Insert table) etc.
In other words, the query should take all wordform of given lemma and match it sentences in which the wordforms occur.
Update II.
Few observation.
1.The following simplified query to receive w_id for all word forms returns rows with repeated w_id of the first word form.
2. The word forms can have several rows in 3_words table.
SELECT w.keyword, w.word_form, w3.w_id FROM `2_wordform1` w
JOIN `3_words` w3
ON w3.word = w.keyword and w3.gram = w.gram
WHERE w.keyword like 'tala' and w.gram = 'f'
Rows
tala tala 8809
tala tala 89664
tala tala 97991
Tala Tala 8809
Tala Tala 89664
Tala Tala 97991
tala tölur 8809
tala tölur 89664
tala tölur 97991
Tables and data
table - headwords, 70000 rows
CREATE TABLE IF NOT EXISTS `1_headword` (
`id` int(9) NOT NULL,
`keyword` varchar(100) CHARACTER SET utf8 COLLATE utf8_icelandic_ci NOT NULL,
`num_keyword` int(9) NOT NULL DEFAULT '0',
`gram` varchar(40) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC AUTO_INCREMENT=55328 ;
ALTER TABLE `1_headword`
ADD PRIMARY KEY (`id`), ADD KEY `keyword` (`keyword`);
table - word forms - 700 000 rows
CREATE TABLE IF NOT EXISTS `2_wordform` (
`id` int(10) NOT NULL,
`keyword` varchar(120) CHARACTER SET utf8 COLLATE utf8_icelandic_ci NOT NULL,
`num_keyword` int(4) NOT NULL,
`word_form` varchar(120) CHARACTER SET utf8 COLLATE utf8_icelandic_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=678480 ;
ALTER TABLE `2_wordform`
ADD PRIMARY KEY (`id`), ADD KEY `word_form` (`word_form`);
table - word forms tagged from corpus with w_id (word id), 1 million of rows
CREATE TABLE `3_words` (
`w_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`word` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`gram` varchar(255) DEFAULT NULL,
`freq` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`w_id`),
KEY `word` (`word`),
KEY `w_id` (`w_id`)
) ENGINE=MyISAM AUTO_INCREMENT=800468 DEFAULT CHARSET=utf8;
table - w_id (word id) connected to s_id (sentence id), word can be found in several sentences, plus position in the sentence, 22 millions of rows
CREATE TABLE `4_inv_w` (
`w_id` int(10) unsigned NOT NULL DEFAULT '0',
`s_id` int(10) unsigned NOT NULL DEFAULT '0',
`pos` mediumint(2) unsigned NOT NULL DEFAULT '0',
KEY `w_id` (`w_id`),
KEY `s_id` (`s_id`),
KEY `w_s` (`w_id`,`s_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
table - s_id (sentence id) with sentence, 1 million of rows
CREATE TABLE `5_sentences` (
`s_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`sentence` text,
KEY `s_id` (`s_id`)
) ENGINE=MyISAM AUTO_INCREMENT=999953 DEFAULT CHARSET=utf8;
Process
select all word forms of given lemma f.e "hestur" (horse in English)
SELECT `word_form` FROM `2_wordform` WHERE `keyword` like 'hestur'
result consist from 16 to 50 results, now cycle the result as f.e. with accusative "hest" of "hestur"
SELECT `w_id` FROM `3_words` WHERE `word` like 'hest'
the result can contain several w_id, f.e. with '10138'
SELECT `s_id`, `pos` FROM `4_inv_w` WHERE `w_id` = '10138' group by `s_id`
the result can contain several sentences, to display f.e. sentence '7201'
SELECT `sentence` FROM `5_sentences` WHERE `s_id` = '7201'
Update
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42490, 'hestur', 0, 'hest');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42498, 'hestur', 0, 'hesta');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42501, 'hestur', 0, 'hestana');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42503, 'hestur', 0, 'hestanna');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42497, 'hestur', 0, 'hestar');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42500, 'hestur', 0, 'hestarnir');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42491, 'hestur', 0, 'hesti');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42494, 'hestur', 0, 'hestinn');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42495, 'hestur', 0, 'hestinum');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42492, 'hestur', 0, 'hests');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42496, 'hestur', 0, 'hestsins');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42499, 'hestur', 0, 'hestum');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42502, 'hestur', 0, 'hestunum');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42489, 'hestur', 0, 'hestur');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42493, 'hestur', 0, 'hesturinn');
INSERT INTO 3_words (w_id, word, gram, freq) VALUES
(11484, 'hestur', 'nken', 122),
(60681, 'Hestur', 'nken', 15),
(484318, 'HESTUR', 'nken', 1),
(491111, 'Hestur', 'nken-s', 1);
INSERT INTO 3_words (w_id, word, gram, freq) VALUES
(10138, 'hest', 'nkeo', 141),
(159967, 'Hest', 'nkeo', 4),
(491114, 'Hest', 'ssm', 1);
INSERT INTO 4_inv_w (w_id, s_id, pos) VALUES
(11484, 2671, 4),
(11484, 22522, 7),
(11484, 30169, 8),
(11484, 32487, 4),
(11484, 33841, 9),
(11484, 38116, 5),
(11484, 40450, 6),
(11484, 42741, 32),
(11484, 45789, 10),
(11484, 58998, 3),
(11484, 74343, 4),
(11484, 76001, 3),
(11484, 99014, 9),
(11484, 99688, 6),
(11484, 109849, 21),
(11484, 119708, 21),
(11484, 131353, 34),
(11484, 147820, 6),
(11484, 148326, 25),
(11484, 160475, 40),
(11484, 167227, 2),
(11484, 170401, 3),
(11484, 178416, 18),
(11484, 197295, 12),
(11484, 197295, 6),
(11484, 198420, 19),
(11484, 203446, 28),
(11484, 204448, 1),
(11484, 215402, 1),
(11484, 237323, 4),
(11484, 249282, 4),
(11484, 263949, 1),
(11484, 263949, 22),
(11484, 266489, 27),
(11484, 270540, 5),
(11484, 272543, 5),
(11484, 272560, 1),
(11484, 272560, 8),
(11484, 282170, 20),
(11484, 284407, 26),
(11484, 290524, 6),
(11484, 291438, 10),
(11484, 293344, 6),
(11484, 294034, 49),
(11484, 317007, 7),
(11484, 325049, 22),
(11484, 328392, 14),
(11484, 368188, 47),
(11484, 391892, 14),
(11484, 401157, 11),
(11484, 412656, 24),
(11484, 421635, 17),
(11484, 439320, 3),
(11484, 467063, 5),
(11484, 469324, 23),
(11484, 477392, 2),
(11484, 480318, 4),
(11484, 487883, 1),
(11484, 490577, 42),
(11484, 499783, 9),
(11484, 500405, 23),
(11484, 501118, 15),
(11484, 527227, 3),
(11484, 539686, 25),
(11484, 543056, 9),
(11484, 544261, 3),
(11484, 547700, 20),
(11484, 555638, 19),
(11484, 570234, 2),
(11484, 592710, 2),
(11484, 616662, 1),
(11484, 619011, 16),
(11484, 632123, 2),
(11484, 633124, 2),
(11484, 636792, 8),
(11484, 636792, 3),
(11484, 646603, 17),
(11484, 664738, 4),
(11484, 670017, 4),
(11484, 685997, 4),
(11484, 686202, 1),
(11484, 691794, 12),
(11484, 698341, 2),
(11484, 715281, 3),
(11484, 715984, 37),
(11484, 716970, 10),
(11484, 716970, 4),
(11484, 752605, 36),
(11484, 756660, 19),
(11484, 760277, 3),
(11484, 776593, 3),
(11484, 785701, 24),
(11484, 789099, 3),
(11484, 794930, 1),
(11484, 795623, 12),
(11484, 802997, 6),
(11484, 812806, 6),
(11484, 814046, 21),
(11484, 820178, 6),
(11484, 823173, 22),
(11484, 843094, 3),
(11484, 844156, 1),
(11484, 844736, 24),
(11484, 853350, 18),
(11484, 869322, 3),
(11484, 885176, 2),
(11484, 899545, 22),
(11484, 904086, 16),
(11484, 907863, 9),
(11484, 909396, 9),
(11484, 912876, 3),
(11484, 919994, 4),
(11484, 927840, 24),
(11484, 927840, 5),
(11484, 934220, 40),
(11484, 936941, 11),
(11484, 952837, 13),
(11484, 969201, 11),
(11484, 970240, 1),
(11484, 970836, 19),
(11484, 972107, 1),
(11484, 990474, 6);
INSERT INTO 4_inv_w (w_id, s_id, pos) VALUES
(10138, 7201, 27),
(10138, 18772, 3),
(10138, 30001, 6),
(10138, 42089, 4),
(10138, 42089, 14),
(10138, 42234, 4),
(10138, 49383, 5),
(10138, 54795, 18),
(10138, 57564, 23),
(10138, 88542, 7),
(10138, 93027, 10),
(10138, 101097, 21),
(10138, 134312, 12),
(10138, 139116, 33),
(10138, 139522, 6),
(10138, 159109, 7),
(10138, 159109, 16),
(10138, 161497, 21),
(10138, 163948, 2),
(10138, 165301, 20),
(10138, 166478, 21),
(10138, 183452, 6),
(10138, 184390, 20),
(10138, 189930, 25),
(10138, 201629, 9),
(10138, 204590, 4),
(10138, 211374, 5),
(10138, 216483, 14),
(10138, 223617, 5),
(10138, 233652, 12),
(10138, 236571, 11),
(10138, 241302, 8),
(10138, 246485, 10),
(10138, 256910, 16),
(10138, 262349, 3),
(10138, 262925, 5),
(10138, 267047, 28),
(10138, 291988, 18),
(10138, 292680, 22),
(10138, 294814, 32),
(10138, 326917, 6),
(10138, 330019, 12),
(10138, 333411, 35),
(10138, 337880, 5),
(10138, 342003, 13),
(10138, 355325, 12),
(10138, 356409, 13),
(10138, 363795, 5),
(10138, 365735, 26),
(10138, 376570, 25),
(10138, 378214, 10),
(10138, 379159, 11),
(10138, 379236, 4),
(10138, 379533, 2),
(10138, 388753, 8),
(10138, 420633, 18),
(10138, 433121, 5),
(10138, 434645, 10),
(10138, 435895, 3),
(10138, 455575, 5),
(10138, 461900, 23),
(10138, 464040, 6),
(10138, 466657, 6),
(10138, 469848, 11),
(10138, 475569, 17),
(10138, 482701, 41),
(10138, 527708, 29),
(10138, 527708, 16),
(10138, 529426, 7),
(10138, 530753, 10),
(10138, 538071, 27),
(10138, 542685, 10),
(10138, 553742, 22),
(10138, 553742, 13),
(10138, 557216, 4),
(10138, 563747, 9),
(10138, 564716, 4),
(10138, 569146, 7),
(10138, 578368, 3),
(10138, 581713, 9),
(10138, 595890, 9),
(10138, 599015, 5),
(10138, 608570, 30),
(10138, 610218, 11),
(10138, 610218, 2),
(10138, 612099, 9),
(10138, 612568, 14),
(10138, 612894, 9),
(10138, 615361, 19),
(10138, 618001, 14),
(10138, 624969, 7),
(10138, 628252, 16),
(10138, 628635, 12),
(10138, 635977, 10),
(10138, 643675, 8),
(10138, 650487, 9),
(10138, 651489, 3),
(10138, 657552, 18),
(10138, 672884, 12),
(10138, 677130, 2),
(10138, 678841, 7),
(10138, 678841, 26),
(10138, 682904, 4),
(10138, 691251, 19),
(10138, 706325, 9),
(10138, 714680, 45),
(10138, 717460, 5),
(10138, 717489, 11),
(10138, 722393, 5),
(10138, 729972, 12),
(10138, 735745, 12),
(10138, 738334, 7),
(10138, 740791, 21),
(10138, 775696, 8),
(10138, 776984, 16),
(10138, 786073, 31),
(10138, 793185, 17),
(10138, 821475, 4),
(10138, 835234, 7),
(10138, 842713, 3),
(10138, 842730, 8),
(10138, 847372, 9),
(10138, 849612, 20),
(10138, 861768, 26),
(10138, 864231, 6),
(10138, 865927, 7),
(10138, 873939, 7),
(10138, 883591, 29),
(10138, 884260, 19),
(10138, 894952, 17),
(10138, 898453, 19),
(10138, 899290, 4),
(10138, 909225, 29),
(10138, 910173, 4),
(10138, 922447, 2),
(10138, 939319, 2),
(10138, 956278, 4),
(10138, 967342, 18),
(10138, 977090, 3),
(10138, 991346, 31),
(10138, 991346, 40);
INSERT INTO 5_sentences (s_id, sentence) VALUES
(2671, 'Hrímnir|nken-s frá|aþ Hrafnagili|nkeþ-s Glæsilegasti|lkenve hestur|nken aldar|nvee !|!');
INSERT INTO 5_sentences (s_id, sentence) VALUES
(7201, 'Hann|fpken heilsar|sfg3en öllum|fokfþ nema|c Braga|nkeþ-s sem|ct nú|aa dregur|sfg3en í|ao land|nheo og|c vill|sfg3en friðmælast|snm við|ao Loka|nkeo-s með|aþ loforði|nheþ um|ao góðar|lvfosf gjafir|nvfo ,|, sverð|nhfo ,|, hest|nkeo og|c hring|nkeo en|c hann|fpken svarar|sfg3en bara|aa með|aþ illu|lheþsf .|.');

Any sample of data would be very helpful together with expected result.
So far you can start form this attempt:
http://sqlfiddle.com/#!9/7110ef/4
SELECT w.word_form,
w3.w_id,
w4.s_id,
w4.pos,
s.sentence
FROM `2_wordform` w
INNER JOIN `3_words` w3
ON w3.`word` = w.keyword
INNER JOIN `4_inv_w` w4
ON w4.w_id = w3.w_id
INNER JOIN `5_sentences` s
ON s.s_id = w4.s_id
WHERE w.keyword like '%hestur%'
if you need only sentence you can:
SELECT DISTINCT s.sentence
FROM `2_wordform` w
INNER JOIN `3_words` w3
ON w3.`word` = w.keyword
INNER JOIN `4_inv_w` w4
ON w4.w_id = w3.w_id
INNER JOIN `5_sentences` s
ON s.s_id = w4.s_id
WHERE w.keyword like '%hestur%'

Related

Mysql query to increase counter or marker by one for every empty record..?

I have a table structure as shown below, with columns id, number. I want to add column counter with desired output given below..like 1,1,1,1 then counter needs to be incremented by 1 on every empty row.
So for next rows it should be 2,2,2,2... until next empty rows, and so on. How to write query for this??
Any help would be appreciated.. thanks in advance..
Table structure:
CREATE TABLE `stack`
(
`id` int(11) NOT NULL auto_increment,
`number` int(5) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=300 ;
--
-- Dumping data for table `stack`
--
INSERT INTO `stack` (`id`, `number`) VALUES
(1, 75201),
(2, 55008),
(3, 55007),
(4, 75222),
(5, 0),
(6, 74992),
(7, 14553),
(8, 54582),
(9, 54581),
(10, 74991),
(11, 14554),
(12, 0),
(13, 71413),
(14, 71414),
(15, 71415),
(16, 71416),
(17, 0),
(18, 59823),
(19, 59824),
(20, 59821),
(21, 59825),
(22, 59826),
(23, 0),
(24, 58220),
(25, 58702),
(26, 18247),
(27, 51753),
(28, 12854),
(29, 15160),
(30, 18248),
(31, 51606),
(32, 18478),
(33, 68747),
(34, 68749),
(35, 58221),
(36, 18233),
(37, 15159),
(38, 18234),
(39, 58701),
(40, 58222),
(41, 68748),
(42, 51754),
(43, 18477),
(44, 51605),
(45, 68750),
(46, 18235),
(47, 18235),
(48, 12853),
(49, 18236),
(50, 0),
(51, 56617),
(52, 16349),
(53, 56612),
(54, 56614),
(55, 56613),
(56, 56616),
(57, 56362),
(58, 56611),
(59, 56363),
(60, 56610),
(61, 56619),
(62, 56620),
(63, 56621),
(64, 16350),
(65, 0),
(66, 64590),
(67, 64153),
(68, 64162),
(69, 64588),
(70, 64587),
(71, 64156),
(72, 64159),
(73, 64589),
(74, 0),
(75, 19152),
(76, 59425),
(77, 12959),
(78, 59426),
(79, 19151),
(80, 12960),
(81, 0),
(82, 54809),
(83, 54810),
(84, 54826),
(85, 14813),
(86, 54703),
(87, 74835),
(88, 74836),
(89, 54704),
(90, 14814),
(91, 54825),
(92, 0),
(93, 56700),
(94, 16128),
(95, 56319),
(96, 56718),
(97, 16723),
(98, 16724),
(99, 56717),
(100, 56320),
(101, 16127),
(102, 56701),
(103, 0),
(104, 56261),
(105, 22625),
(106, 12691),
(107, 16086),
(108, 12639),
(109, 12680),
(110, 22649),
(111, 12609),
(112, 12679),
(113, 12640),
(114, 66020),
(115, 16089),
(116, 17616),
(117, 12687),
(118, 66019),
(119, 16220),
(120, 12675),
(121, 12608),
(122, 16219),
(123, 16021),
(124, 22650),
(125, 12692),
(126, 12610),
(127, 7115),
(128, 56262),
(129, 16022),
(130, 12688),
(131, 22626),
(132, 22688),
(133, 12607),
(134, 16090),
(135, 12676),
(136, 16085),
(137, 17615),
(138, 12687),
(139, 22687),
(140, 7116),
(141, 0),
(142, 38716),
(143, 38455),
(144, 38302),
(145, 38703),
(146, 38402),
(147, 38404),
(148, 38304),
(149, 38702),
(150, 38803),
(151, 38406),
(152, 38306),
(153, 38408),
(154, 38704),
(155, 38101),
(156, 38401),
(157, 38805),
(158, 38410),
(159, 38403),
(160, 38301),
(161, 38802),
(162, 38051),
(163, 38412),
(164, 38308),
(165, 38807),
(166, 38102),
(167, 38405),
(168, 38706),
(169, 38414),
(170, 38707),
(171, 38310),
(172, 38407),
(173, 38202),
(174, 38303),
(175, 38409),
(176, 38416),
(177, 38809),
(178, 38104),
(179, 38708),
(180, 38204),
(181, 38105),
(182, 38710),
(183, 38811),
(184, 38420),
(185, 38413),
(186, 38415),
(187, 38422),
(188, 38601),
(189, 38106),
(190, 38810),
(191, 38813),
(192, 38424),
(193, 38417),
(194, 38312),
(195, 38419),
(196, 38426),
(197, 38305),
(198, 38709),
(199, 38428),
(200, 38711),
(201, 38812),
(202, 38421),
(203, 38602),
(204, 38501),
(205, 38713),
(206, 38430),
(207, 58002),
(208, 38307),
(209, 38432),
(210, 38814),
(211, 38717),
(212, 38423),
(213, 38434),
(214, 38819),
(215, 38314),
(216, 38425),
(217, 38816),
(218, 38719),
(219, 38316),
(220, 38436),
(221, 38318),
(222, 38818),
(223, 38502),
(224, 38429),
(225, 38718),
(226, 38431),
(227, 38720),
(228, 38438),
(229, 38820),
(230, 38107),
(231, 38721),
(232, 38440),
(233, 38722),
(234, 38109),
(235, 38435),
(236, 68007),
(237, 38201),
(238, 38442),
(239, 38309),
(240, 38437),
(241, 38108),
(242, 38444),
(243, 38311),
(244, 38446),
(245, 38110),
(246, 38441),
(247, 38448),
(248, 38206),
(249, 38723),
(250, 38724),
(251, 38313),
(252, 38450),
(253, 38726),
(254, 38315),
(255, 38445),
(256, 38452),
(257, 38447),
(258, 38826),
(259, 38317),
(260, 38831),
(261, 38728),
(262, 38449),
(263, 38725),
(264, 38454),
(265, 38828),
(266, 38451),
(267, 38727),
(268, 38456),
(269, 38319),
(270, 38453),
(271, 38830),
(272, 38321),
(273, 0),
(274, 19016),
(275, 59050),
(276, 59547),
(277, 59548),
(278, 59049),
(279, 19015),
(280, 0),
(281, 52171),
(282, 52174),
(283, 52172),
(284, 52173),
(285, 0),
(286, 12343),
(287, 55713),
(288, 55749),
(289, 75718),
(290, 7525),
(291, 55750),
(292, 7526),
(293, 75722),
(294, 55751),
(295, 55714),
(296, 75717),
(297, 75721),
(298, 12344),
(299, 55752);
Desired output i want:
CREATE TABLE `stack` (
`id` int(11) NOT NULL auto_increment,
`counter` int(5) NOT NULL,
`number` int(5) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=300 ;
--
-- Dumping data for table `stack`
--
INSERT INTO `stack` (`id`, `counter`, `number`) VALUES
(1, 1, 75201),
(2, 1, 55008),
(3, 1, 55007),
(4, 1, 75222),
(5, 2, 0),
(6, 2, 74992),
(7, 2, 14553),
(8, 2, 54582),
(9, 2, 54581),
(10, 2, 74991),
(11, 2, 14554),
(12, 3, 0),
(13, 3, 71413),
(14, 3, 71414),
(15, 3, 71415),
(16, 3, 71416),
(17, 4, 0),
(18, 4, 59823),
(19, 4, 59824),
(20, 4, 59821),
(21, 4, 59825),
(22, 4, 59826),
(23, 5, 0),
(24, 5, 58220),
(25, 5, 58702),
(26, 5, 18247),
(27, 5, 51753),
(28, 5, 12854),
(29, 5, 15160),
(30, 5, 18248),
(31, 5, 51606),
(32, 5, 18478),
(33, 5, 68747),
(34, 5, 68749),
(35, 5, 58221),
(36, 5, 18233),
(37, 5, 15159),
(38, 5, 18234),
(39, 5, 58701),
(40, 5, 58222),
(41, 5, 68748),
(42, 5, 51754),
(43, 5, 18477),
(44, 5, 51605),
(45, 5, 68750),
(46, 5, 18235),
(47, 5, 18235),
(48, 5, 12853),
(49, 5, 18236),
(50, 6, 0),
(51, 6, 56617),
(52, 6, 16349),
(53, 6, 56612),
(54, 6, 56614),
(55, 6, 56613),
(56, 6, 56616),
(57, 6, 56362),
(58, 6, 56611),
(59, 6, 56363),
(60, 6, 56610),
(61, 6, 56619),
(62, 6, 56620),
(63, 6, 56621),
(64, 6, 16350),
(65, 7, 0),
(66, 7, 64590),
(67, 7, 64153),
(68, 7, 64162),
(69, 7, 64588),
(70, 7, 64587),
(71, 7, 64156),
(72, 7, 64159),
(73, 7, 64589),
(74, 8, 0),
(75, 8, 19152),
(76, 8, 59425),
(77, 8, 12959),
(78, 8, 59426),
(79, 8, 19151),
(80, 8, 12960),
(81, 9, 0),
(82, 9, 54809),
(83, 9, 54810),
(84, 9, 54826),
(85, 9, 14813),
(86, 9, 54703),
(87, 9, 74835),
(88, 9, 74836),
(89, 9, 54704),
(90, 9, 14814),
(91, 9, 54825),
(92, 10, 0),
(93, 10, 56700),
(94, 10, 16128),
(95, 10, 56319),
(96, 10, 56718),
(97, 10, 16723),
(98, 10, 16724),
(99, 10, 56717),
(100, 10, 56320),
(101, 10, 16127),
(102, 10, 56701),
(103, 11, 0),
(104, 11, 56261),
(105, 11, 22625),
(106, 11, 12691),
(107, 11, 16086),
(108, 11, 12639),
(109, 11, 12680),
(110, 11, 22649),
(111, 11, 12609),
(112, 11, 12679),
(113, 11, 12640),
(114, 11, 66020),
(115, 11, 16089),
(116, 11, 17616),
(117, 11, 12687),
(118, 11, 66019),
(119, 11, 16220),
(120, 11, 12675),
(121, 11, 12608),
(122, 11, 16219),
(123, 11, 16021),
(124, 11, 22650),
(125, 11, 12692),
(126, 11, 12610),
(127, 11, 7115),
(128, 11, 56262),
(129, 11, 16022),
(130, 11, 12688),
(131, 11, 22626),
(132, 11, 22688),
(133, 11, 12607),
(134, 11, 16090),
(135, 11, 12676),
(136, 11, 16085),
(137, 11, 17615),
(138, 11, 12687),
(139, 11, 22687),
(140, 11, 7116),
(141, 12, 0),
(142, 12, 38716),
(143, 12, 38455),
(144, 12, 38302),
(145, 12, 38703),
(146, 12, 38402),
(147, 12, 38404),
(148, 12, 38304),
(149, 12, 38702),
(150, 12, 38803),
(151, 12, 38406),
(152, 12, 38306),
(153, 12, 38408),
(154, 12, 38704),
(155, 12, 38101),
(156, 12, 38401),
(157, 12, 38805),
(158, 12, 38410),
(159, 12, 38403),
(160, 12, 38301),
(161, 12, 38802),
(162, 12, 38051),
(163, 12, 38412),
(164, 12, 38308),
(165, 12, 38807),
(166, 12, 38102),
(167, 12, 38405),
(168, 12, 38706),
(169, 12, 38414),
(170, 12, 38707),
(171, 12, 38310),
(172, 12, 38407),
(173, 12, 38202),
(174, 12, 38303),
(175, 12, 38409),
(176, 12, 38416),
(177, 12, 38809),
(178, 12, 38104),
(179, 12, 38708),
(180, 12, 38204),
(181, 12, 38105),
(182, 12, 38710),
(183, 12, 38811),
(184, 12, 38420),
(185, 12, 38413),
(186, 12, 38415),
(187, 12, 38422),
(188, 12, 38601),
(189, 12, 38106),
(190, 12, 38810),
(191, 12, 38813),
(192, 12, 38424),
(193, 12, 38417),
(194, 12, 38312),
(195, 12, 38419),
(196, 12, 38426),
(197, 12, 38305),
(198, 12, 38709),
(199, 12, 38428),
(200, 12, 38711),
(201, 12, 38812),
(202, 12, 38421),
(203, 12, 38602),
(204, 12, 38501),
(205, 12, 38713),
(206, 12, 38430),
(207, 12, 58002),
(208, 12, 38307),
(209, 12, 38432),
(210, 12, 38814),
(211, 12, 38717),
(212, 12, 38423),
(213, 12, 38434),
(214, 12, 38819),
(215, 12, 38314),
(216, 12, 38425),
(217, 12, 38816),
(218, 12, 38719),
(219, 12, 38316),
(220, 12, 38436),
(221, 12, 38318),
(222, 12, 38818),
(223, 12, 38502),
(224, 12, 38429),
(225, 12, 38718),
(226, 12, 38431),
(227, 12, 38720),
(228, 12, 38438),
(229, 12, 38820),
(230, 12, 38107),
(231, 12, 38721),
(232, 12, 38440),
(233, 12, 38722),
(234, 12, 38109),
(235, 12, 38435),
(236, 12, 68007),
(237, 12, 38201),
(238, 12, 38442),
(239, 12, 38309),
(240, 12, 38437),
(241, 12, 38108),
(242, 12, 38444),
(243, 12, 38311),
(244, 12, 38446),
(245, 12, 38110),
(246, 12, 38441),
(247, 12, 38448),
(248, 12, 38206),
(249, 12, 38723),
(250, 12, 38724),
(251, 12, 38313),
(252, 12, 38450),
(253, 12, 38726),
(254, 12, 38315),
(255, 12, 38445),
(256, 12, 38452),
(257, 12, 38447),
(258, 12, 38826),
(259, 12, 38317),
(260, 12, 38831),
(261, 12, 38728),
(262, 12, 38449),
(263, 12, 38725),
(264, 12, 38454),
(265, 12, 38828),
(266, 12, 38451),
(267, 12, 38727),
(268, 12, 38456),
(269, 12, 38319),
(270, 12, 38453),
(271, 12, 38830),
(272, 12, 38321),
(273, 13, 0),
(274, 13, 19016),
(275, 13, 59050),
(276, 13, 59547),
(277, 13, 59548),
(278, 13, 59049),
(279, 13, 19015),
(280, 14, 0),
(281, 14, 52171),
(282, 14, 52174),
(283, 14, 52172),
(284, 14, 52173),
(285, 15, 0),
(286, 15, 12343),
(287, 15, 55713),
(288, 15, 55749),
(289, 15, 75718),
(290, 15, 7525),
(291, 15, 55750),
(292, 15, 7526),
(293, 15, 75722),
(294, 15, 55751),
(295, 15, 55714),
(296, 15, 75717),
(297, 15, 75721),
(298, 15, 12344),
(299, 15, 55752);
You can try to declare a variable #Val with CASE WHEN to make it.
using CASE WHEN to judgment number. if number is 0 or null do accumulate the #Val
Schema (MySQL v5.7)
CREATE TABLE `stack` (
`id` int(11) NOT NULL auto_increment,
`number` int(5) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=23 ;
--
-- Dumping data for table `stack`
--
INSERT INTO `stack` (`id`, `number`) VALUES
(1, 52201),
(2, 53008),
(3, 55007),
(4, 75222),
(5, 0),
(6, 74992),
(7, 14553),
(8, 54582),
(9, 54581),
(10, 74991),
(11, 14554),
(12, 0),
(13, 11413),
(14, 72414),
(15, 31415),
(16, 71416),
(17, 0),
(18, 59823),
(19, 69824),
(20, 59821),
(21, 69825),
(22, 59826);
Query #1
SET #Val= 1;
There are no results to be displayed.
Query #2
SELECT id,
(CASE WHEN coalesce(number,0) = 0 THEN #Val:=#Val+1 ELSE #Val END) counter,
number
FROM `stack` t1
order by id;
| id | number | counter |
| --- | ------ | ------- |
| 1 | 52201 | 1 |
| 2 | 53008 | 1 |
| 3 | 55007 | 1 |
| 4 | 75222 | 1 |
| 5 | 0 | 2 |
| 6 | 74992 | 2 |
| 7 | 14553 | 2 |
| 8 | 54582 | 2 |
| 9 | 54581 | 2 |
| 10 | 74991 | 2 |
| 11 | 14554 | 2 |
| 12 | 0 | 3 |
| 13 | 11413 | 3 |
| 14 | 72414 | 3 |
| 15 | 31415 | 3 |
| 16 | 71416 | 3 |
| 17 | 0 | 4 |
| 18 | 59823 | 4 |
| 19 | 69824 | 4 |
| 20 | 59821 | 4 |
| 21 | 69825 | 4 |
| 22 | 59826 | 4 |
View on DB Fiddle
You can emulate row_number functionality (for older versions of MySQL; version < 8.0).
Try the following query (SQL Fiddle Demo):
SET #row_number = 1;
SELECT
id,
number,
#row_number:= IF(IFNULL(number,0) = 0,
#row_number + 1,
#row_number) AS counter
FROM
stack
ORDER BY id ASC
Note:
Above code will also handle cases when number field value is null instead of 0.
Ifnull() function helps in returning 0 value, if a particular number is null.
Utilizing If() function, we can then check if the current value is 0 or not, and update the counter accordingly.
You can also achieve this by simply writing a procedure
1) Firstly, You can alter your table structure by adding column counter
ALTER TABLE stack ADD COLUMN counter INT NOT NULL DEFAULT 0;
2) Then create a procedure as:
DELIMITER $$
CREATE PROCEDURE `updateVal_Counter`()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE result INT DEFAULT 1;
DECLARE a,b,id1 INT;
DECLARE cur1 CURSOR FOR SELECT id,number,counter FROM `test`.`stack`;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop:
LOOP
FETCH cur1 INTO id1,a,b;
IF a = 0 THEN
SET result = result + 1;
END IF;
IF done THEN
LEAVE read_loop;
END IF;
UPDATE `stack` SET counter =result WHERE number=a AND id = id1;
END LOOP;
CLOSE cur1;
END$$
DELIMITER ;
3) Finally call the procedure as
call updateVal_Counter;
This will add a new column with desired value in your table structure.

SQL query INSERT column count error

I have removed 'age_range' from this query.
INSERT INTO `filters` (`id`, `user_id`, `profession_preference`, `country`, `city`, `order_by`, `profession_orientation`, `age_range`, `distance_range`, `location_dating`) VALUES
(9, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(10, 12, 3, 'Egypt', '', 2, 1, '', '', 0),
(11, 19, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(13, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(14, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(15, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(25, 121, 3, 'All Countries', '', 3, 1, '18,23', '0,500 ', 0),
(26, 316, 3, 'United States', '', 3, 1, '17,25', '0,500', 0);
I executed again and receive this error:
MySQL said: Documentation
#1136 - Column count doesn't match value count at row 1
When you insert a record, it matches the values in the VALUES list to the columns in the columns list by comma-separated position. So, this insert statement:
INSERT INTO `foo` (`A`, `B`, `C`)
VALUES ('valueA', 'valueB', 'valueC')
It will insert valueA into column A, valueB into column B, etc. because they match positions in their respective lists. If you remove B from the columns list and leave VALUES alone, it will not attempt to insert valueA into column A, but valueB into column C because they now match value positions, but it won't know what to do with valueC because there are now more values than columns, so since you removed the column from the second position, you would also need to remove the value from the second position.
So back to your query, you would need to determine which position age_range occupied in the columns list and remove the values from the same position in the values list.
Does that make sense?
You have 9 columns defined in your insert statement and you are trying to insert 10 values. you either need to add another column definition or remove from your values.
According to rule the column name define and provided values count should be same. In your case one column value in extra.
As the documentation says
"Column count doesn't match value count"
You specify 9 columns (id, user_id, profession_preference, country, city, order_by, profession_orientation, distance_range, location_dating) in your insert statement
and you are trying to insert 10 values.
You have to remove one value or add another column
Before removing column this is the script which will work
CREATE TABLE filters (id INT, user_id INT, profession_preference INT, country VARCHAR(50), city VARCHAR(50), order_by INT, profession_orientation INT, age_range VARCHAR(50), distance_range VARCHAR(50), location_dating INT);
INSERT INTO filters (id, user_id, profession_preference, country, city, order_by, profession_orientation, age_range, distance_range, location_dating) VALUES
(9, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(10, 12, 3, 'Egypt', '', 2, 1, '', '', 0),
(11, 19, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(13, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(14, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(15, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(25, 121, 3, 'All Countries', '', 3, 1, '18,23', '0,500 ', 0),
(26, 316, 3, 'United States', '', 3, 1, '17,25', '0,500', 0);
Now since you removed age_range column, below script will work:
INSERT INTO filters (id, user_id, profession_preference, country, city, order_by, profession_orientation, distance_range, location_dating) VALUES
(9, 20, 3, 'All Countries', '', 2, 1, '0,500', 0),
(10, 12, 3, 'Egypt', '', 2, 1, '', 0),
(11, 19, 3, 'All Countries', '', 2, 1, '0,500', 0),
(13, 20, 3, 'All Countries', '', 2, 1, '0,500', 0),
(14, 20, 3, 'All Countries', '', 2, 1, '0,500', 0),
(15, 20, 3, 'All Countries', '', 2, 1, '0,500', 0),
(25, 121, 3, 'All Countries', '', 3, 1, '0,500', 0),
(26, 316, 3, 'United States', '', 3, 1, '0,500', 0);
I removed third last column from insert script.
Hope this helps!

Wordpress Database importing giving strange error

I exported the database of a Wordpress. So far so good. But when i tried to import it through PHPMyAdmin from a cPanel i started receiving the following error:
INSERT INTO wp_commentmeta VALUES (
"1", "1", "_wp_trash_meta_status", "1" );
MySQL said: Documentation
1030 - Got error -1 from storage engine
I tried changing the browser, the host, exporting the database through different modules. Nothing worked.
I also tried fixing the database using the 'repair' function available in the cPanel but it didn't worked as i got this in response:
[projamz1_pds.wp_commentmeta] note: The storage engine for the table
doesn't support repair [projamz1_pds.wp_comments] note: The storage
engine for the table doesn't support repair
[projamz1_pds.wp_layerslider] note: The storage engine for the table
doesn't support repair [projamz1_pds.wp_links] note: The storage
engine for the table doesn't support repair [projamz1_pds.wp_options]
note: The storage engine for the table doesn't support repair
[projamz1_pds.wp_popover_ip_cache] note: The storage engine for the
table doesn't support repair [projamz1_pds.wp_postmeta] note: The
storage engine for the table doesn't support repair
[projamz1_pds.wp_posts] note: The storage engine for the table doesn't
support repair [projamz1_pds.wp_revslider_css] note: The storage
engine for the table doesn't support repair
[projamz1_pds.wp_revslider_layer_animations] note: The storage engine
for the table doesn't support repair
[projamz1_pds.wp_revslider_settings] note: The storage engine for the
table doesn't support repair [projamz1_pds.wp_revslider_sliders] note:
The storage engine for the table doesn't support repair
[projamz1_pds.wp_revslider_slides] note: The storage engine for the
table doesn't support repair [projamz1_pds.wp_revslider_static_slides]
note: The storage engine for the table doesn't support repair
[projamz1_pds.wp_term_relationships] note: The storage engine for the
table doesn't support repair [projamz1_pds.wp_term_taxonomy] note: The
storage engine for the table doesn't support repair
[projamz1_pds.wp_terms] note: The storage engine for the table doesn't
support repair [projamz1_pds.wp_usermeta] note: The storage engine for
the table doesn't support repair [projamz1_pds.wp_users] note: The
storage engine for the table doesn't support repair
The code that i suppose is the core of the problem is:
INSERT INTO wp_commentmeta (meta_id, comment_id, meta_key,
meta_value) VALUES (1, 1, '_wp_trash_meta_status', '1'), (2, 1,
'_wp_trash_meta_time', '1425058099'), (3, 18, '_wp_trash_meta_status',
'0'), (4, 18, '_wp_trash_meta_time', '1425058099'), (5, 34,
'_wp_trash_meta_status', '0'), (6, 34, '_wp_trash_meta_time',
'1425058099'), (7, 33, '_wp_trash_meta_status', '0'), (8, 33,
'_wp_trash_meta_time', '1425058099'), (9, 10, '_wp_trash_meta_status',
'0'), (10, 10, '_wp_trash_meta_time', '1425058099'), (11, 9,
'_wp_trash_meta_status', '0'), (12, 9, '_wp_trash_meta_time',
'1425058099'), (13, 19, '_wp_trash_meta_status', '1'), (14, 19,
'_wp_trash_meta_time', '1425058099'), (15, 23,
'_wp_trash_meta_status', '1'), (16, 23, '_wp_trash_meta_time',
'1425058099'), (17, 22, '_wp_trash_meta_status', '1'), (18, 22,
'_wp_trash_meta_time', '1425058099'), (19, 32,
'_wp_trash_meta_status', '0'), (20, 32, '_wp_trash_meta_time',
'1425058099'), (21, 31, '_wp_trash_meta_status', '0'), (22, 31,
'_wp_trash_meta_time', '1425058099'), (23, 3, '_wp_trash_meta_status',
'0'), (24, 3, '_wp_trash_meta_time', '1425058099'), (25, 30,
'_wp_trash_meta_status', '0'), (26, 30, '_wp_trash_meta_time',
'1425058099'), (27, 29, '_wp_trash_meta_status', '0'), (28, 29,
'_wp_trash_meta_time', '1425058099'), (29, 28,
'_wp_trash_meta_status', '0'), (30, 28, '_wp_trash_meta_time',
'1425058099'), (31, 15, '_wp_trash_meta_status', '0'), (32, 15,
'_wp_trash_meta_time', '1425058099'), (33, 27,
'_wp_trash_meta_status', '0'), (34, 27, '_wp_trash_meta_time',
'1425058099'), (35, 26, '_wp_trash_meta_status', '0'), (36, 26,
'_wp_trash_meta_time', '1425058099'), (37, 2, '_wp_trash_meta_status',
'0'), (38, 2, '_wp_trash_meta_time', '1425058099'), (39, 25,
'_wp_trash_meta_status', '0'), (40, 25, '_wp_trash_meta_time',
'1425058099'), (41, 24, '_wp_trash_meta_status', '0'), (42, 24,
'_wp_trash_meta_time', '1425058104'), (43, 8, '_wp_trash_meta_status',
'1'), (44, 8, '_wp_trash_meta_time', '1425058104'), (45, 7,
'_wp_trash_meta_status', '1'), (46, 7, '_wp_trash_meta_time',
'1425058104'), (47, 6, '_wp_trash_meta_status', '1'), (48, 6,
'_wp_trash_meta_time', '1425058104'), (49, 5, '_wp_trash_meta_status',
'1'), (50, 5, '_wp_trash_meta_time', '1425058104'), (51, 4,
'_wp_trash_meta_status', '1'), (52, 4, '_wp_trash_meta_time',
'1425058104'), (53, 11, '_wp_trash_meta_status', '1'), (54, 11,
'_wp_trash_meta_time', '1425058104'), (55, 14,
'_wp_trash_meta_status', '1'), (56, 14, '_wp_trash_meta_time',
'1425058104'), (57, 13, '_wp_trash_meta_status', '1'), (58, 13,
'_wp_trash_meta_time', '1425058104'), (59, 12,
'_wp_trash_meta_status', '1'), (60, 12, '_wp_trash_meta_time',
'1425058104'), (61, 17, '_wp_trash_meta_status', '1'), (62, 17,
'_wp_trash_meta_time', '1425058104'), (63, 16,
'_wp_trash_meta_status', '1'), (64, 16, '_wp_trash_meta_time',
'1425058104');
Tho' i did not wanted to delete it and import like that because i am afraid i will break something.
Any suggestions?
LE: I tried importing without the part i suspected being the problem. Without success. Still the same error - #1030 - Got error -1 from storage engine . But now i got 2 tables imported instead of one. Still cannot figure out the problem. As the wordpress is working without any kind of problem. I think something happens when i try to export the database.
Cheers
Some cheap and nasty shared hosting providers don't support all the MySQL access methods.
Try this: For each table find the part of the SQL file generated by your export that looks like this, for example.
CREATE TABLE `wp_options` (
`option_id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`option_name` VARCHAR(64) NOT NULL DEFAULT '',
`option_value` LONGTEXT NOT NULL,
`autoload` VARCHAR(20) NOT NULL DEFAULT 'yes',
PRIMARY KEY (`option_id`),
UNIQUE INDEX `option_name` (`option_name`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
;
Delete the line saying ENGINE=something, and retry your import. That will allow the MySQL server to use whatever ENGINE it has available.

MySQL database queries between queries

Here is my SQL file:
-- phpMyAdmin SQL Dump
-- version 3.4.7.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jan 23, 2012 at 09:26 AM
-- Server version: 5.1.56
-- PHP Version: 5.2.9
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `danielle_youtube`
--
-- --------------------------------------------------------
--
-- Table structure for table `items`
--
CREATE TABLE IF NOT EXISTS `items` (
`id` int(11) NOT NULL,
`name` varchar(65) COLLATE utf8_unicode_ci NOT NULL,
`cost` float NOT NULL,
`seller_id` int(11) NOT NULL,
`bids` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Dumping data for table `items`
--
INSERT INTO `items` (`id`, `name`, `cost`, `seller_id`, `bids`) VALUES
(1, 'Brand New iMac Computer', 149.99, 32, 3),
(2, 'used diaper from my sister', 2.04, 1, 0),
(3, 'Fresh apple pie', 14.99, 54, 32),
(4, 'New gym socks', 2.34, 90, 566),
(5, 'Weedwacker only slightly used', 4.56, 84, 2),
(6, 'New ipad stolen from best buy', 399, 32, 23),
(7, 'Book about having babies', 21.34, 44, 21),
(8, 'Woman Jeans', 49.5, 56, 123),
(9, 'traditional carpet', 25.45, 14, 75),
(10, '3 boxes of frogs', 30.49, 68, 145),
(11, '48 boxes of frogs', 74.29, 6, 99),
(12, '7 boxes of frogs', 857.75, 18, 88),
(13, 'laptop', 743.3, 89, 158),
(14, 'thumbelina', 228.05, 15, 49),
(15, 'bed', 127.15, 65, 189),
(16, 'shampoing', 12.8, 6, 105),
(17, 'stove', 37.66, 68, 111),
(18, 'cushion', 7.15, 97, 157),
(19, 'refrigerator', 657.49, 61, 129),
(20, 'gold necklace', 853.07, 10, 101),
(21, 'pan', 33.7, 7, 184),
(22, 'awesome alien computer game', 10.75, 18, 29),
(23, 'baby coat', 89.99, 14, 47),
(24, 'baby seat', 145.78, 2, 199),
(25, 'satchel', 44.71, 15, 66),
(26, 'women perfum', 110.9, 48, 84),
(27, 'conveyor belt', 1120.75, 11, 4),
(28, 'used car', 5700.5, 12, 135),
(29, 'supercomputer', 49.75, 50, 176),
(30, 'mirror', 26.8, 19, 56),
(31, 'piano', 1800.4, 13, 147),
(32, 'quitar', 88.4, 25, 164),
(33, 'trumpet', 255.15, 36, 23),
(34, 'machintosh', 3845, 20, 107),
(35, 'earphone', 10.5, 17, 110),
(36, 'computer', 418, 11, 152),
(37, 'night light', 13.87, 97, 198),
(38, 'pc bag', 50.99, 48, 65),
(39, 'babyfoot', 376.7, 2, 121),
(40, 'hairdryer', 88.9, 12, 177),
(41, 'babyliss', 130.75, 68, 79),
(42, 'door', 150.5, 98, 13),
(43, 'baby soap', 12.7, 4, 198),
(44, 'used phone', 43.75, 9, 69),
(45, 'bath', 757.15, 96, 55),
(46, 'flower', 10.75, 16, 89),
(47, 'battery charger', 48.75, 25, 87),
(48, 'air conditioner', 975, 12, 151),
(49, 'casserole', 115.75, 46, 35),
(50, 'used toilet', 180.7, 64, 11),
(51, 'teashirt', 14.98, 65, 114),
(52, 'moto', 920, 22, 174),
(53, 'saxophone', 220.9, 60, 140),
(54, 'bicycle', 180.55, 97, 35),
(55, 'man perfum', 95, 75, 199),
(56, 'table', 157.25, 91, 48),
(57, 'boat', 4890.5, 17, 177),
(58, 'iphone', 547, 8, 28),
(59, 'body milk', 50.5, 16, 90),
(60, 'new curtain for bedroom', 278.4, 92, 11),
(61, 'diamond ring', 1900, 15, 45),
(62, 'swept', 4.5, 9, 99),
(63, 'women hat', 17.55, 39, 60),
(64, 'washing machine', 680.9, 42, 125),
(65, 'baby bottle', 27.98, 91, 117),
(66, 'women sun glasses', 66.7, 18, 174),
(67, 'person weighs', 65.25, 10, 100),
(68, 'photo frame', 18, 85, 170),
(69, 'key board', 16.7, 90, 101),
(70, 'screen', 250, 81, 188),
(71, 'bucket', 2.5, 1, 19),
(72, 'lipstick', 24.75, 3, 44),
(73, 'wardrobe', 120.75, 9, 71),
(74, 'blue dress size 40', 88.9, 7, 113),
(75, 'newspaper', 1.5, 95, 172),
(76, 'scanner', 350, 14, 62),
(77, 'camera', 550.7, 17, 95),
(78, 'camcorder', 788.99, 25, 127),
(79, 'gun', 420.1, 81, 107),
(80, 'domestic dog', 200, 19, 129),
(81, 'horse', 759.5, 30, 115),
(82, 'truck', 7800.5, 32, 123),
(83, 'soccer ball', 95.49, 54, 155),
(84, 'gold earring', 385, 75, 92),
(85, 'basket', 250.45, 46, 142),
(86, 'bikini', 85.2, 12, 57),
(87, 'red skirt', 15.9, 18, 188),
(88, 'copier machine', 800.7, 50, 160),
(89, 'handbag', 35.9, 8, 108),
(90, 'bath towel', 25.1, 11, 186),
(91, 'coffee machine', 210.89, 15, 170),
(92, 'wedding dress', 690, 26, 48),
(93, 'man sun glasses', 80.7, 19, 174),
(94, 'candle', 7.5, 22, 102),
(95, 'scarf', 11.9, 7, 143),
(96, 'microwave', 150.29, 6, 11),
(97, 'electric oven', 645, 62, 171),
(98, 'play station', 256.75, 12, 188),
(99, 'dvd', 126.84, 14, 113),
(100, 'magazine', 3.5, 8, 152);
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;`
Now, when I run this query:
SELECT name , MIN(cost) FROM items WHERE name LIKE '% boxes of frogs' AND seller_id IN (
SELECT seller_id FROM items WHERE name LIKE '% boxes of frogs'
)
I get
name: 3 boxes of frogs
MIN(cost): 30.489999771118164
But when I use:
SELECT name , MIN(cost) FROM items WHERE seller_id IN(
SELECT seller_id FROM items WHERE name LIKE '% boxes of frogs'
)
I get this result:
name: 3 boxes of frogs
MIN(cost): 10.75
I want to know why the results are different?
The first query returns the name of the item and the cheapest price from items of a box of frogs (see the condition on name in the external query).
The second query returns the name of the item and the cheapest price from items of any item sold by someone who is also selling a box of frogs (the condition on name is only applied in the internal condition).

Bulk insert attribute sets in Magento database

I need to insert 30,000 attribute sets in Magento database. I tried it as follows
set_time_limit(0);
function createAttributeSet($setName)
{
$entityTypeId = Mage::getModel('eav/entity')
->setType('catalog_product')
->getTypeId(); // 4 - Default
$newSet = Mage::getModel('eav/entity_attribute_set');
$newSet->setEntityTypeId($entityTypeId);
$newSet->setAttributeSetName($setName);
$newSet->save();
$newSet->initFromSkeleton($entityTypeId);
$newSet->save();
}
for($i=1; $i<=30000; $i++)
{
$setName = 'rug_'.$i;
createAttributeSet($setName);
}
Above script works fine but is very slow, it takes 12 seconds to insert one attribute set, that means 6000 minute needed to insert 30k attribute sets, I am using Corei7.
Edit:
Instead of using data models that were used in previous script I decided to use direct queries. To do so I dig entire database structure for attribute sets. By reverse engineering the Magento data models I created a new script containing direct queries. This script gives amazing performance.
All 30,000 attribute sets have been inserted to eav_attribute_se but there is a limitation of 65535 records on eav_attribute_group table, so no groups were inserted against attributes sets having id above 8198. Hence 8198 attribute sets can be functional at a time, here is the code.
for ($i = 1; $i <= 30000; $i++)
{
$attribute_set_name = 'rug_' . $i;
$sql_1 = "INSERT INTO `mg_eav_attribute_set` (`entity_type_id`, `attribute_set_name`, `sort_order`) VALUES (4, '$attribute_set_name', $i)";
$result_1 = mysql_query($sql_1);
$attribute_set_id = mysql_insert_id();
if ($attribute_set_id > 0)
{
$attribute_groups = array(
'Prices',
'General',
'Meta Information',
'Images',
'Recurring Profile',
'Design',
'Gift Options',
'Selectable Options'
);
$sql_2 = "INSERT INTO `mg_eav_attribute_group` (`attribute_set_id`, `attribute_group_name`, `sort_order`, `default_id`) VALUES
($attribute_set_id, 'Gift Options', 7, 0),
($attribute_set_id, 'Design', 6, 0),
($attribute_set_id, 'Recurring Profile', 5, 0),
($attribute_set_id, 'Images', 4, 0),
($attribute_set_id, 'Meta Information', 3, 0),
($attribute_set_id, 'Prices', 2, 0),
($attribute_set_id, 'General', 1, 1),
($attribute_set_id, 'Selectable Options', 8, 0);";
$result_2 = mysql_query($sql_2);
$sql_3 = "SELECT `attribute_group_id`, `attribute_group_name` FROM `mg_eav_attribute_group` WHERE `attribute_set_id` = $attribute_set_id;";
$result_3 = mysql_query($sql_3);
while ($row = mysql_fetch_array($result_3))
{
$gid = trim($row['attribute_group_id']);
$game = trim($row['attribute_group_name']);
switch ($game)
{
case 'Prices':
$sql4 = "INSERT INTO `mg_eav_entity_attribute` (`entity_type_id`, `attribute_set_id`, `attribute_group_id`, `attribute_id`, `sort_order`) VALUES
(4, $attribute_set_id, $gid, 99, 8),
(4, $attribute_set_id, $gid, 75, 1),
(4, $attribute_set_id, $gid, 76, 3),
(4, $attribute_set_id, $gid, 77, 4),
(4, $attribute_set_id, $gid, 78, 5),
(4, $attribute_set_id, $gid, 79, 6),
(4, $attribute_set_id, $gid, 90, 2),
(4, $attribute_set_id, $gid, 91, 7),
(4, $attribute_set_id, $gid, 118, 8),
(4, $attribute_set_id, $gid, 119, 9),
(4, $attribute_set_id, $gid, 120, 10),
(4, $attribute_set_id, $gid, 121, 11),
(4, $attribute_set_id, $gid, 122, 12),
(4, $attribute_set_id, $gid, 127, 13)";
mysql_query($sql4);
break;
case 'General':
$sql4 = "INSERT INTO `mg_eav_entity_attribute` (`entity_type_id`, `attribute_set_id`, `attribute_group_id`, `attribute_id`, `sort_order`) VALUES
(4, $attribute_set_id, $gid, 89, 6),
(4, $attribute_set_id, $gid, 98, 11),
(4, $attribute_set_id, $gid, 108, 13),
(4, $attribute_set_id, $gid, 110, 14),
(4, $attribute_set_id, $gid, 111, 15),
(4, $attribute_set_id, $gid, 112, 16),
(4, $attribute_set_id, $gid, 113, 17),
(4, $attribute_set_id, $gid, 114, 18),
(4, $attribute_set_id, $gid, 115, 19),
(4, $attribute_set_id, $gid, 116, 20),
(4, $attribute_set_id, $gid, 124, 22),
(4, $attribute_set_id, $gid, 125, 23),
(4, $attribute_set_id, $gid, 126, 24),
(4, $attribute_set_id, $gid, 128, 25),
(4, $attribute_set_id, $gid, 129, 26),
(4, $attribute_set_id, $gid, 130, 27),
(4, $attribute_set_id, $gid, 131, 28),
(4, $attribute_set_id, $gid, 132, 29),
(4, $attribute_set_id, $gid, 71, 1),
(4, $attribute_set_id, $gid, 72, 2),
(4, $attribute_set_id, $gid, 73, 3),
(4, $attribute_set_id, $gid, 74, 4),
(4, $attribute_set_id, $gid, 80, 5),
(4, $attribute_set_id, $gid, 93, 6),
(4, $attribute_set_id, $gid, 94, 7),
(4, $attribute_set_id, $gid, 96, 8),
(4, $attribute_set_id, $gid, 97, 9),
(4, $attribute_set_id, $gid, 102, 10),
(4, $attribute_set_id, $gid, 117, 11)";
mysql_query($sql4);
break;
case 'Meta Information':
$sql4 = "INSERT INTO `mg_eav_entity_attribute` (`entity_type_id`, `attribute_set_id`, `attribute_group_id`, `attribute_id`, `sort_order`) VALUES
(4, $attribute_set_id, $gid, 82, 1),
(4, $attribute_set_id, $gid, 83, 2),
(4, $attribute_set_id, $gid, 84, 3)";
mysql_query($sql4);
break;
case 'Images':
$sql4 = "INSERT INTO `mg_eav_entity_attribute` (`entity_type_id`, `attribute_set_id`, `attribute_group_id`, `attribute_id`, `sort_order`) VALUES
(4, $attribute_set_id, $gid, 85, 1),
(4, $attribute_set_id, $gid, 86, 2),
(4, $attribute_set_id, $gid, 87, 3),
(4, $attribute_set_id, $gid, 88, 4),
(4, $attribute_set_id, $gid, 95, 5)";
mysql_query($sql4);
break;
case 'Recurring Profile':
$sql4 = "INSERT INTO `mg_eav_entity_attribute` (`entity_type_id`, `attribute_set_id`, `attribute_group_id`, `attribute_id`, `sort_order`) VALUES
(4, $attribute_set_id, $gid, 100, 1),
(4, $attribute_set_id, $gid, 101, 2)";
mysql_query($sql4);
break;
case 'Design':
$sql4 = "INSERT INTO `mg_eav_entity_attribute` (`entity_type_id`, `attribute_set_id`, `attribute_group_id`, `attribute_id`, `sort_order`) VALUES
(4, $attribute_set_id, $gid, 103, 1),
(4, $attribute_set_id, $gid, 104, 2),
(4, $attribute_set_id, $gid, 105, 3),
(4, $attribute_set_id, $gid, 106, 4),
(4, $attribute_set_id, $gid, 107, 5),
(4, $attribute_set_id, $gid, 109, 6)";
mysql_query($sql4);
break;
case 'Gift Options':
$sql4 = "INSERT INTO `mg_eav_entity_attribute` (`entity_type_id`, `attribute_set_id`, `attribute_group_id`, `attribute_id`, `sort_order`) VALUES
(4, $attribute_set_id, $gid, 123, 1)";
mysql_query($sql4);
break;
case 'Selectable Options':
$sql4 = "INSERT INTO `mg_eav_entity_attribute` (`entity_type_id`, `attribute_set_id`, `attribute_group_id`, `attribute_id`, `sort_order`) VALUES
(4, $attribute_set_id, $gid, 134, 1);";
mysql_query($sql4);
break;
}
}
}
}