Могу ли я сжать эти запросы в меньше - PullRequest
0 голосов
/ 30 августа 2011

Можно ли сделать 1 запрос и по-прежнему извлекать необходимые данные?

У меня много запросов, и я хотел бы объединить их, чтобы ускорить загрузку страницы.

$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_rows($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);  

Ответы [ 3 ]

4 голосов
/ 30 августа 2011

Тебе нужны только цифры. Также, судя по всему, это MySQL. Вот ваш один запрос:

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

Каждый счетчик представляет собой столбец, вы должны использовать его псевдоним соответственно

1 голос
/ 30 августа 2011
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 
0 голосов
/ 30 августа 2011

Хорошо, последний запрос выбирает все записи, поэтому предыдущие запросы являются избыточными.Если вы хотите сделать это без выбора всех записей, используйте что-то вроде:

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

. Вы можете использовать ИЛИ для выбора любых записей, соответствующих заданным критериям1 ИЛИ критериев2.И используется по-разному и означает, что запись должна соответствовать критериям1 И критериям2 для возврата.Посмотрите И / ИЛИ на w3schools для убедительных примеров.

...