Lejnieks Posted January 3, 2010 Report Share Posted January 3, 2010 $sql = "SELECT `table1`.*, `table2`.* FROM table1, table2 WHERE ((`table1`.`rinda1` <7) AND (`table2`.`rinda2` <20))"; $result = mysql_query($sql); echo while($row = mysql_fetch_array($result)) Man parāda rezultātus tikai no vienas tabulas un daudzas reizes atkārto. Nezinu, ko darīt, jo jau kādu laiku man ir šī problēma uzkārusies. Quote Link to comment Share on other sites More sharing options...
bubu Posted January 3, 2010 Report Share Posted January 3, 2010 Tev vajag JOIN'ot tabulas, ja vienas tabulas ierakstiem atbislt tikai viens (vai daži ieraksti) no otrās tabulas. Tavā gadījumā MySQL's katru pirmās tabulas ierakstu atgriež kopā ar katru no otrās tabulas ierakstiem - tas uztaisa Dekarta reizinājumu. Lasi šeit: http://datubazes.wordpress.com/sql-pamati/ - Tabulu kombinēšana (savienojumi, join). Quote Link to comment Share on other sites More sharing options...
Lejnieks Posted January 3, 2010 Author Report Share Posted January 3, 2010 $sql = "SELECT `table1`.*, `table2`.* FROM table1 JOIN table2 ON ((`table1`.`rinda1` <7) AND (`table2`.`rinda2` <20))"; $result = mysql_query($sql); echo while($row = mysql_fetch_array($result)) Es pamēģināju šādi bet man nekas nesanāk tik un tā. Met ārā to pašu, ko pirms tam. Quote Link to comment Share on other sites More sharing options...
2easy Posted January 3, 2010 Report Share Posted January 3, 2010 mb tev vajag vnk UNION? http://www.w3schools.com/sql/sql_union.asp Quote Link to comment Share on other sites More sharing options...
Lejnieks Posted January 3, 2010 Author Report Share Posted January 3, 2010 mb tev vajag vnk UNION? http://www.w3schools.com/sql/sql_union.asp Sanāca! Quote Link to comment Share on other sites More sharing options...
2easy Posted January 3, 2010 Report Share Posted January 3, 2010 urrraaaaaaa! :D:D:D ;) Quote Link to comment Share on other sites More sharing options...
Lejnieks Posted January 4, 2010 Author Report Share Posted January 4, 2010 (edited) $result = mysql_query("SELECT * FROM `table1` WHERE `table1`.`rinda1` <7 UNION SELECT * FROM `table2` WHERE `table2`.`rinda2` <20"); echo while($row = mysql_fetch_array($result)) Kā lai tagad šitam uzliek limit?? Edited January 4, 2010 by Lejnieks Quote Link to comment Share on other sites More sharing options...
2easy Posted January 4, 2010 Report Share Posted January 4, 2010 pamēģini limit uzlikt ar LIMIT ;) vsp šis bij super jautājums :D:D:D Quote Link to comment Share on other sites More sharing options...
Gints Plivna Posted January 5, 2010 Report Share Posted January 5, 2010 Kā lai tagad šitam uzliek limit?? dažas lietas: 1) kopas operatoriem (UNION, UNION ALL, citās db būs arī citi) order by klauzas un limitus parasti liek tikai pie pēdējā SELECTa. 2) LIMIT droši vien vajadzētu kopā ar kādu ORDER BY, citādi potenciāli pastāv iespēja dabūt jebkuras n rindiņas 3) atceries, ka UNION izravē ārā dublikātus, bet UNION ALL tos atstāj Gints Plivna http://datubazes.wordpress.com/ 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.