Return list of users - mysql

I have entity like this
{
id,
projectId,
name,
age,
address
}
Now I need to return list of users that are connected to projects which have 4 or more users
For example if I have in database records like this
{1, 1, "John", 21, "Address1"}
{2, 1, "Joey", 22, "Address2"}
{3, 2, "Tom", 25, "Address3"}
{4, 3, "Mike", 23, "Address4"}
{5, 1, "John", 26, "Address5"}
{6, 3, "Jane", 28, "Address6"}
{7, 2, "Joe", 23, "Address7"}
{8, 2, "Steve", 24, "Address8"}
{9, 3, "Will", 29, "Address9"}
{10, 1, "Robert", 21, "Address10"}
{11, 2, "James", 20, "Address11"}
i should get returned list
{1, 1, "John", 21, "Address1"}
{2, 1, "Joey", 22, "Address2"}
{3, 2, "Tom", 25, "Address3"}
{5, 1, "John", 26, "Address5"}
{7, 2, "Joe", 23, "Address7"}
{8, 2, "Steve", 24, "Address8"}
{10, 1, "Robert", 21, "Address10"}
{11, 2, "James", 20, "Address11"}
because project with ID 1 have 4 users and also project with ID 2 have 4 users, but project with ID 3 have 3 users and I don't need that users as result.
I'm using SpringData JPA but as I researched JPA doesn't provide option to make such filtering and as other option I found that I can write custom MySql Query but don't know how that query should look like
I've tried something with group by projectId but don't know how to count and get only groups that have 4 or more users
#Query(value = "SELECT * FROM USER u GROUP BY projectId", nativeQuery = true)
List<User> findUsers();

create table temp (
id int,
projectId int,
name varchar(20),
age int,
address varchar(20)
);
insert into temp (id,projectId,name,age,address) values (1, 1, 'John', 21, 'Address1');
insert into temp (id,projectId,name,age,address) values (2, 1, 'Joey', 22, 'Address2');
insert into temp (id,projectId,name,age,address) values (3, 2, 'Tom', 25, 'Address3');
insert into temp (id,projectId,name,age,address) values (4, 3, 'Mike', 23, 'Address4');
insert into temp (id,projectId,name,age,address) values (5, 1, 'John', 26, 'Address5');
insert into temp (id,projectId,name,age,address) values (6, 3, 'Jane', 28, 'Address6');
insert into temp (id,projectId,name,age,address) values (7, 2, 'Joe', 23, 'Address7');
insert into temp (id,projectId,name,age,address) values (8, 2, 'Steve', 24, 'Address8');
insert into temp (id,projectId,name,age,address) values (9, 3, 'Will', 29, 'Address9');
insert into temp (id,projectId,name,age,address) values (10, 1, 'Robert', 21, 'Address10');
insert into temp (id,projectId,name,age,address) values (11, 2, 'James', 20, 'Address11');
QUERY :
SELECT *
FROM temp
WHERE projectid IN (SELECT projectid
FROM temp
GROUP BY projectid
HAVING Count(*) > 3)

Related

Finding Connection Pairs

Suppose we have a table in mySQL database where fname has a connection to another fname(BB_Connection_name), we would like have a query to find the pair(s) of friends who find connection among themselves.
E.g
Sidharth and Asim both have each others BBid and BB_Connection_ID
I have looked for similar case of father, son and grandson question but in that not each father has a son and thus inner joining them makes things easier for solving. I tried using that but didn't work.
Here i need to check BB_Connection_ID for every fname(A) and then corresponding fname has A's BBid as his BB_Connection_ID or not.
The pairs which would be chosen, should be like Sidharth<->Asim
We need to find the pairs who have their connection ID to each other.
==========================================================================
Code for recreation of the table:
-----------------------------------------------------------------------------
create table world.bigbb(
BBid int not null auto_increment,
fname varchar(20) NOT NULL,
lname varchar(30),
BBdays int not null,
No_of_Nom int,
BB_rank int not null,
BB_Task varchar(10),
BB_Connection_ID int,
BB_Connection_name varchar(10),
primary key (BBid)
);
insert into world.bigbb (fname, lname, BBdays, No_of_Nom, BB_rank, BB_Task, BB_Connection_ID, BB_Connection_name)
values
('Sidharth', 'Shukla', 40, 4, 2, 'Kitchen', 11, 'Asim'),
('Arhaan', 'Khan', 7, 1, 9, 'Kitchen', 16, 'Rashmi'),
('Vikas', 'Bhau', 7, 1, 8, 'Bedroom', 11, 'Asim'),
('Khesari', 'Bihari', 7, 1, 12, 'Kitchen', 9, 'Paras'),
('Tehseem', 'Poonawala', 7, 1, 11, 'Washroom', 12, 'Khesari'),
('Shehnaaz', 'Gill', 40, 4, 4, 'Washroom', 9, 'Paras'),
('Himanshi', 'Khurana', 7, 0, 7, 'Bedroom', 8, 'Shefali'),
('Shefali', 'Zariwala', 7, 1, 10, 'Bedroom', 1, 'Sidharth'),
('Paras', 'Chabra', 40, 3, 1, 'Bathroom', 10, 'Mahira'),
('Mahira', 'Sharma', 40, 4, 5, 'Kitchen', 9, 'Paras'),
('Asim', 'Khan', 40, 3, 3, 'Bathroom', 1, 'Sidharth'),
('Arti', 'Singh', 40, 5, 6, 'Captain', 1, 'Sidharth'),
('Sidharth', 'Dey', 35, 6, 16, 'None', 14, 'Shefali'),
('Shefali', 'Bagga', 38, 5, 15, 'None', 13, 'Sidharth'),
('Abu', 'Fifi', 22, 5, 17, 'None', 11, 'Asim'),
('Rashmi', 'Desai', 38, 5, 13, 'None', 17, 'Debolina'),
('Debolina', 'Bhattacharjee', 38, 5, 14, 'None', 16, 'Rashmi');
One solution would be to self-join the table:
select
b1.fname name1,
b2.fname name2
from bigbb b1
inner join bigbb b2
on b1.BB_Connection_ID = b2.BBid
and b2.BB_Connection_ID = b1.BBid
and b1.BBid < b2.BBid
This will give you one record for each pair, with the record having the smallest BBid in the first column.
This demo on DB Fiddle with your sample data returns:
name1 | name2
:------- | :-------
Sidharth | Asim
Paras | Mahira
Sidharth | Shefali
Rashmi | Debolina

MYSQL- Substract two colums and get the result

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');

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!

trying to make a average list for a bowling club

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

Set auto_increment value to each unique group

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.