开发者

Can I condense these queries into less

Is it possible to make 1 query and still pull the required data?

I have many queries and would like to consolidate them to make the page load faster.

Cheers

$result = mysql_query("SELECT * FROM products where added = '$date'");
$new_items = mysql_num_rows($result);
$result = mysql_query("SELECT * FROM products where isasin = 1");
$isasin = mysql_num_rows($result);
$result = mysql_query("SELECT * FROM products where isasin = 0");
$notasin = mysql_num_rows($result);
$result = mysql_query("SELECT * FROM products where EAN = ''");
$blank_ean = mysql_num_rows($result);
$result = mysql_query("SELECT * FROM products where EAN = '-'");
$ean_line = mysql_num_rows($result);
$result = mysql_query("SELECT * FROM products where EAN = 'PRERELEASE'");
$ean_prerelease = mysql_num_rows($result);
$result = mysql_query("SELECT * FROM products where format = 'delete'");
$delete = mysql_num_rows($result);
$result = mysql_query("SELECT * FROM products where format = 'VIDEO GAMES'");
$vgames = mysql_num_rows($result);
$result = mysql_query("SELECT * FROM products where format = 'CONSOLE ACCESORIES'");
$conacc = mysql_num_rows($result);
$result = mysql_query("SELECT * FROM products where format = 'Games'");
$games = mysql_num_ro开发者_开发技巧ws($result);
$result = mysql_query("SELECT * FROM products where format = 'Cool Stuff'");
$cool_stuff = mysql_num_rows($result);
$result = mysql_query("SELECT * FROM products where format = 'Unknown'");
$Unknown = mysql_num_rows($result);
$result = mysql_query("SELECT * FROM products where description = ''");
$no_desc = mysql_num_rows($result);
$result = mysql_query("SELECT * FROM products where product_name = ''");
$no_name = mysql_num_rows($result);
//game stats
$result = mysql_query("SELECT * FROM products where format = 'Xbox 360'");
$xbox360 = mysql_num_rows($result);
$result = mysql_query("SELECT * FROM products where format = 'Playstation 3'");
$ps3 = mysql_num_rows($result);
$result = mysql_query("SELECT * FROM products where format = 'Nintendo DS'");
$nds = mysql_num_rows($result);  
$result = mysql_query("SELECT * FROM products where format = 'Nintendo Wii'");
$wii = mysql_num_rows($result);
$result = mysql_query("SELECT * FROM products where format = 'PC'");
$pc = mysql_num_rows($result);
$result = mysql_query("SELECT * FROM products");
$total_games = mysql_num_rows($result);  


You only need the counts, by the looks. Also, by the looks, it's MySQL. Here is your one query:

select
(SELECT count(*) FROM products where added = '$date') as Alias01,
(SELECT count(*) FROM products where isasin = 1) as Alias02,
(SELECT count(*) FROM products where isasin = 0) as Alias03,
(SELECT count(*) FROM products where EAN = '') as Alias04,
(SELECT count(*) FROM products where EAN = '-') as Alias05,
(SELECT count(*) FROM products where EAN = 'PRERELEASE') as Alias06,
(SELECT count(*) FROM products where format = 'delete') as Alias07,
(SELECT count(*) FROM products where format = 'VIDEO GAMES') as Alias08,
(SELECT count(*) FROM products where format = 'CONSOLE ACCESORIES') as Alias09,
(SELECT count(*) FROM products where format = 'Games') as Alias10,
(SELECT count(*) FROM products where format = 'Cool Stuff') as Alias11,
(SELECT count(*) FROM products where format = 'Unknown') as Alias12,
(SELECT count(*) FROM products where description = '') as Alias13,
(SELECT count(*) FROM products where product_name = '') as Alias14,
(SELECT count(*) FROM products where format = 'Xbox 360') as Alias15,
(SELECT count(*) FROM products where format = 'Playstation 3') as Alias16,
(SELECT count(*) FROM products where format = 'Nintendo DS') as Alias17,
(SELECT count(*) FROM products where format = 'Nintendo Wii') as Alias18,
(SELECT count(*) FROM products where format = 'PC') as Alias19,
(SELECT count(*) FROM products) as Alias20

Each count is a column, you should alias them accordingly


SELECT
      ( SELECT COUNT(*) FROM products where added = '$date' 
      ) AS new_items
    , ( SELECT COUNT(*) FROM products where isasin = 1
      ) AS isasin
    , ...
      ...
    , ( SELECT COUNT(*) FROM products 
      ) AS all_products 


Ok, the last query selects all records so the previous queries are redundant. If you want to do this without selecting all records use something like:

Select * from products 
where (format='Xbox 360' OR format='PC' OR format='Playstation 3') ...

You can use OR for selecting any records matching the specified criteria1 OR criteria2. AND is used differently and means a record must match criteria1 AND criteria2 to be returned. Look up And/OR on w3schools for solid examples.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