reiniger Posted May 28, 2009 Report Share Posted May 28, 2009 maja ID= 1 un ID_P = 0; riga ID = 113 un ID_P = 1; liela ID = 115 un ID_P 113; raksta ID_P = 115 $m = 1; SELECT post.*,category.* FROM post JOIN category ON (post.id_p=category.id AND category.id_p=$m) OR (post.id_p=category.id AND category.id=$m) WHERE NOW() >= start_date AND NOW() <= end_date Ja padod 113 vai 115 rezultāts rokā. Kā lai atrod visus rakstus ja padota vērtība 1 mēģināju šādi bet nestrādā: SELECT post.*,category.* FROM post JOIN category ON (post.id_p=category.id AND category.id_p=$m) OR (post.id_p=category.id AND category.id=$m) INNER JOIN category AS c1(post.id_p=category.id AND category.id_p=c1.id AND c1.id_p = $m) WHERE NOW() >= start_date AND NOW() <= end_date Kur esmu nošāvis nepareizi??? Quote Link to comment Share on other sites More sharing options...
Gints Plivna Posted May 29, 2009 Report Share Posted May 29, 2009 Tabulu struktūra (vēlams create table skripti)? Piemēra dati (vēlams insert into values)? Nepieciešamais rezultāts (vēlams piemērs un saprātīgs paskaidrojums)? Pašlaik vismaz man no augšminētā praktiski nekas nav skaidrs. Gints Plivna http://datubazes.wordpress.com Quote Link to comment Share on other sites More sharing options...
reiniger Posted May 29, 2009 Author Report Share Posted May 29, 2009 (edited) brīvā veidā uzrakstīju prasīto create category id, id_p, title create post id, id_p, name insert into category (id , id_p , title) value (1 , 0 , maja); insert into category (id , id_p , title) value (113 , 1 , riga); insert into category (id , id_p , title) value (115 , 113 , liela); insert into post (id , id_p , name) value ( 1, 115 , kruta maja); funtion count($m){ $result = mysql_query("SELECT post.*,category.* FROM post JOIN category ON (post.id_p=category.id AND category.id_p=$m) OR (post.id_p=category.id AND category.id=$m) WHERE NOW() >= start_date AND NOW() <= end_date"); $all_c = mysql_num_rows($result); print_r($all_c); } ... count(115) rezultats ir 1 ... count(113) rezultats ir 1 ... count(1) ??? ... ka lai izveidoju select, lai nododot uz funkciju count vērtību 1 man rezultātā skaitītu ierakstus? Ja vēl kas neskaidrs, tad jautājiet. Iespējams ka vajag vēlvienu JOIN, bet nevaru saprast ka to vajadzētu norealizēt. Edited May 29, 2009 by reiniger Quote Link to comment Share on other sites More sharing options...
xPtv45z Posted May 29, 2009 Report Share Posted May 29, 2009 Es rakstītu kaut kā tā. "SELECT post.*,category.* FROM post JOIN category ON (post.id_p=category.id) WHERE NOW() >= start_date AND NOW() <= end_date AND (category.id={$m} OR category.id_p={$m})" Quote Link to comment Share on other sites More sharing options...
reiniger Posted May 29, 2009 Author Report Share Posted May 29, 2009 (edited) Es rakstītu kaut kā tā. "SELECT post.*,category.* FROM post JOIN category ON (post.id_p=category.id) WHERE NOW() >= start_date AND NOW() <= end_date AND (category.id={$m} OR category.id_p={$m})" Šis strādā tā pat kā mans nedaudz sarežģītākais select. Paldies par opitmizētāku kodu, bet manu galveno problēmu tas neatrisināja Izpildot : ... count(115) rezultats ir 1 ... count(113) rezultats ir 1 ... bet count(1) rezultāts man parādās 0 Principā vajag ta, lai category.id = category.id_p = category.id = category.id_p = $m ! Kaut kā šādi, bet šis pieraksts nava pareizs! Edited May 29, 2009 by reiniger Quote Link to comment Share on other sites More sharing options...
xPtv45z Posted May 29, 2009 Report Share Posted May 29, 2009 (edited) Bet pareizi jau arī atgriež. :) Šis insert into post (id , id_p , name) value ( 1, 115 , kruta maja); ir piesaistīts tikai šim insert into category (id , id_p , title) value (115 , 113 , liela); Līdz ar ko, gan 115, 113 atgriezīs 1 ierakstu. t.i. šos divus nav kam piejoinot. insert into category (id , id_p , title) value (1 , 0 , maja); insert into category (id , id_p , title) value (113 , 1 , riga); Edited May 29, 2009 by xPtv45z Quote Link to comment Share on other sites More sharing options...
reiniger Posted May 29, 2009 Author Report Share Posted May 29, 2009 Mans mēģinājums: SELECT * FROM post JOIN category ON (post.id_p=category.id) RIGHT JOIN category AS cc ON (category.id=category.id) WHERE NOW() >= start_date AND NOW() <= end_date AND (category.id=1 OR category.id_p=1 OR (category.id_p = cc.id AND cc.id_p =1)) Šāds select ar funkciju count(1) man atgriež pareizu ierakstu skaitu! BET tagad izpildot count(113) vai count(115) man tiek izdrukādts visu categoriju skaits nevis tikai manu vienu ierakstu! Tad sanāk ka nava iespējams izveidot tādu select lai atlasītu pareizi ar count(1) ! Quote Link to comment Share on other sites More sharing options...
reiniger Posted May 29, 2009 Author Report Share Posted May 29, 2009 (edited) Atrisināju: funtion count($m){ $result = mysql_query("SELECT * FROM post JOIN category ON (post.id_p=category.id) JOIN category AS cc ON (category.id_p=cc.id) WHERE NOW() >= start_date AND NOW() <= end_date AND (category.id=$m OR category.id_p=$m OR cc.id_p =$m)"); $all_c = mysql_num_rows($result); print_r($all_c); } Paldies par palīdzību! Edited May 29, 2009 by reiniger Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.