ziedinjsh Posted November 13, 2010 Report Share Posted November 13, 2010 (edited) $result = mysql_query("SELECT * FROM report WHERE type='payment'"); $payment = mysql_num_rows($result); $result = mysql_query("SELECT * FROM report WHERE type='bug'"); $bug = mysql_num_rows($result); $result = mysql_query("SELECT * FROM report WHERE type='advice'"); $advice = mysql_num_rows($result); Edited November 13, 2010 by ziedinjsh Quote Link to comment Share on other sites More sharing options...
daGrevis Posted November 13, 2010 Report Share Posted November 13, 2010 Nu cik nu saīsināt... Bet atceries, ka ne vienmēr īsāks kods būs labāks. Lasīšanas ērtums ir noteicošais, kas "saīsinātajā" variantā ciešs. =( $payment = mysql_num_rows( mysql_query( "SELECT * FROM report WHERE type='payment'" ) ); P.S. Pietam, zvaigznīti izmantot (runāju par pieprasījumu) izmantot nav ieteicams "production" kodā. Quote Link to comment Share on other sites More sharing options...
marcis Posted November 13, 2010 Report Share Posted November 13, 2010 Ierakstu skaitīšanai ir paredzēta MySQL COUNT() funkcija. Tu šobrīd vienkārši lieki tērē resursus. SELECT COUNT(IF(`type` = 'payment', 1, NULL)) AS `payment`, COUNT(IF(`type` = 'bug', 1, NULL)) AS `bug`, COUNT(IF(`type` = 'advice', 1, NULL)) AS `advice` FROM `report` Šis strādā apmēram tik pat ātri kā skaitīt katru type atsevišķi ar UNION ALL Quote Link to comment Share on other sites More sharing options...
xPtv45z Posted November 13, 2010 Report Share Posted November 13, 2010 Šādi nebūtu ātrāk? SELECT type, COUNT(*) AS count FROM report WHERE type IN ('payment','bug','advice') GROUP BY type Quote Link to comment Share on other sites More sharing options...
Gints Plivna Posted November 13, 2010 Report Share Posted November 13, 2010 Ja vajag tikai skaitu, tad tas pirmais variants ir kaut kādas šausmas un ārprāts. Iedomājies Tev ir 3 grāmatplaukti, kurā ir akurāti saliktas grāmatiņas, kurām visām var redzēt muguriņas. Tā vietā, lai kaut vai pirkstu piedurot pie katras tās saskaitītu, grāmatas tiek ņemtas ārā no visiem 3 plauktiem un akurāti kraujot zemē saskaitītas :) Cik nu var spriest pēc sākotnējā koda fragmenta, tad xPtv45z dotais variants ir īstais. Gints Plivna http://datubazes.wordpress.com Quote Link to comment Share on other sites More sharing options...
ziedinjsh Posted November 13, 2010 Author Report Share Posted November 13, 2010 Ierakstu skaitīšanai ir paredzēta MySQL COUNT() funkcija. Tu šobrīd vienkārši lieki tērē resursus. SELECT COUNT(IF(`type` = 'payment', 1, NULL)) AS `payment`, COUNT(IF(`type` = 'bug', 1, NULL)) AS `bug`, COUNT(IF(`type` = 'advice', 1, NULL)) AS `advice` FROM `report` Šis strādā apmēram tik pat ātri kā skaitīt katru type atsevišķi ar UNION ALL un kā es varu viņu atsevišķi izvadīt? piem tagad mani ir $payment $bug un $advice Quote Link to comment Share on other sites More sharing options...
daGrevis Posted November 13, 2010 Report Share Posted November 13, 2010 Es domāt', ka kaut kā šitā... $query = mysql_query(" SELECT COUNT(IF(`type` = 'payment', 1, NULL)) AS `payment`, COUNT(IF(`type` = 'bug', 1, NULL)) AS `bug`, COUNT(IF(`type` = 'advice', 1, NULL)) AS `advice` FROM `report` ") or exit( mysql_query() ); $database = mysql_fetch_row( $query ); echo $database['payment']; 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.