Pomoc - Szukaj - U¿ytkownicy - Kalendarz
Pe³na wersja: [Mysql] Select z uni± gdzie drugi select inny od pierszego
Forum PHP.pl > Forum > PHP
pieto
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
singles
Poczytaj na temat DISTINCT.
pieto
Cytat(singles @ 27.07.2007, 12:23:41 ) *
Poczytaj na temat DISTINCT.


Poczytaj na temat UNION
singles
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
Kicok
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.
singles
O, na to nie wpad³em. UNION ALL te¿ zadzia³a, przed chwil± sprawdza³em.
pieto
biggrin.gif Wiec i ja zwracam honor
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.