Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Datenbanken (https://www.delphipraxis.net/15-datenbanken/)
-   -   MySQL: Bedingtes ORDER BY? (https://www.delphipraxis.net/75370-mysql-bedingtes-order.html)

Bernhard Geyer 18. Aug 2006 15:15

Re: MySQL: Bedingtes ORDER BY?
 
Zitat:

Zitat von Jelly
Zitat:

Zitat von Bernhard Geyer
Ja.

Nein :angel2:

Komisch :gruebel: Access verdrägt es auch anders.

Vermutlich sortiert dein Beispliel nur das letzte Select.

Jelly 18. Aug 2006 15:19

Re: MySQL: Bedingtes ORDER BY?
 
Kann sein, dass Access das anders handhabt... Aber MySQL sortiert nur die Gesamtergebnismenge... Und andere DBMS tun das auch (MSSQL z.B.), so dass ich eher davon ausgeh, dass Access ne Ausnahme bildet.

Bernhard Geyer 18. Aug 2006 16:23

Re: MySQL: Bedingtes ORDER BY?
 
Stimmt. :freak:

Ich wußte doch wieso ich Acces nur im allerletzten Notfall verwende.

omata 18. Aug 2006 16:39

Re: MySQL: Bedingtes ORDER BY?
 
Hallo DGL-luke,

klar geht das...

SQL-Code:
$sql = "SELECT t.*, u.username, u.user_id,
               u2.username as user2, u2.user_id as id2,
               p.post_username, p2.post_username AS post_username2,
               p2.post_time
   FROM " . TOPICS_TABLE . " t,
             " . USERS_TABLE . " u,
             " . POSTS_TABLE . " p,
             " . POSTS_TABLE . " p2,
             " . USERS_TABLE . " u2
   WHERE t.forum_id = $forum_id
       AND t.topic_poster = u.user_id
     AND p.post_id = t.topic_first_post_id
     AND p2.post_id = t.topic_last_post_id
     AND u2.user_id = p2.poster_id
     AND t.topic_type <> " . POST_ANNOUNCE . "
     AND t.topic_title LIKE '$start_letter%'
     $limit_topics_time
   ORDER BY CASE WHEN t.forum_id IN (1, 2)
                   THEN NULL
                   ELSE t.topic_type
                 END DESC, t.topic_title ASC
   LIMIT $start, ".$board_config['topics_per_page'];
ich würde die Abfrage allerdings mit JOINs machen...
SQL-Code:
$sql = "SELECT t.*, u.username, u.user_id,
               u2.username as user2, u2.user_id as id2,
               p.post_username, p2.post_username AS post_username2,
               p2.post_time
   FROM " . TOPICS_TABLE . " t
        INNER JOIN " . USERS_TABLE . " u
          ON t.topic_poster = u.user_id
        INNER JOIN " . POSTS_TABLE . " p
          ON t.topic_first_post_id = p.post_id
        INNER JOIN " . POSTS_TABLE . " p2
          ON t.topic_last_post_id = p2.post_id
        INNER JOIN " . USERS_TABLE . " u2
          ON p2.poster_id = u2.user_id
   WHERE t.forum_id = $forum_id
     AND t.topic_type <> " . POST_ANNOUNCE . "
     AND t.topic_title LIKE '$start_letter%'
     $limit_topics_time
   ORDER BY CASE WHEN t.forum_id IN (1, 2)
                   THEN NULL
                   ELSE t.topic_type
                 END DESC, t.topic_title ASC
   LIMIT $start, ".$board_config['topics_per_page'];
Gruss
Thorsten

DGL-luke 18. Aug 2006 21:48

Re: MySQL: Bedingtes ORDER BY?
 
:thumb:

Wusst ichs doch, dass das gehen muss. :P

Danke!

EDIT: :gruebel: ich werd morgen in die doc schauen, ich versprechs, aber was genau "verbindet" so ein Join?

faux 19. Aug 2006 18:58

Re: MySQL: Bedingtes ORDER BY?
 
Kurz gesagt, ein JOIN verbindet zwei (oder mehrere) Tabellen in diesem Fall.
Einfaches Beispiel:

SQL-Code:
SELECT topic_title, username
FROM phpbb_topics t INNER JOIN phpbb_users u
  ON t.topic_poster = u.user_id
liefert die selben Ergebnisse wie
SQL-Code:
SELECT topic_title, username
FROM phpbb_topics t, phpbb_users u
WHERE t.topic_poster = u.user_id
Man sollte aber (aus Perfomancegründen?) hier JOIN gegenüber WHERE vorziehen (sofern es das entsprechene DBMS unterstützt).

Neben INNER JOINs gibts dann noch OUTER JOINs (wie könnte es anders sein), aber das wird die Doku sicherlich besser erklären können. ;)

Grüße
Faux


Alle Zeitangaben in WEZ +1. Es ist jetzt 15:59 Uhr.
Seite 2 von 2     12   

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz