nemakuphp Posted March 10, 2011 Report Share Posted March 10, 2011 Vispārīgā tabulas struktūra ir apmēram šāda: id | name | statuss Kā veidot kveriju, lai izvadītu, pieņemsim, 10 ierakstus (labi, tas būtu viegli, LIMIT 10), bet ja ieraksti, kam statuss = 0 ir vairāk kā 10, tad izvelk tos visus, kuriem statuss ir 0? Quote Link to comment Share on other sites More sharing options...
labaiss Posted March 10, 2011 Report Share Posted March 10, 2011 SELECT * FROM tabula WHERE status=0 LIMIT 10 kaut kā tā Quote Link to comment Share on other sites More sharing options...
nemakuphp Posted March 10, 2011 Author Report Share Posted March 10, 2011 (edited) Tu tēmu izlasīji līdz galam? ja ieraksti, kam statuss = 0 ir vairāk kā 10, tad izvelk tos visus, kuriem statuss ir 0 Vispārīgā gadījumā tas būtu SELECT * FROM table ORDER BY id DESC LIMIT 10 bet nav zināms, vai ieraksti ar statuss 0 būs vairāk kā 10, jo ja ieraksti ar statuss = 0 ir mazāk kā 10, tad var izvadīt arī ierakstus, kuriem statuss = 1 Edited March 10, 2011 by nemakuphp Quote Link to comment Share on other sites More sharing options...
labaiss Posted March 10, 2011 Report Share Posted March 10, 2011 (edited) ja ieraksti ar stuss 0 ir mazāk kā 10, tad atlikušie jāizvada ar stuss 1 ? p.s. es laikam lasīt nemāku vai kaut kā tā...kaut ko es nesaprotu.. Edited March 10, 2011 by labaiss Quote Link to comment Share on other sites More sharing options...
nemakuphp Posted March 10, 2011 Author Report Share Posted March 10, 2011 Jā, pareizi saprati Quote Link to comment Share on other sites More sharing options...
wintermute Posted March 10, 2011 Report Share Posted March 10, 2011 (edited) Izklausās ka visa šitā pasākuma loģika ir stipti .. emm .. nepareiza. Liekas labakais ko tu šitādā gadījumā vari izdarīt ir uzstaisīt SQL procedūru, kas šito norealizē. Bet man tomēr interesē: " Kāpēc tev vajag šitād dīvainu rezultātu no SQL ? " Edited March 10, 2011 by wintermute Quote Link to comment Share on other sites More sharing options...
nemakuphp Posted March 10, 2011 Author Report Share Posted March 10, 2011 (edited) Un kas tur būtu tik dīvains? Izvilkt vai nu pēdējos 10 ar jebkādu statusu, vai visus ar statusu 0, ja tādi ir vairāk kā 10 EDIT: Protams, ja jums tas liekas par sarežģītu, varēšu arī iztikt, izdomāšu kaut ko citu. Edited March 10, 2011 by nemakuphp Quote Link to comment Share on other sites More sharing options...
labaiss Posted March 10, 2011 Report Share Posted March 10, 2011 varbūt tur kaut ko var sakombinēt ar OR un EXISTS Condition - bet ja godīgi - ES nezinu Quote Link to comment Share on other sites More sharing options...
m8t Posted March 10, 2011 Report Share Posted March 10, 2011 Ar mysql_num_rows() pačeko cik rindām tev statuss ir 0 un ja to skaits > 10, tad izvelc tās visas. Kas tur sarežģīts? Quote Link to comment Share on other sites More sharing options...
nemakuphp Posted March 10, 2011 Author Report Share Posted March 10, 2011 Papildus kverijs, biju domājis to visu paveikt ar vienu kveriju Quote Link to comment Share on other sites More sharing options...
Code Posted March 10, 2011 Report Share Posted March 10, 2011 Risinājums caur pakaļu. :D SELECT * FROM tabula WHERE statuss=0 and (SELECT COUNT(*) FROM tabula where statuss=0)>10 UNION ALL SELECT * FROM tabula WHERE (SELECT COUNT(*) FROM tabula where statuss=0)<=10 ORDER BY id DESC LIMIT 10 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.