我可以将这些查询压缩为更少的内容吗

发布于 2024-12-01 21:30:26 字数 2270 浏览 2 评论 0原文

是否可以进行 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);  

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_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);  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岛歌少女 2024-12-08 21:30:26

你只需要看数量、看外观。而且,从外观上看,它是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

每个计数都是一列,您应该相应地为它们别名

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

冷血 2024-12-08 21:30:26
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 
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 
趁微风不噪 2024-12-08 21:30:26

好的,最后一个查询选择了所有记录,因此之前的查询是多余的。如果您想在不选择所有记录的情况下执行此操作,请使用以下内容:

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

您可以使用 OR 来选择与指定条件 1 或条件 2 匹配的任何记录。 AND 的用法不同,意味着记录必须匹配 criteria1 AND criteria2 才能返回。在 w3schools 上查找 And/OR 以获取可靠的示例。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文