Vous pouvez utiliser la jointure gauche et compter sur les entrées existantes pour n'effectuer que l'insertion d'entrées lorsque 3 enregistrements ou plus sont déjà stockés ou que l'insertion a au moins trois enregistrements à insérer pour un ID utilisateur.
CREATE TABLE if not exists `product_46353892` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
/*
truncate table to allow rerun of example
*/
truncate product_46353892 ;
/*
example showing no inserts since there are only two records being inserted
and no previous records for this user_id
*/
insert into product_46353892 (id, user_id)
select `new`.*
-- , new_count.count new_count
-- , old.count old_count
from (
select 1 id, 1 user_id
union all
select 2 id, 1 user_id
) `new`
left join (
select user_id, count(*) count from (
select 1 id, 1 user_id
union all
select 2 id, 1 user_id
) new_group
group by user_id
) new_count
on `new`.user_id = new_count.user_id
left join (
select user_id, count(*) count from product_46353892 product
group by user_id
) old
on new.user_id = old.user_id
where (new_count.count >= 3 or old.count >= 3 or (new_count.count + old.count) >= 3) ;
/*
this one works because there are three or more records in the insert
*/
insert into product_46353892 (id, user_id)
select `new`.*
from (
select 3 id, 1 user_id
union all
select 4 id, 1 user_id
union all
select 5 id, 1 user_id
union all
select 6 id, 1 user_id
) `new`
left join (
select user_id, count(*) count from (
select 3 id, 1 user_id
union all
select 4 id, 1 user_id
union all
select 5 id, 1 user_id
union all
select 6 id, 1 user_id
) new_group
group by user_id
) new_count
on `new`.user_id = new_count.user_id
left join (
select user_id, count(*) count from product_46353892 product
group by user_id
) old
on new.user_id = old.user_id
where (new_count.count >= 3 or old.count >= 3 or (new_count.count + old.count) >= 3) ;
/*
this now works since there are already the required number of initial records stored
for this user_id
*/
insert into product_46353892 (id, user_id)
select `new`.*
-- , new_count.count new_count
-- , old.count old_count
from (
select 1 id, 1 user_id
union all
select 2 id, 1 user_id
) `new`
left join (
select user_id, count(*) count from (
select 1 id, 1 user_id
union all
select 2 id, 1 user_id
) new_group
group by user_id
) new_count
on `new`.user_id = new_count.user_id
left join (
select user_id, count(*) count from product_46353892 product
group by user_id
) old
on new.user_id = old.user_id
where (new_count.count >= 3 or old.count >= 3 or (new_count.count + old.count) >= 3) ;
select * from product_46353892 ;
Exemples de résultats. Notez l'ordre des ID, 1 et 2 a été inséré sur la deuxième insertion après qu'il y avait déjà trois enregistrements ou plus pour l'ID d'utilisateur dans la table.
id user_id
3 1
4 1
5 1
6 1
1 1
2 1
Il semble que la seule chose à vouloir arrêter est quand il y a seulement 1 ou 2 produits connexes existants. Tout ce que vous autorisez à travers. Est-ce exact? –
oui. @ KeithJohnHutchison..please pouvez-vous m'aider avec cela. –
Veuillez ajouter des commandes de schéma et remplir ce schéma avec des exemples de données à votre question. –