![]() |
Datenbank: MySQL • Version: 5 • Zugriff über: Zeoslib
Problem mit Query
Hallo!
Ich hätte da ein Problem mit einem Query: In einem Table sind einige Felder für ein Voting. Jetzt können die Leute so oft abstimmen wie sie möchten, aber ich möchte trotzdem nur die letzte Stimme anhand der IP-Adresse zählen. (Ich weiss, dass IP-Adressen nicht unbedingt statisch sein müssen, aber das ist hier nicht das Thema ). Ohne diese IP-Beschränkung sieht mein Query wie folgt aus:
SQL-Code:
Nur wie müsste es aussehen, wenn ich immer nur die letzte gespeicherte IP-Adresse werten möchte?
SELECT voting_text, count(*) AS `ergebnis` FROM `data` group by `voting_text`
Danke für eure Hilfe. Christian. |
Re: Problem mit Query
Wo steht den die IP-Adresse?
|
Re: Problem mit Query
Ah. Wäre vielleicht eine interessante Info - Sorry :)
Ist im gleichen Table unter voting_ip drin. |
Re: Problem mit Query
Versuch es mal mit:
SQL-Code:
SELECT voting_text, count(ID) AS `ergebnis` FROM ( Select FIRST( ID)`data` from `data` group by `voting_ip`) group by `voting_text`;
|
Re: Problem mit Query
Für die "letzte" Stimme fehlt aber IMHO noch ein Zeitkriterium (DATETIME-Feld) :gruebel:
|
Re: Problem mit Query
Zitat:
Zitat:
|
Re: Problem mit Query
Schau Dir mal meinen
![]() |
Re: Problem mit Query
Zitat:
|
Re: Problem mit Query
Zitat:
|
Re: Problem mit Query
Er möchte, wenn von einer IP-Adresse mehrere Votings erfolgt sind, nur die letzte werten.
|
Re: Problem mit Query
Danke mkinzler :)
|
Re: Problem mit Query
Dann kommt beim count aber immer 1 heraus.
[edit] Oder ist die Gesamtanzahl der unterschiedlichen IPs gemeint? [/edit] |
Re: Problem mit Query
Zitat:
|
Re: Problem mit Query
Zitat:
|
Re: Problem mit Query
SQL-Code:
SELECT voting_text, count(ID) AS ergebnis FROM ( Select voting_text, MAX( ID) as ID from data group by voting_ip, voting_text) group by voting_text;
|
Re: Problem mit Query
Zitat:
ERROR 1248 (42000): Every derived table must have its own alias |
Re: Problem mit Query
Dann verpasse ihm doch einen Alias
|
Re: Problem mit Query
Und wie soll das aussehen?
Sorry, aber das ist mir echt ein wenig zu hoch... |
Re: Problem mit Query
SQL-Code:
SELECT a.voting_text, a.count(ID) AS ergebnis FROM ( Select voting_text, MAX( ID) as ID from data group by voting_ip, voting_text) a group by a.voting_text;
|
Re: Problem mit Query
Habs grad probiert. Gibt ein falsches Ergebnis aus - die doppelten IPs werden trotzdem gezählt.
|
Re: Problem mit Query
Liste der Anhänge anzeigen (Anzahl: 1)
Habe das in FB getestet da macht die Abfrage, was sie soll :gruebel:
|
Re: Problem mit Query
Sehr seltsam.
Vielleicht liegt es daran, dass MySQL nichts mit a.count(ID) anfangen kann? (ERROR 1305: FUNCTION a.count does not exist) |
Re: Problem mit Query
Sorry der Alias gehört natürlich in die Klammer
SQL-Code:
SELECT a.voting_text, count(a.ID) AS ergebnis FROM ( Select voting_text, MAX( ID) as ID from data group by voting_ip, voting_text) a group by a.voting_text;
|
Re: Problem mit Query
Also bei mir sieht es jetzt so aus:
SQL-Code:
geändert von mir wurde lediglich das
mysql> SELECT a.voting_text, count(a.ID) AS ergebnis FROM ( Select voting_text, MAX(voting_id) as ID from data group by voting_ip, voting_text) a group by a.voting_text;
SQL-Code:
weil wenn ich nur MAX(ID) hier nehme bringt er den Fehler "Unknown column 'ID' in 'field list'"
MAX(voting_id) as ID ...
Auf jeden Fall bringt "mein" SQL nicht den gewünschten Effekt. Im Übrigen möcht ich mich schon mal für den Einsatz bedanken! Find ich echt toll! Danke! |
Re: Problem mit Query
Führe mal nur die innere Abfrage aus und lass dir das Ergebnis anzeigen
|
Re: Problem mit Query
Das hier:
Code:
+-------------+------+
| voting_text | ID | +-------------+------+ | Ja | 2 | | Ja | 1 | | Nein | 3 | +-------------+------+ |
Re: Problem mit Query
Wie sieht die komplette Tabelle dazu aus?
|
Re: Problem mit Query
so:
Code:
+-----------+---------------------+-------------+-----------+
| voting_ip | voting_date | voting_text | voting_id | +-----------+---------------------+-------------+-----------+ | 10.0.60.1 | 2008-04-21 11:43:00 | Ja | 1 | | 10.0.60.2 | 2008-04-21 11:40:00 | Ja | 2 | | 10.0.60.1 | 2008-04-21 15:13:00 | Nein | 3 | +-----------+---------------------+-------------+-----------+ |
Re: Problem mit Query
Ok. Ich Gruppiere ja nach Text und ID
SQL-Code:
SELECT a.voting_text, count(a.ID) AS ergebnis FROM
(select c.voting_text, MAX(b.voting_id) as ID from data b join data c on c.id = b.ID group by voting_ip) a group by a.voting_text; |
Re: Problem mit Query
ERROR 1054 (42S22): Unknown column 'c.id' in 'on clause'
|
Re: Problem mit Query
SQL-Code:
SELECT a.voting_text, count(a.ID) AS ergebnis FROM
(select c.voting_text, MAX(b.voting_id) as ID from data b join data c on c.voting_id = b.voting_id group by b.voting_ip) a group by a.voting_text; |
Re: Problem mit Query
Jetzt funktioniert es schon "besser":
Ich bekomme jedoch für Antwort "Ja" bei ergebnis 2. Eigentlich sollte es 1 bei Ja und 1 bei Nein sein. |
Re: Problem mit Query
So sollte es funktionieren:
SQL-Code:
SELECT a.voting_text, count(a.voting_ip) AS ergebnis FROM
( select distinct( c.voting_ip), max(c.voting_text) as voting_text from data b join data c on c.voting_ip = b.voting_ip group by c.voting_ip) a group by a.voting_text; |
Re: Problem mit Query
Danke, hab es jetzt (mit Hilfe eines Freundes) so gelöst:
SQL-Code:
Danke an alle Mitdenker :)
SELECT t1.voting_text, count(*) AS `ergebnis`
FROM `data` t1 LEFT JOIN `data` t2 ON t1.voting_ip = t2.voting_ip AND t1.voting_date < t2.voting_date WHERE t2.voting_id IS NULL GROUP BY t1.voting_text; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 00:04 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz