Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP][MySQL] Pobieranie danych z 2 tabel w jednym zapytaniu
Forum PHP.pl > Forum > Przedszkole
bahh
Witam,

ostatnio się uczyłem nieco o Oracle, i chciałem sprawdzić, czy są tutaj podobne możliwości.

mam tabelę articles_categories składającą się z pól id, article_id i category_id oraz tabele articles_translations zawierającą pola article_id i added (to pole datetime), chcę pobrać wszystkie article_id gdzie pole added jest mniejsza od aktualnej daty i category_id w tabeli articles_categories jest równa np. 921

wstępnie wygląda to mniej więcej tak:

  1. $pr = mysql_query('SELECT * FROM `articles_categories` WHERE category_id = 2613 ORDER by `article_id` DESC LIMIT 3') or die ( mysql_error() );
  2. while ($row = mysql_fetch_object($pr)) {
  3. $pr1 = mysql_fetch_object(mysql_query('SELECT * FROM `articles_translations` WHERE `article_id`='.$row -> article_id.' LIMIT 1'));
  4. echo '<tr><td><a href="http://www.firmymiesne.pl/artykul/'.$row->article_id.','.$row->article_id.'" title="'.$pr1->title.'">'.$pr1->title.'</a></td></tr>';
  5. }


a po zmianie o której powyżej mówiłem w $pr dałem takie zapytanie:

  1. $pr = mysql_query('SELECT * FROM `articles_categories`, `articles_translations` WHERE articles_categories.category_id = 2613 AND articles_translations.article_id=articles_categories.article_id AND articles_translations.added<NOW() ORDER by `articles_translations.added` DESC LIMIT 3') OR die ( mysql_error() );


lecz niestety nie działa, pisze

Unknown column 'articles_translations.added' in 'order clause'


Jak ten problem rozwiązać?
Turson
  1. SELECT * FROM `articles_categories`
  2. LEFT JOIN `articles_translations` ON articles_translations.article_id=articles_categories.article_id
  3. WHERE articles_categories.category_id = 2613 AND articles_translations.added<NOW()
  4. ORDER BY `articles_translations.added` DESC LIMIT 3
mmmmmmm
Prawie jak @Turson
  1. SELECT * FROM `articles_categories`
  2. LEFT JOIN `articles_translations` ON articles_translations.article_id=articles_categories.article_id
  3. WHERE articles_categories.category_id = 2613 AND articles_translations.added<NOW()
  4. ORDER BY `articles_translations`.`added` DESC LIMIT 3
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.