Maison > Questions et réponses > le corps du texte
P粉6595182942023-09-06 00:57:21
Ceci n'est pas un tableau clé-valeur. Souvent appelé table/relation/schéma entité-attribut-valeur.
Regardez la question, si les tableaux étaient disposés sous la première et la deuxième forme normale, cela serait trivial - il suffit de concaténer les valeurs, de regrouper par ces valeurs et de compter....
SELECT manufacturer, model, firstname, COUNT(DISTINCT submission_id) FROM atable GROUP BY manufacturer, model, firstname HAVING COUNT(DISTINCT submission_id)>1;
ou utilisez Connect....
SELECT a.manufacturer, a.model, a.firstname , a.submission_id, b.submission_id FROM atable a JOIN atable b ON a.manufacturer=b.manufacturer AND a.model=b.model AND a.firstname=b.firstname WHERE a.submission_id<b.submission_id ;
Ou utilisez le tri et comparez les lignes adjacentes....
SELECT * FROM ( SELECT @prev.submission_id AS prev_submission_id , @prev.manufacturer AS prev_manufacturer , @prev.model AS prev_model , @prev.firstname AS pref_firstname , a.submission_id , a.manufacturer , a.model , set @prev.submission_id:=a.submission_id as currsid , set @prev.manufacturer:=a.manufacturer as currman , set @prev.model:=a.model as currmodel , set @prev.firstname=a.forstname as currname FROM atable ORDER BY manufacturer, model, firstname, submission_id ) WHERE prev_manufacturer=manufacturer AND prev_model=model AND prev_firstname=firstname AND prev_submission_id<>submission_id;
La solution est donc simplement de faire en sorte que vos données ressemblent à une relation normale....
SELECT ilv.values , COUNT(ilv.submission_id) , GROUP_CONCAT(ilv.submission_id) FROM (SELECT a.submission_id , GROUP_CONCAT(CONCAT(a.key, '=',a.value)) AS values FROM atable a GROUP BY a.submission_id ) ilv GROUP BY ilv.values HAVING COUNT(ilv.submission_id)>1;
J'espère que les solutions basées sur la concaténation et la séquence devraient être évidentes maintenant.