WebWork Magazin - Webseiten erstellen lassen, Online Medien, html

Webhoster, Webhosting Provider und Domain registrieren

Home | Registrieren | Einloggen | Suchen | Aktuelles | GSL-Webservice | Suleitec Webhosting
Reparatur-Forum | Elektro forum | Ersatzteilshop Haushalt und Elektronik


Homepage und Webhosting-Forum

Scripte und Programme für PHP, MYSQL. Diskussionen zur Programmierung im Web. Fragen zu CMS, Blogsoftware, Shops, Newsletter und vielen weiteren Scripten.


Forum » PHP & MySQL » GROUP BY Problem » Antworten
Benutzername:
Passwort: Passwort vergessen?
Inhalt der Nachricht: Fett | Kursiv | Unterstrichen | Link | Bild | Smiley | Zitat | Zentriert | Quellcode| Kleiner Text
Optionen: Emailbenachrichtigung bei Antworten
 

Die letzten 5 Postings in diesem Thema » Alle anzeigen
von smonkey
Hallo,

ich habe das gleiche Problem. Das Problem ist eben, dass ORDER BY erst nach GROUP BY ausgeführt wird.
Es gibt zwar die möglichkeit mit GROUP BY field ASC|DESC das GROUP BY-Feld zu sortieren, macht aber in meinen Augen keinen Sinn.
Hast du vielleicht inzwischen eine Lösung dafür gefunden?

Grüsse
smonkey
von Pain_
Probiers einfach mal

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: 
70: 
71: 
72: 
73: 
74: 
75: 
76: 
77: 
78: 
79: 
80: 
81: 
82: 
83: 
84: 
85: 
86: 
87: 
88: 
89: 
90: 
91: 
92: 
93: 
94: 
95: 
96: 
97: 
98: 
99: 
100: 
101: 
102: 
103: 
104: 
105: 
106: 
107: 
108: 
109: 
110: 
111: 
112: 
113: 
114: 
115: 
116: 
117: 
118: 
119: 
120: 
121: 
122: 
123: 
124: 
125: 
126: 
127: 
128: 
129: 
130: 
131: 
132: 
133: 
134: 
135: 
136: 
137: 
138: 
139: 
140: 
141: 
142: 
143: 
144: 
145: 
146: 
147: 
148: 
149: 
150: 
151: 
152: 
153: 
154: 
155: 
156: 
157: 
158: 
159: 
160: 
161: 
162: 
163: 
164: 
165: 
166: 
167: 
168: 
169: 
170: 
171: 
172: 
173: 
174: 
175: 
176: 
177: 
178: 
179: 
180: 
181: 
182: 
183: 
184: 
185: 
186: 
187: 
188: 
189: 
190: 
191: 
192: 
193: 
194: 
195: 
196: 
197: 
198: 
199: 
200: 
201: 
202: 
203: 
204: 
205: 
206: 
207: 
208: 
209: 
210:
<?
$cur_id = $_GET['id'];
$newentry = $_GET['newentry'];
$title = $_POST['title'];
$text = $_POST['text'];
?>
<?
$sqlconn = mysql_connect("localhost", "root", "");
if(!$sqlconn)die("MySQL Verbindung fehlgeschlagen!");
$suc = mysql_select_db("test", $sqlconn);
if(!$suc)die("Datenbank konnte nicht ausgewählt werden!");
$tbl_tree = "alpha_tree_demo";
?>
<?
function sublist($ploc=""){
     global $sqlconn, $tbl_tree;
     if($ploc==""){
          $ok_reg = "%";
          $not_reg = "%.%";
     }else{
          $ok_reg = "$ploc.%";
          $not_reg = "%";
          $level = substr_count($ploc,".");
          for($i=0;$i<($level+2);$i++)
               $not_reg .= ".%";
     }
     $query = "SELECT id,loc,title,text,(SUBSTRING_INDEX(loc,'.',-1)+0) AS n FROM $tbl_tree WHERE loc LIKE '$ok_reg' AND loc NOT LIKE '$not_reg' ORDER BY n";
     $result = mysql_query($query,$sqlconn);
     echo mysql_error();
     while(list($id,$loc,$title,$text,$n)=mysql_fetch_row($result)){
?>
<span class="sub">
<?
          output_line($id,$title);
          sublist($loc);
?>
</span>
<?
     }
}
function output_line($id,$title){
     global $cur_id;
     if($cur_id && $cur_id==$id){
          $is_cur = true;
     }else{
          $is_cur = false;
     }
     if($is_cur){
?>
• <font color="#FF7F00"><b><? echo $title ?></b></font>

<?
     }else{
?>
• <a href="?id=<? echo $id?>"><font color="#FF7F00"><? echo $title ?></font></a>

<?
     }
}
function randnum($length){
     $sym = "0123456789";
     $num = (rand()%(strlen($sym)-1))+1;
     $str = substr($sym,$num,1);
     for($i=1;$i<$length;$i++){
          $num = rand()%strlen($sym);
          $str .= substr($sym,$num,1);
     }
     return $str;
}
?>
<html>
<head>
<title>Hierarchische Datenbank-Tabellen: Demo</title>
<style type="text/css">
<!--
.sub{
     display:block;
     padding-left:30px;
}
h2{
     background:#FFEDB0;
     border-bottom:2px solid black;
}
-->
</style>
</head>
<body text="#000000" bgcolor="#FFFFFF" link="#FF0000" alink="#FF0000" vlink="#FF0000">
<center>
<h1>Hierarchische Datenbank-Tabellen: Demo-Script</h1>
<table width="600"><tr><td>
<?
if($newentry){
?>
<h2>Neuer Eintrag:</h2>
<?
     if($cur_id){
          $query = "SELECT loc FROM $tbl_tree WHERE id='$cur_id'";
          $result = mysql_query($query,$sqlconn);
          $data = mysql_fetch_array($result);
          $cur_loc = $data['loc'];
          $ok_reg = "$cur_loc.%";
          $not_reg = "%.%";
          $level = substr_count($ploc,".");
          for($i=0;$i<($level+2);$i++)
               $not_reg .= ".%";
     }else{
          $ok_reg = "%";
          $not_reg = "%.%";
     }
     $query = "SELECT loc,(SUBSTRING_INDEX(loc,'.',-1)+0) AS n FROM $tbl_tree WHERE loc LIKE '$ok_reg' AND loc NOT LIKE '$not_reg' ORDER BY n DESC LIMIT 0,1";
     $result = mysql_query($query,$sqlconn);
     $c = mysql_num_rows($result);
          if($c==0){
          $next_n = 1;
     }else if($c==1){
          $data = mysql_fetch_array($result);
          $last_n = $data["n"];
          $next_n = $last_n + 1;
     }
     if($cur_id==""){
          $next_loc = "$next_n";
     }else{
          $next_loc = "$cur_loc.$next_n";
     }
     do{
          $id = randnum(5);
          $query = "SELECT COUNT(*) AS c FROM $tbl_tree WHERE id='$id'";
          $result = mysql_query($query,$sqlconn);
          $data = mysql_fetch_array($result);
          $c = $data['c'];
          if($c>0){
               $idfree = false;
          }else{
               $idfree = true;
          }
     }while(!$idfree);
	  $query = "INSERT INTO $tbl_tree (id,loc,title,text) VALUES ('$id','$next_loc','$title','$text')";
     $suc = mysql_query($query,$sqlconn);
     if(!$suc){
?>
Fehler beim Eintragen!

<?
     }else{
?>
Der neue Eintrag wurde erfolgreich eingetragen.

<?
     }
}
?>
<h2>Alle Einträge:</h2>
• <a href="?">Der Baum:</a>

