Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Mysql] Select z unią gdzie drugi select inny od pierszego, nie takie oczywiste ;)
pieto
post 27.07.2007, 11:17:47
Post #1





Grupa: Zarejestrowani
Postów: 227
Pomógł: 1
Dołączył: 12.05.2002
Skąd: gdańsk

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


Witam

  1. <?php
  2. $featured_query = tep_db_query("(select c.categories_id ,c.categories_image, cd.categories_name,  cd.categories_htc_desc_tag from " . TABLE_CATEGORIES . " c, categories_description cd where c.art ='0' and c.categories_id ='" . (int)$parent_category_id . "' and cd.language_id='" . (int)$languages_id . "' and  c.categories_id = cd.categories_id LIMIT 1 )
  3.  
  4. UNION (select c.categories_id,c.categories_image, cd.categories_name,  cd.categories_htc_desc_tag from " . TABLE_CATEGORIES . " c, categories_description cd, featured_categories fc where c.art ='0'  ".$categories_featured_sql ." ".$manufacturers_featured_sql." and cd.language_id='" . (int)$languages_id . "' and  fc.categories_id = c.categories_id and c.categories_id = cd.categories_id ORDER BY RAND() LIMIT 1) ");
  5. ?>


Powyzsze zapytanie wybiera dwa rekordy, dwie rozne zasady z tej samej tablicy
Czasem zdazy sie ze druga czresc zapytania wylosuje ten sam rekord co pierwsza. (wtedy go nie pokazuje)

i tu pytanie jak jak tego uniknąć ?

próbowałem tak

  1. <?php
  2. $featured_query = tep_db_query("(select c.categories_id as first_cid,c.categories_image, cd.categories_name,  cd.categories_htc_desc_tag from " . TABLE_CATEGORIES . " c, categories_description cd where c.art ='0' and c.categories_id ='" . (int)$parent_category_id . "' and cd.language_id='" . (int)$languages_id . "' and  c.categories_id = cd.categories_id LIMIT 1 )
  3.  
  4. UNION (select c.categories_id,c.categories_image, cd.categories_name,  cd.categories_htc_desc_tag from " . TABLE_CATEGORIES . " c, categories_description cd, featured_categories fc where c.art ='0' and c.categories_id <> first_cid ".$categories_featured_sql ." ".$manufacturers_featured_sql." and cd.language_id='" . (int)$languages_id . "' and  fc.categories_id = c.categories_id and c.categories_id = cd.categories_id ORDER BY RAND() LIMIT 1) ");
  5. ?>


Co skutkuje

  1. <?php
  2. 1054 - Unknown column 'first_cid' in 'where clause'
  3. ?>


Jakies propozycje ?
z góry thx


--------------------
Materiały i Wypracowania znajdziesz na zgapa.pl i
Encyklopedii Zgapedia
Jedyny przedawkowany portal motoryzacyjny to autoholik.pl
Go to the top of the page
+Quote Post
singles
post 27.07.2007, 11:23:41
Post #2





Grupa: Zarejestrowani
Postów: 121
Pomógł: 26
Dołączył: 2.07.2007

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


Poczytaj na temat DISTINCT.


--------------------
Works for me => u mnie działa - blog o (o)programowaniu i nie tylko
meet.php - darmowe meetupy związane z PHP
Go to the top of the page
+Quote Post
pieto
post 27.07.2007, 13:57:54
Post #3





Grupa: Zarejestrowani
Postów: 227
Pomógł: 1
Dołączył: 12.05.2002
Skąd: gdańsk

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


Cytat(singles @ 27.07.2007, 12:23:41 ) *
Poczytaj na temat DISTINCT.


Poczytaj na temat UNION


--------------------
Materiały i Wypracowania znajdziesz na zgapa.pl i
Encyklopedii Zgapedia
Jedyny przedawkowany portal motoryzacyjny to autoholik.pl
Go to the top of the page
+Quote Post
singles
post 27.07.2007, 15:44:56
Post #4





Grupa: Zarejestrowani
Postów: 121
Pomógł: 26
Dołączył: 2.07.2007

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


Mój bład, nie doczytałem do końca - przepraszam i zwracam honor smile.gif

W ramach zadośćuczynienia wymyśliłem coś takiego:
  1. SELECT *, RAND() AS CHECK FROM usr WHERE usr_id = 308 UNION SELECT *,RAND() AS CHECK FROM usr WHERE usr_id = 308


Mała szansa, ze wylosujesz 2 razy te samą liczbę. Nada się ? smile.gif

Ten post edytował singles 27.07.2007, 15:45:35


--------------------
Works for me => u mnie działa - blog o (o)programowaniu i nie tylko
meet.php - darmowe meetupy związane z PHP
Go to the top of the page
+Quote Post
Kicok
post 28.07.2007, 08:13:42
Post #5





Grupa: Zarejestrowani
Postów: 1 033
Pomógł: 125
Dołączył: 17.09.2005
Skąd: Żywiec

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


Cytat("pieto")
Poczytaj na temat UNION


No właśnie, poczytaj na temat UNION smile.gif
A dokładnie to:
Cytat("Manual")
With the optional ALL keyword, duplicate-row removal does not occur and the result includes all matching rows from all the SELECT statements.


Ten post edytował Kicok 28.07.2007, 08:14:15


--------------------
"Sumienie mam czyste, bo nieużywane."
Go to the top of the page
+Quote Post
singles
post 28.07.2007, 12:00:10
Post #6





Grupa: Zarejestrowani
Postów: 121
Pomógł: 26
Dołączył: 2.07.2007

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


O, na to nie wpadłem. UNION ALL też zadziała, przed chwilą sprawdzałem.


--------------------
Works for me => u mnie działa - blog o (o)programowaniu i nie tylko
meet.php - darmowe meetupy związane z PHP
Go to the top of the page
+Quote Post
pieto
post 2.08.2007, 16:33:10
Post #7





Grupa: Zarejestrowani
Postów: 227
Pomógł: 1
Dołączył: 12.05.2002
Skąd: gdańsk

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


biggrin.gif Wiec i ja zwracam honor


--------------------
Materiały i Wypracowania znajdziesz na zgapa.pl i
Encyklopedii Zgapedia
Jedyny przedawkowany portal motoryzacyjny to autoholik.pl
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: 13.07.2025 - 01:38