Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Jak posortowac tabele wg wynikow z innej tabeli
netvalue
post 26.06.2018, 09:35:10
Post #1





Grupa: Zarejestrowani
Postów: 199
Pomógł: 2
Dołączył: 9.06.2008

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


mam dwie tabele

media_category:

id | title_pl
------------------
5 | abc
7 | bca
9 | ads
10 | xxx

oraz

offer_media
id | media_id | offer_id
-------------------------
1 | 7 | 31
2 | 5 | 31

potrzebuje wyciagnac wszystkie rekordy z tabeli media_category ale posrtowane wg kolejnosci id z tabeli offer_media dla offer_id = 31

wynik powinien byc taki

7 | bca
5 | abc
9 | ads
10 | xxx


robie tak ale cos mi nie wychodzi:
  1. SELECT id,title_pl, id2
  2. FROM (
  3. SELECT o.media_id AS id, m.title_pl, o.offer_id AS id2 FROM offer_media o JOIN media_category m ON m.id=o.offer_id WHERE o.offer_id=31
  4. UNION
  5. SELECT id AS id ,title_pl, NULL AS id2 FROM media_category
  6. ) t
  7. GROUP BY title_pl ORDER BY id2 DESC


Ten post edytował netvalue 26.06.2018, 09:36:58
Go to the top of the page
+Quote Post
nospor
post 26.06.2018, 09:37:37
Post #2





Grupa: Moderatorzy
Postów: 36 440
Pomógł: 6290
Dołączył: 27.12.2004




czemu nie zrobisz zwyklego LEFT JOIN na offer_media tylko kombinujesz z podzapytaniami i UNION?


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
netvalue
post 26.06.2018, 10:03:37
Post #3





Grupa: Zarejestrowani
Postów: 199
Pomógł: 2
Dołączył: 9.06.2008

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


Ponieważ jak dam
  1. SELECT m.title_pl , m.id FROM media_category m LEFT JOIN offer_media o ON o.media_id=m.id WHERE o.offer_id=31


wyświetli mi tylko dwa wiersze które są w offer_media
Go to the top of the page
+Quote Post
nospor
post 26.06.2018, 10:04:43
Post #4





Grupa: Moderatorzy
Postów: 36 440
Pomógł: 6290
Dołączył: 27.12.2004




no ale o.offer_id=31 masz robic w sekcji JOIN po ON a nie w where....


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
netvalue
post 26.06.2018, 10:14:28
Post #5





Grupa: Zarejestrowani
Postów: 199
Pomógł: 2
Dołączył: 9.06.2008

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


OMG. ale fail robie smile.gif dzieki Nospor . Starosc nie radosc smile.gif
Go to the top of the page
+Quote Post
nospor
post 26.06.2018, 10:24:16
Post #6





Grupa: Moderatorzy
Postów: 36 440
Pomógł: 6290
Dołączył: 27.12.2004




Cytat(netvalue @ 26.06.2018, 10:14:28 ) *
Starosc nie radosc smile.gif
Znam to z pierwszej reki wink.gif Zdarza sie kazdemu


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
Pyton_000
post 26.06.2018, 10:29:14
Post #7





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


@netvalue a ile ty masz lat że tak twierdzisz wink.gif
Go to the top of the page
+Quote Post
nospor
post 26.06.2018, 10:31:55
Post #8





Grupa: Moderatorzy
Postów: 36 440
Pomógł: 6290
Dołączył: 27.12.2004




@Pyton sie nie boj, ciebie staruszku i tak nikt nie przebije wink.gif


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

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: 28.03.2024 - 09:38