mySQL Abfrage

brainsucker

Erfahrenes Mitglied
Hallo zusammen, ich habe folgendes Problem und hoffe Hilfe zu finden:

Folgende 2 Tabellen sind gegeben

Tabelle 1:

w_id(int, autoincrement,primarykey) - bezeichnung(varchar)
1 test
2 neuer test
3 noch ein test
4 und nochmal
5 letzter test


Tabelle 2:

t_id (int, autoincrement,primarykey), w_id_tab(int, foreignkey aus tabelle 1), anzahl(int)

1 2 5
2 2 3
3 2 10
4 2 2
5 4 1
................



Ich möchte mir jetzt ein SQL Statement schreiben welches mir ausgibt zu welchem w_id aus tabelle 1 der wert für die anzahl aus tabelle 2 am kleinsten ist


Ich müsste doch die tabellen joinen und mit dem min() as anzahl arbeiten oder?

Ich hätte es so versucht:

PHP:
Select w_id, bezeichnung, w_id_tab, min(anzahl) as minimum
from w_id, t_id
where tabelle1.w_id = tabelle2.w_id_tab
group by w_id_tab

leider funktioniert das nicht.

Könnt ihr mir helfen?
 
Hallo,

vielleicht so?
SQL:
select t1.w_id, t1.bezeichnung, t2.anzahl
from tabelle1 t1, tabelle2 t2
where t1.w_id = t2.w_id_tab
and t2.anzahl = (select min(anzahl) from tabelle2)

Grüße,
Matthias
 

Neue Beiträge

Zurück