La soluzione da adottare è quella che ti ha suggerito optime.
codice:
mysql> create table serie (id int) engine = myisam;
Query OK, 0 rows affected (0.01 sec)
mysql> insert into serie (id) values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10);
Query OK, 10 rows affected (0.00 sec)
Records: 10 Duplicates: 0 Warnings: 0
mysql> create table oggetti (
-> id int unsigned not null auto_increment primary key,
-> nr int,
-> oggetto varchar(50)
-> ) engine = myisam;
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql> insert into oggetti (nr,oggetto)
-> select min(s.id) ,'scarpa'
-> from serie as s
-> left join oggetti as o
-> on s.id = o.nr and o.oggetto = 'scarpa'
-> where o.nr is null;
Query OK, 1 row affected (0.02 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into oggetti (nr,oggetto)
-> select min(s.id) ,'maglione'
-> from serie as s
-> left join oggetti as o
-> on s.id = o.nr and o.oggetto = 'maglione'
-> where o.nr is null;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into oggetti (nr,oggetto)
-> select min(s.id) ,'maglione'
-> from serie as s
-> left join oggetti as o
-> on s.id = o.nr and o.oggetto = 'maglione'
-> where o.nr is null;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from oggetti;
+----+------+----------+
| id | nr | oggetto |
+----+------+----------+
| 1 | 1 | scarpa |
| 2 | 1 | maglione |
| 3 | 2 | maglione |
+----+------+----------+
3 rows in set (0.00 sec)
mysql> insert into oggetti (nr,oggetto)
-> select min(s.id) ,'scarpa'
-> from serie as s
-> left join oggetti as o
-> on s.id = o.nr and o.oggetto = 'scarpa'
-> where o.nr is null;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from oggetti;
+----+------+----------+
| id | nr | oggetto |
+----+------+----------+
| 1 | 1 | scarpa |
| 2 | 1 | maglione |
| 3 | 2 | maglione |
| 4 | 2 | scarpa |
+----+------+----------+
4 rows in set (0.00 sec)
mysql> delete from oggetti where id < 4;
Query OK, 3 rows affected (0.04 sec)
mysql> select * from oggetti;
+----+------+---------+
| id | nr | oggetto |
+----+------+---------+
| 4 | 2 | scarpa |
+----+------+---------+
1 row in set (0.00 sec)
mysql> insert into oggetti (nr,oggetto)
-> select min(s.id) ,'scarpa'
-> from serie as s
-> left join oggetti as o
-> on s.id = o.nr and o.oggetto = 'scarpa'
-> where o.nr is null;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into oggetti (nr,oggetto)
-> select min(s.id) ,'maglione'
-> from serie as s
-> left join oggetti as o
-> on s.id = o.nr and o.oggetto = 'maglione'
-> where o.nr is null;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql>
mysql> select * from oggetti;
+----+------+----------+
| id | nr | oggetto |
+----+------+----------+
| 6 | 1 | maglione |
| 5 | 1 | scarpa |
| 4 | 2 | scarpa |
+----+------+----------+
3 rows in set (0.00 sec)