<?
sublist();
?>
<h2>Gewählter Eintrag:</h2>
<?
if($cur_id){
     $query = "SELECT title,text FROM $tbl_tree WHERE id='$cur_id'";
     $result = mysql_query($query,$sqlconn);
     $data = mysql_fetch_array($result);
     $title = $data["title"];
     $text = $data["text"];
?>
<b><? echo $title?></b> (ID: <? echo $cur_id ?>)



<b>Text:</b>

<? echo nl2br($text)?>
<?
}else{
?>
Es wurde kein Eintrag gewählt!

<?
}
?>
<h2>Neuen Eintrag machen:</h2>
Der Eintrag wird unter dem Gewählten erstellt.

Wenn kein Eintrag gewählt ist, wird ein Neuer auf Ebene 0 erzäugt.



<form action="?id=<? echo $cur_id ?>&newentry=1" method="POST">
Titel:

<input type="text" name="title">



Text:

<textarea name="text" rows="7" cols="30"></TEXTAREA>



<input type="submit" value="Eintragen">
</form>
<?
mysql_close($sqlconn);
?>
</td></tr></table>
</center>
</body>
</html>
von Herr_Zatacke
... Zusatz:

ich müsste wohl logischerweise zuerst sortieren und dann Gruppieren ... meint ihr subqueries machen da Sinn?
nur is mein mysql-Server ein 3.2..irgendwas - der unsterstützt glaub keine Subqueries, was bedeutet ich müsste ein SELECT INTO TABLE verwenden, oder ?

Falls jemand deine richtige Lösung für mein Problem hat - oder vllt. einen Link oder so
von Herr_Zatacke
max(Date) funktioniert nicht, weil damit nicht gewährleitet ist das auch die richtige ID zu diesem Date augegeben wird ....

Ich möchte als Ergebnis ja genau von jedem user das höchste Date und die richtige zum Date gehörende ID bekommen

Im konkreten Fall:

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
10: 
11:
+----+------+-------+
| ID | User | Date  |
+----+------+-------+
|  8 |    3 | ...22 |
|  9 |    4 | ...23 |
| 10 |    1 | ...31 |
| 11 |    5 | ...32 |
| 12 |    2 | ...44 |
+----+------+-------+


wenn ich MAX(Date) verwende , bekomme ich Ergebnisse wie z.B.

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
10: 
11:
+----+------+-------+
| ID | User | Date  |
+----+------+-------+
|  6 |    3 | ...22 |
|  9 |    4 | ...23 |
|  1 |    1 | ...31 |
| 11 |    5 | ...32 |
|  2 |    2 | ...44 |
+----+------+-------+
von HoRnominatoR
SELECT ... WHERE id = $id
SELECT MAX(date)

Nach oben