Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP][MySQL] Pobieranie danych z 2 tabel w jednym zapytaniu
bahh
post 3.06.2014, 07:40:18
Post #1





Grupa: Zarejestrowani
Postów: 261
Pomógł: 2
Dołączył: 13.05.2012

Ostrzeżenie: (0%)
-----


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ć?
Go to the top of the page
+Quote Post
Turson
post 3.06.2014, 07:45:49
Post #2





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

Ostrzeżenie: (0%)
-----


  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


Ten post edytował Turson 3.06.2014, 07:46:11
Go to the top of the page
+Quote Post
mmmmmmm
post 3.06.2014, 07:52:16
Post #3





Grupa: Zarejestrowani
Postów: 1 421
Pomógł: 310
Dołączył: 18.04.2012

Ostrzeżenie: (0%)
-----


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
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 29.06.2025 - 20:04