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!
Related
i am very new to SQL and have to complete my first ever project. i keep getting errors to do with syntax and i have no idea where i've gone wrong, i've checked for commas, hyphens in the wrong place and feel like i'm going mad. can someone help me please?
the errors:
1. (49, 'Purple Kiss', null, '퍼플키스', '2021-03-15', 'RBW', 7, 7, 'PLORY', true), "null" is not valid at this position, expecting ')'
(again, where is the null coming from?)
2. (37, 'Uni.T', null, '유니티', '2018-05-18', 'Unit Culture', 8, 9, false), extraneous input false found, expecting ')'
(what is an extraneous input?)
3. (26, 'The Ark', null, '디아크', '2015-04-12', 'Music K', 5, 5, null, false),
9 is not valid at this position, expecting ')'
(not sure where the 9 is coming from)
it might be a really simple fix, but again, this is my first project and have no idea what's wrong, please help! i don't know if it's something to do with the korean characters? i read somewhere to use 'NVARCHAR' as the datatype so that's what i used, please explain it to me like i'm a child xD
create database female_kpop_idols;
use female_kpop_idols;
create table kpop_girlgroups
(idol_id int primary key auto_increment not null,
eng_name varchar(50) not null,
short_name varchar(50),
kor_name nvarchar(50) not null,
debut date not null,
company varchar(50) not null,
members int not null,
orig_members int not null,
fanclub varchar(50),
active boolean);
insert into kpop_girlgroups
values
(1, 'Baby V.O.X', null, '베이비복스', '1997-07-05', 'DR Music', 5, 5, null, false),
(2, 'S.E.S', null, '에스.이.에스', '1997-11-01', 'SM', 3, 3, null, false),
(3, 'Fin.K.L', null, '핑클', '1998-05-22', 'DSP', 4, 4, null,
(4, 'Brown Eyed Girls', 'B.E.G', '브라운 아이드 걸스 ', '2006-03-02', 'Mystic', 4, 4, 'Everlasting', true),
(5, 'Wonder Girls', 'WG', '원더걸스', '2007-02-13', 'JYP', 4, 5, 'Wonderful', false),
(6, 'Kara', null, '카라', '2007-03-29', 'DSP', 4, 5, 'Kamilia', false),
(7, 'Girls\' Generation', 'SNSD', '2007-08-05', 'SM', 5, 9, 'SONE', true),
(8, 'After School', null, '애프터스쿨', '2009-01-15', 'Pledis', 6, 5, 'Playgirlz', false),
(9, '2NE1', null, '투애니원', '2009-05-06', 'YG', 3, 4, 'Blackjack', false),
(10, '4Minute', null, '포미닛', '2009-06-15', 'Cube', 5, 5, '4nia', false),
(11, 'T-ara', null, '티아라', '2009-07-29', 'MBK', 4, 6, 'Queen\'s', true), -- hiatus --
(12, 'f(x)', null, '에프엑스', '2009-08-24', 'SM', 4, 5, 'MeU', false),
(13, 'Sistar', null, '씨스타', '2010-03-06', 'Starship', 4, 4, 'STAR1', false),
(14, 'missA', null, '미쓰에이', '2010-07-01', 'JYP', 3, 4, 'Say A', false),
(15, 'Girl\'s Day', 'GsD', '걸스데이', '2010-07-09', 'DreamT', 4, 5, 'Dai5y', true), -- hiatus --
(16, 'Rania', null, '라니아', '2011-04-06', 'DR Music', 5, 8, null, false), -- redebuted as Black Swan --
(17, 'Brave Girls', null, '브레이브걸스', '2011-04-08', 'Brave', 4, 5, null, true),
(18, 'Apink', null, '에이핑크', '2011-04-19', 'Plan A', 6, 7, 'Pink Panda', true),
(19, 'EXID', null, '이엑스아이디', '2012-02-16', 'Yedang', 5, 6, 'LEGO', true),
(20, 'AOA', null, '에이오에이', '2012-07-30', 'FNC', 4, 8, 'ELVIS', true), -- hiatus --
(21, 'Mamamoo', null, '마마무', '2014-06-18', 'RBW', 4, 4, 'Moomoo', true),
(22, 'Red Velevet', 'RV', '레드벨벳', '2014-08-01', 'SM', 5, 4, true),
(23, 'Minx', null, '밍스', '2014-09-18', 'Happy Face', 5, 5, false), -- redebuted as DreamCatcher --
(24, 'Gfriend', null, '여자친구', '2015-01-15', 'Source', 6, 6, 'Buddy', false),
(25, 'CLC', null, '씨엘씨', '2015-03-19', 'Cube', 7, 5, 'Cheshire', false),
(26, 'The Ark', null, '디아크', '2015-04-12', 'Music K', 5, 5, null, false), -- some members redebuted as KHAN --
(27, 'Oh My Girl', 'OHMG', '오마이걸', '2015-04-21', 'WM', 6, 8, 'Miracle', true),
(28, 'TWICE', null, '트와이스', '2015-10-20', 'JYP', '9, '9, 'ONCE', true),
(29, 'WJSN', 'Cosmic Girls', '2016-02-25', 'Starship', 13, 12, 'Ujung', true),
(30, 'IOI', 'I.O.I', '아이오아이', '2016-05-04', 'YMC', 11, 11, null, false),
(31, 'BLACKPINK', null, '블랙핑크', '2016-08-08', 'YG', 4, 4, 'BLINK', true),
(32, 'Momoland', null, '모모랜드', '2016-11-09', 'Duble Kick', 9, 6, true),
(33, 'DreamCatcher', null, '드림캐쳐', '2017-01-13', 'Happy Face', 7, 7, 'InSomnia', true),
(34, 'Pristin', null, '프리스틴', '2017-03-21', 'Pledis', 10, 10, 'HIgh', false), -- some redebuted as Hinapia --
(35, 'fromis_9', null, '프로미스나인', '2018-01-24', 'Pledis', 9, 9, 'Flover', true),
(36, '(G)I-DLE', null, '(여자)아이들', '2018-05-02', 'Cube', 5, 6, 'Neverland', true),
(37, 'Uni.T', null, '유니티', '2018-05-18', 'Unit Culture', 8, 9, false), -- come back to this one and add Sonamoo, SPICA, DIA, April, Dal Shabet, Laboum, The ARK --
(38, 'KHAN', null, '칸', '2018-05-23', 'Maroo', 2, 2, false),
(39, 'Loona', 'LOOΠΔ', '이달의 소녀', '2018-08-20', 'Blockberry', 12, 12, 'Orbit', true),
(40, 'IZ*ONE', 'IZONE', '아이즈원', '2018-10-29', 'Off The Record', 12, 12, 'WIZ*ONE' false),
(41, 'Cherry Bullet', null, '체리블렛', '2019-01-21', 'FNC', 7, 10, 'Lullet', true),
(42, 'ITZY', null, '있지', '2019-02-12', 'JYP', 5, 5, 'MIDZY', true),
(43, 'Everglow', null, '에버글로우 ', '2019-03-18', 'Yuehua', 6, 6, 'Forever', true),
(44, 'Rocket Punch', null, '로켓펀치', '2019-08-07', 'Woollim', 6, 6, 'Ketchy', true),
(45, 'Hinapia', null, '희나피아', '2019-11-03', 'Alseubit', 5, 5, 'UBY', false);
INSERT INTO `2018_players`(`p_id`, `player_fname`, `player_lname`, `stats_id`, `home_id`) VALUES (
(1, 'LeBron', 'James', 2, 3, 23),
(2, 'Stephen', 'Curry', 4, 5, 30),
(3, 'James', 'Harden', 6, 7, 13),
(4, 'Giannis', 'Antekokounmpo', 8, 9, 34),
(5, 'Paul', 'George', 10, 11, 13);
Server version: 5.7.23
1 error were found during analysis.
#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 '' at line 7
You are specifying 5 columns, but trying to insert 6 values per row, and there is an extra bracket after VALUES, you want something like:
INSERT INTO `2018_players`(`p_id`, `player_fname`, `player_lname`, `stats_id`, `home_id`, `extra_column`) VALUES
(1, 'LeBron', 'James', 2, 3, 23),
(2, 'Stephen', 'Curry', 4, 5, 30),
(3, 'James', 'Harden', 6, 7, 13),
(4, 'Giannis', 'Antekokounmpo', 8, 9, 34),
(5, 'Paul', 'George', 10, 11, 13);
I am trying to write a query wherein it should count the number of students and tell me the remaining seats available in a vehicle.
Have managaed to identify which student is associated to which bus but getting stuck to find the seat remaining
Below is data :
vehnum route seats student id
23 2 45 2345
33 3 46 6789
Below is the query :
SELECT deveh.vehicle_reg_no AS vehnum
, veh.route_code AS route
, deveh.seating_capacity AS vehseat
, class.fk_stu_id
FROM tbl_stu_class AS class
JOIN tbl_stu_route AS route
ON route.fk_stu_cls_id = class.pk_stu_cls_id
JOIN list_routes AS veh
ON route.fk_route_id = veh.pk_route_id
JOIN list_vehicles AS deveh
ON deveh.pk_vehicle_id = veh.fk_vehicle_id
WHERE class.fk_year_id = 62
AND class.current_yr = 'Y'
Added sample data :
INSERT INTO `list_vehicles` (`pk_vehicle_id`, `vehicle_reg_no`, `vehicle_type`, `regd_owner_name`, `seating_capacity`, `brand_model`, `type_of_body`, `reg_address`, `fuel_type`, `chasis_no`, `reg_authority`, `engine_no`, `color`, `reg_date`, `reg_valid_date`, `month_yr_mfg`, `fk_user_id`, `timestamp`) VALUES
(46, 'J58987', 'Bus', 'M', 30, 'VOlvo', 'Steel', 'FBD', 'Petrol', '565', 'M1', '5689', 'blue', '2016-10-02', '2016-10-02', '2014-12-31', 1, '2018-07-11 18:01:06'),
(53, 'J1234', 'Bus', 'der', 45, 'Volvo', 'Metal', 'Indirapuram', 'Petrol', '123456', 'det', '2365', 'blue', '2010-12-12', '2020-12-12', '2009-12-11', 1, '2018-07-12 06:54:50'),
(54, 'J1234er', 'Van', 'der', 46, 'Volvo', 'Metal', 'Indirapuram', 'Petrol', '12345634', 'det', '236534', 'blue', '2020-02-03', '2020-02-03', '2008-11-11', 1, '2018-07-12 06:57:59');
INSERT INTO `tbl_stu_class` (`pk_stu_cls_id`, `fk_stu_id`, `fk_year_id`, `fk_class_id`, `fk_section_id`, `current_yr`, `fk_user_id`, `timestamp`) VALUES
(1, 56, 50, 22, 10, 'N', 1, '2018-06-08 06:57:34'),
(3, 123, 50, 24, 7, 'N', 1, '2018-06-12 07:54:46'),
(4, 126, 50, 24, 7, 'N', 56, '2018-06-12 07:54:46'),
(5, 123, 52, 25, 7, 'Y', 1, '2018-06-12 17:30:32'),
(6, 126, 52, 25, 7, 'Y', 1, '2018-06-12 17:30:32'),
(7, 132, 50, 22, 9, 'Y', 1, '2018-06-24 10:27:57'),
(8, 133, 51, 23, NULL, 'Y', 1, '2018-06-24 18:22:33'),
(10, 127, 51, 23, NULL, 'Y', 0, '2018-07-11 17:47:05'),
(11, 134, 62, 22, NULL, 'Y', 0, '2018-07-13 08:11:16'),
(12, 135, 62, 21, 7, 'Y', 1, '2018-07-13 11:12:08'),
(13, 136, 62, 21, 9, 'Y', 1, '2018-07-13 14:59:04');
I'm trying to make an average list for a bowling club. I have a database with the following tables:
team_medlem (medlem_id, name, address, etc)
team_samling (match_id, lag_id, borta (0 = home game, 1 = away game), date, time, etc)
team_match (id, match_id, s1, s2, etc)
team_resultat (id, medlem_id, results, series, banp, match_id)
I want to get the total average, average home and away average. result is the total points for the entire game for each player and the series is the number of played series.
I have this:
SELECT team_resultat.medlem_id, team_medlem.namn, sum(resultat)/sum(serier) as snitt, sum(banp)
FROM team_resultat
JOIN team_medlem ON team_resultat.medlem_id = team_medlem.medlem_id
JOIN team_samling ON team_resultat.match_id = team_samling.match_id
WHERE datum >= current_date - interval '1' year
GROUP BY namn
ORDER BY snitt desc
How can I get home average and away average?
I have tried this but it miscalculated averages
SELECT team_resultat.medlem_id, team_medlem.namn, sum(resultat)/sum(serier) as snitt, sum(banp), avg(case when not borta then resultat/serier end) as hemmasnitt, avg(case when borta then resultat/serier end) as bortasnitt
FROM team_resultat
JOIN team_medlem ON team_resultat.medlem_id = team_medlem.medlem_id
JOIN team_samling ON team_resultat.match_id = team_samling.match_id
WHERE datum >= current_date - interval '1' year
GROUP BY namn
ORDER BY snitt desc
Sampeldata:
-- phpMyAdmin SQL Dump
-- version 4.1.14
-- http://www.phpmyadmin.net
--
-- Värd: 127.0.0.1
-- Tid vid skapande: 03 nov 2014 kl 20:07
-- Serverversion: 5.6.17
-- PHP-version: 5.5.12
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Databas: `teamet`
--
-- --------------------------------------------------------
--
-- Tabellstruktur `team_medlem`
--
CREATE TABLE IF NOT EXISTS `team_medlem` (
`medlem_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`namn` varchar(100) COLLATE utf8_swedish_ci NOT NULL,
PRIMARY KEY (`medlem_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci COMMENT='Medlems register'AUTO_INCREMENT=36 ;
--
-- Dumpning av Data i tabell `team_medlem`
--
INSERT INTO `team_medlem` (`medlem_id`, `namn`) VALUES
(1, 'Sven-Åke Jansson'),
(2, 'Christer Wendel'),
(4, 'Sören Carlsson'),
(5, 'Jan Ingvarsson'),
(6, 'Lars-Göran Wetterholm'),
(7, 'Anders Svensson'),
(8, 'Bengt Carlsson'),
(9, 'Per-Olof Johansson'),
(10, 'Barsom Calan'),
(11, 'Mikael Mårtensson'),
(12, 'Andreas Johansson'),
(13, 'Jonas Wendel'),
(14, 'Sören Fransson'),
(15, 'Daniel Fransson'),
(16, 'Stefan Lord'),
(18, 'Lennart Johansson'),
(19, 'Jonas Nilsson'),
(20, 'Mikael Nilsson'),
(21, 'Patrik Emanuelsson'),
(22, 'Jörgen Norman'),
(24, 'Anders Johansson'),
(25, 'Andreas Larsson'),
(26, 'Roger Larsson'),
(27, 'Peter Ericson'),
(28, 'Dado Hrnic'),
(29, 'Maria Lord-Johansson'),
(33, 'Mats Wellermark');
-- phpMyAdmin SQL Dump
-- version 4.1.14
-- http://www.phpmyadmin.net
--
-- Värd: 127.0.0.1
-- Tid vid skapande: 03 nov 2014 kl 20:13
-- Serverversion: 5.6.17
-- PHP-version: 5.5.12
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Databas: `teamet`
--
-- --------------------------------------------------------
--
-- Tabellstruktur `team_samling`
--
CREATE TABLE IF NOT EXISTS `team_samling` (
`match_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`lag_id` int(11) DEFAULT NULL,
`omg` int(2) unsigned DEFAULT NULL,
`borta` tinyint(1) unsigned DEFAULT '0',
`datum` date DEFAULT NULL,
`tid` time DEFAULT NULL,
`motstandare` varchar(50) COLLATE utf8_swedish_ci DEFAULT NULL,
`samling` varchar(6) COLLATE utf8_swedish_ci DEFAULT NULL,
`ovrigt` varchar(50) COLLATE utf8_swedish_ci DEFAULT NULL,
PRIMARY KEY (`match_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci COMMENT='Samling' AUTO_INCREMENT=14 ;
--
-- Dumpning av Data i tabell `team_samling`
--
INSERT INTO `team_samling` (`match_id`, `lag_id`, `omg`, `borta`, `datum`, `tid`, `motstandare`, `samling`, `ovrigt`) VALUES
(1, 1, 1, 0, '2014-10-28', '12:15:00', 'Forsheda', '11:30', 'match dräkt'),
(2, 2, 1, 0, '2014-10-27', '10:00:00', 'Jönköping KK', '09:15', ''),
(3, 3, 3, 0, '2014-10-30', '15:20:00', 'Alvesta', '14:30', ''),
(4, 2, 2, 1, '2014-11-05', '12:00:00', 'Sävsjö', '09:15', 'Buss'),
(5, 2, 2, 1, '2014-11-05', '14:00:00', 'Eksjö', NULL, NULL),
(6, 3, 2, 0, '2014-11-06', '11:15:00', 'Jönköping kk', '10:30', NULL),
(11, 1, 20, 1, '2014-11-05', '12:30:00', 'test borta 2', '09:30', NULL),
(13, 1, 22, 0, '2014-11-06', '10:00:00', 'Test hemma', '09:00', NULL);
-- phpMyAdmin SQL Dump
-- version 4.1.14
-- http://www.phpmyadmin.net
--
-- Värd: 127.0.0.1
-- Tid vid skapande: 03 nov 2014 kl 20:16
-- Serverversion: 5.6.17
-- PHP-version: 5.5.12
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Databas: `teamet`
--
-- --------------------------------------------------------
--
-- Tabellstruktur `team_resultat`
--
CREATE TABLE IF NOT EXISTS `team_resultat` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`medlem_id` int(11) unsigned DEFAULT NULL,
`resultat` decimal(5,2) unsigned DEFAULT NULL,
`serier` int(2) unsigned DEFAULT NULL,
`banp` int(1) unsigned DEFAULT NULL,
`match_id` int(11) unsigned DEFAULT NULL,
`resultat_sparad` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci COMMENT='Match resultat' AUTO_INCREMENT=53 ;
--
-- Dumpning av Data i tabell `team_resultat`
--
INSERT INTO `team_resultat` (`id`, `medlem_id`, `resultat`, `serier`, `banp`, `match_id`, `resultat_sparad`) VALUES
(1, 6, '800.00', 4, 3, 2, '2014-10-11 17:11:37'),
(2, 7, '790.00', 4, 3, 2, '2014-10-11 17:11:37'),
(3, 11, '780.00', 4, 2, 2, '2014-10-11 17:11:37'),
(4, 12, '770.00', 4, 2, 2, '2014-10-11 17:11:37'),
(5, 14, '760.00', 4, 3, 2, '2014-10-11 17:11:37'),
(6, 15, '750.00', 4, 2, 2, '2014-10-11 17:11:37'),
(7, 24, '740.00', 4, 2, 2, '2014-10-11 17:11:37'),
(8, 25, '505.00', 3, 1, 2, '2014-10-11 17:11:37'),
(9, 27, '165.00', 1, 0, 2, '2014-10-11 17:11:37'),
(10, 8, '700.00', 4, 2, 1, '2014-10-26 14:29:20'),
(11, 9, '690.00', 4, 3, 1, '2014-10-26 14:29:20'),
(12, 10, '680.00', 4, 2, 1, '2014-10-26 14:29:20'),
(13, 13, '670.00', 4, 2, 1, '2014-10-26 14:29:20'),
(14, 19, '660.00', 4, 2, 1, '2014-10-26 14:29:20'),
(15, 20, '650.00', 4, 1, 1, '2014-10-26 14:29:20'),
(16, 22, '640.00', 4, 2, 1, '2014-10-26 14:29:20'),
(17, 25, '640.00', 4, 1, 1, '2014-10-26 14:29:20'),
(19, 1, '800.00', 4, 2, 3, '2014-10-26 14:31:00'),
(20, 2, '790.00', 4, 2, 3, '2014-10-26 14:31:00'),
(21, 4, '780.00', 4, 2, 3, '2014-10-26 14:31:00'),
(22, 5, '770.00', 4, 2, 3, '2014-10-26 14:31:00'),
(23, 7, '760.00', 4, 2, 3, '2014-10-26 14:31:00'),
(24, 8, '750.00', 4, 2, 3, '2014-10-26 14:31:00'),
(25, 14, '740.00', 4, 3, 3, '2014-10-26 14:31:00'),
(26, 15, '500.00', 3, 1, 3, '2014-10-26 14:31:00'),
(27, 18, '150.00', 1, 1, 3, '2014-10-26 14:31:00'),
(28, 2, '800.00', 4, 2, 4, '2014-10-26 16:57:47'),
(29, 6, '790.00', 4, 2, 4, '2014-10-26 16:57:47'),
(30, 11, '780.00', 4, 2, 4, '2014-10-26 16:57:47'),
(31, 12, '770.00', 4, 2, 4, '2014-10-26 16:57:47'),
(32, 14, '760.00', 4, 2, 4, '2014-10-26 16:57:47'),
(33, 15, '750.00', 4, 2, 4, '2014-10-26 16:57:47'),
(34, 25, '740.00', 4, 2, 4, '2014-10-26 16:57:47'),
(35, 33, '730.00', 4, 2, 4, '2014-10-26 16:57:47'),
(36, 2, '800.00', 4, 2, 5, '2014-10-26 16:58:26'),
(37, 6, '790.00', 4, 2, 5, '2014-10-26 16:58:26'),
(38, 11, '780.00', 4, 2, 5, '2014-10-26 16:58:26'),
(39, 12, '770.00', 4, 2, 5, '2014-10-26 16:58:26'),
(40, 14, '760.00', 4, 2, 5, '2014-10-26 16:58:26'),
(41, 15, '750.00', 4, 2, 5, '2014-10-26 16:58:26'),
(42, 25, '740.00', 4, 2, 5, '2014-10-26 16:58:26'),
(43, 33, '730.00', 4, 2, 5, '2014-10-26 16:58:26'),
(44, 10, '700.00', 4, 2, 6, '2014-10-26 16:59:15'),
(45, 13, '690.00', 4, 2, 6, '2014-10-26 16:59:15'),
(46, 16, '680.00', 4, 2, 6, '2014-10-26 16:59:15'),
(47, 19, '670.00', 4, 2, 6, '2014-10-26 16:59:15'),
(48, 18, '660.00', 4, 2, 6, '2014-10-26 16:59:15'),
(49, 21, '650.00', 4, 2, 6, '2014-10-26 16:59:15'),
(50, 26, '640.00', 4, 2, 6, '2014-10-26 16:59:15'),
(51, 27, '520.00', 3, 2, 6, '2014-10-26 16:59:15'),
(52, 29, '160.00', 1, 2, 6, '2014-10-26 16:59:15');
expected results
Namn Snitt Hemma Borta Banp
Sven-Åke Jansson 200,00 200,00 0 2
Christer Wendel 199,17 197,50 200,00 6
Lars-Göran Wetterholm 198,33 200,00 197,50 7
Mikael Mårtensson 195,00 195,00 195,00 6
Sören Carlsson 195,00 195,00 0 2
Anders Svensson 193,75 193,75 0 5
Andreas Johansson 192,50 192,50 192,50 6
Jan Ingvarsson 192,50 192,50 0 2
Sören Fransson 188,75 187,50 190,00 10
Anders Johansson 185,00 185,00 0 2
Daniel Fransson 183,33 178,57 187,50 7
Mats Wellermark 182,50 0 182,50 4
Bengt Carlsson 181,25 181,25 0 4
Andreas Larsson 175,00 163,57 185,00 6
Barsom Calan 172,50 172,50 0 4
Per-Olof Johansson 172,50 172,50 0 3
Peter Ericson 171,25 171,25 0 2
Jonas Wendel 170,00 170,00 0 4
Stefan Lord 170,00 170,00 0 2
Jonas Nilsson 166,25 166,25 0 4
Mikael Nilsson 162,50 162,50 0 1
Patrik Emanuelsson 162,50 162,50 0 2
Lennart Johansson 162,00 162,00 0 3
Jörgen Norman 160,00 160,00 0 2
Maria Lord-Johansson 160,00 160,00 0 2
Roger Larsson 160,00 160,00 0 2
I've got a MySQL innodb table (sqlfiddle demo) with
id, name_id, name, content
And content like
1, NULL, 'Brian', 'Bridge to terabithia'
2, NULL, 'Brian', 'Pulp fiction'
3, NULL, 'Brian', 'Trainspotting'
4, NULL, 'Luke', 'Watchmen'
5, NULL, 'Luke', 'Constantine'
6, NULL, 'Tony', 'Dark knight'
7, NULL, 'Tony', 'Shutter Island'
8, NULL, 'John', 'Machinist'
9, NULL, 'John', 'Matrix'
10, NULL, 'John', 'Sin city'
11, NULL, 'John', 'Mad Max'
The id is unique to each row. But I can't get, how to set auto_increment name_id to each unique name.
Here's (sqlfiddle) what I'm trying to achieve.
1, 1, 'Brian', 'Bridge to terabithia'
2, 1, 'Brian', 'Pulp fiction'
3, 1, 'Brian', 'Trainspotting'
4, 2, 'Luke', 'Watchmen'
5, 2, 'Luke', 'Constantine'
6, 3, 'Tony', 'Dark knight'
7, 3, 'Tony', 'Shutter Island'
8, 4, 'John', 'Machinist'
9, 4, 'John', 'Matrix'
10, 4, 'John', 'Sin city'
11, 4, 'John', 'Mad Max'
Is it possible to do with MySQL only?
Thank you for reading.
The short answer is you can't do this automatically with only auto_increment, and you probably shouldn't either.
If really have reason to do this you would be much better off normalizing your database, creating a "names" table (name_id PK, name) and removing the "name" column from this table. Honestly I'm not sure what you can really gain from doing this in your example, but I imagine it might be an abstraction of the problem.