[MySQL] SELECT met unieke combinatie van twee velden

Pagina: 1
Acties:

  • Jurgle
  • Registratie: Februari 2003
  • Laatst online: 25-03 00:07

Jurgle

100% Compatible

Topicstarter
Beste devvers,

Ik loop vast op een SELECT query en mijn laatste hoop op een oplossing is dit forum.

Ik wil uit onderstaande tabelomschrijving een resultaat hebben met name, id (id is een foreign key, die vaker voor kan komen).

SELECT name, COUNT(id) AS aantal FROM tabel WHERE [iets] GROUP BY name HAVING...

en nu komt het. Ik heb heel sterk het idee dat ik iets met de HAVING clause moet doen. Ik wil dat als de combinatie (project_id, view_id) niet uniek is, elke combinatie maar 1 keer telt in de count.

in pseudo code dus:
SELECT name, COUNT(id) AS aantal FROM tabel WHERE [iets] GROUP BY name HAVING UNIQUE (project_id, view_id)

De tabel ziet er alsvolgt uit:
id (int),
name (varchar),
project_id (int),
view_id (int)

My opinions may have changed but not the fact that I am right ― Ashleigh Brilliant


  • rrrandy
  • Registratie: Juli 2005
  • Laatst online: 25-01 15:24
Mysql zal het volgende wel slikken gok ik:

code:
1
2
3
select name, count(id)
from (select name, id from tabel group by project_id, view_id)
group by name


Let wel, dit is GEEN correcte sql, die group by in de subquery hoort helemaal niet te werken, maar mysql is daar niet zo kritisch om...

  • Jurgle
  • Registratie: Februari 2003
  • Laatst online: 25-03 00:07

Jurgle

100% Compatible

Topicstarter
Hmmm... het probleem is dat subqueries niet werken op de MySQL server waar ik mn queries op afvuur.

My opinions may have changed but not the fact that I am right ― Ashleigh Brilliant


  • Jurgle
  • Registratie: Februari 2003
  • Laatst online: 25-03 00:07

Jurgle

100% Compatible

Topicstarter
Schop!

My opinions may have changed but not the fact that I am right ― Ashleigh Brilliant


  • GX
  • Registratie: Augustus 2000
  • Laatst online: 14-05-2025

GX

Nee.

Urhgl, lastig

SQL:
1
2
3
4
5
6
7
8
9
10
SELECT a.name,
       count(1) AS aantal,
       b.project_id
       b.view_id
FROM   tabel1 a
INNER JOIN tabel2 b
  ON ([linkgeval])
GROUP BY project_id,
         view_id,
         `name`


Zoiets?