有人可以告诉我如何更有效地编写以下SELECT语句集合:
$sql="SELECT * FROM brand WHERE
name NOT LIKE '' OR
name = 'Hot Brands'
ORDER BY name ";
$result=mysql_query($sql);
while($brand=mysql_fetch_array($result)){
?>
<?
$bid = $brand['brand_id'];
$sql2="SELECT * FROM shop_orders WHERE brand = '".$bid."' AND shop_order_month = '".$m."' AND shop_order_year = '".$y."' AND shop_order_action = 'Sale' ";
$result2=mysql_query($sql2);
$total = mysql_num_rows($result2);
$bawtrysql="SELECT * FROM shop_orders WHERE brand = '".$bid."' AND shop_order_month = '".$m."' AND shop_order_year = '".$y."' AND shop_order_location = 'Bawtry' AND shop_order_year = '".$y."' AND shop_order_action = 'Sale' ";
$bawtryresult=mysql_query($bawtrysql);
$bawtrytotal = mysql_num_rows($bawtryresult);
$chainsql="SELECT * FROM shop_orders WHERE brand = '".$bid."' AND shop_order_month = '".$m."' AND shop_order_year = '".$y."' AND shop_order_location = 'Chain' AND shop_order_year = '".$y."' AND shop_order_action = 'Sale' ";
$chainresult=mysql_query($chainsql);
$chaintotal = mysql_num_rows($chainresult);
$granthamsql="SELECT * FROM shop_orders WHERE brand = '".$bid."' AND shop_order_month = '".$m."' AND shop_order_year = '".$y."' AND shop_order_location = 'Grantham' AND shop_order_year = '".$y."' AND shop_order_action = 'Sale' ";
$granthamresult=mysql_query($granthamsql);
$granthamtotal = mysql_num_rows($granthamresult);
$internetsql="SELECT * FROM shop_orders WHERE brand = '".$bid."' AND shop_order_month = '".$m."' AND shop_order_year = '".$y."' AND shop_order_location = 'Internet' AND shop_order_year = '".$y."' AND shop_order_action = 'Sale' ";
$internetresult=mysql_query($internetsql);
$internettotal = mysql_num_rows($internetresult);
$bmsql="SELECT * FROM shop_orders WHERE brand = '".$bid."' AND title = 'Mens' AND shop_order_month = '".$m."' AND shop_order_year = '".$y."' AND shop_order_location = 'Bawtry' AND shop_order_year = '".$y."' AND shop_order_action = 'Sale' ";
$bmresult=mysql_query($bmsql);
$bm = mysql_num_rows($bmresult);
$blsql="SELECT * FROM shop_orders WHERE brand = '".$bid."' AND title = 'Ladies' AND shop_order_month = '".$m."' AND shop_order_year = '".$y."' AND shop_order_location = 'Bawtry' AND shop_order_year = '".$y."' AND shop_order_action = 'Sale' ";
$blresult=mysql_query($blsql);
$bl = mysql_num_rows($blresult);
$cmsql="SELECT * FROM shop_orders WHERE brand = '".$bid."' AND title = 'Mens' AND shop_order_month = '".$m."' AND shop_order_year = '".$y."' AND shop_order_location = 'Chain' AND shop_order_year = '".$y."' AND shop_order_action = 'Sale' ";
$cmresult=mysql_query($cmsql);
$cm = mysql_num_rows($cmresult);
$clsql="SELECT * FROM shop_orders WHERE brand = '".$bid."' AND title = 'Ladies' AND shop_order_month = '".$m."' AND shop_order_year = '".$y."' AND shop_order_location = 'Chain' AND shop_order_year = '".$y."' AND shop_order_action = 'Sale' ";
$clresult=mysql_query($clsql);
$cl = mysql_num_rows($clresult);
$gmsql="SELECT * FROM shop_orders WHERE brand = '".$bid."' AND title = 'Mens' AND shop_order_month = '".$m."' AND shop_order_year = '".$y."' AND shop_order_location = 'Grantham' AND shop_order_year = '".$y."' AND shop_order_action = 'Sale' ";
$gmresult=mysql_query($gmsql);
$gm = mysql_num_rows($gmresult);
$glsql="SELECT * FROM shop_orders WHERE brand = '".$bid."' AND title = 'Ladies' AND shop_order_month = '".$m."' AND shop_order_year = '".$y."' AND shop_order_location = 'Grantham' AND shop_order_year = '".$y."' AND shop_order_action = 'Sale' ";
$glresult=mysql_query($glsql);
$gl = mysql_num_rows($glresult);
$imsql="SELECT * FROM shop_orders WHERE brand = '".$bid."' AND title = 'Mens' AND shop_order_month = '".$m."' AND shop_order_year = '".$y."' AND shop_order_location = 'Internet' AND shop_order_year = '".$y."' AND shop_order_action = 'Sale' ";
$imresult=mysql_query($imsql);
$im = mysql_num_rows($imresult);
$ilsql="SELECT * FROM shop_orders WHERE brand = '".$bid."' AND title = 'Ladies' AND shop_order_month = '".$m."' AND shop_order_year = '".$y."' AND shop_order_location = 'Internet' AND shop_order_year = '".$y."' AND shop_order_action = 'Sale' ";
$ilresult=mysql_query($ilsql);
$il = mysql_num_rows($ilresult);
$query = "SELECT SUM(shop_order_price) FROM shop_orders WHERE brand = '".$bid."' AND shop_order_month = '".$m."' AND shop_order_year = '".$y."' AND shop_order_action = 'Sale'";
$result3 = mysql_query($query) or die(mysql_error());
while($row3 = mysql_fetch_array($result3)){
?>
<?
$showtotal = $row3['SUM(shop_order_price)'] ;
$showaverage = $row3['SUM(shop_order_price)'] / $total ;
?>
<tr>
<td align="center"><strong><?=$brand['name'];?></strong></td>
<td align="center"><?=$bm?></td>
<td align="center"><?=$bl?></td>
<td align="center"><?=$bawtrytotal?></td>
<td align="center"><?=$cm?></td>
<td align="center"><?=$cl?></td>
<td align="center"><?=$chaintotal?></td>
<td align="center"><?=$gm?></td>
<td align="center"><?=$gl?></td>
<td align="center"><?=$granthamtotal?></td>
<td align="center"><?=$im?></td>
<td align="center"><?=$il?></td>
<td align="center"><?=$internettotal?></td>
<td align="center"><strong><?=$total?></strong></td>
<td align="center"><strong><?=round($showtotal, 2)?></strong></td>
<td align="center"><strong><?=round($showaverage, 2)?></strong></td>
</tr>
每个SELECT语句根据位置和性别提取与查询匹配的行数。匹配行的数量最终在底部回显到HTML中。表。
为了提高效率 - 我想知道是否可以结合所有这些语句来减少对数据库的调用次数。
我为这个烂摊子道歉 - 这不是我的代码!我最近来这个项目。
任何建议都将不胜感激。感谢
*更新*
好的,这是我的新SELECT语句:
SELECT * FROM shop_orders
LEFT JOIN product
ON shop_orders.shop_order_product_id=product.product_id
WHERE
DATE(CONCAT(shop_order_year,'-',shop_order_month,'-',shop_order_day))
BETWEEN '".$start."' and '".$end."' AND brand = '".$bid."' AND shop_order_location = 'Bawtry' AND shop_order_action = 'Sale'
OR
DATE(CONCAT(shop_order_year,'-',shop_order_month,'-',shop_order_day))
BETWEEN '".$start."' and '".$end."' AND brand = '".$bid."'
AND shop_order_location = 'Chain' AND shop_order_action = 'Sale'
OR
DATE(CONCAT(shop_order_year,'-',shop_order_month,'-',shop_order_day))
BETWEEN '".$start."' and '".$end."' AND brand = '".$bid."'
AND shop_order_location = 'Grantham' AND shop_order_action = 'Sale'
OR
DATE(CONCAT(shop_order_year,'-',shop_order_month,'-',shop_order_day))
BETWEEN '".$start."' and '".$end."' AND brand = '".$bid."'
AND shop_order_location = 'Internet' AND shop_order_action = 'Sale'
OR
DATE(CONCAT(shop_order_year,'-',shop_order_month,'-',shop_order_day))
BETWEEN '".$start."' and '".$end."' AND brand = '".$bid."'
AND category = 'Mens' AND shop_order_location = 'Bawtry' AND shop_order_action = 'Sale'
OR
DATE(CONCAT(shop_order_year,'-',shop_order_month,'-',shop_order_day))
BETWEEN '".$start."' and '".$end."' AND brand = '".$bid."'
AND category = 'Ladies' AND shop_order_location = 'Bawtry' AND shop_order_action = 'Sale'
OR
DATE(CONCAT(shop_order_year,'-',shop_order_month,'-',shop_order_day))
BETWEEN '".$start."' and '".$end."' AND brand = '".$bid."'
AND category = 'Mens' AND shop_order_location = 'Chain' AND shop_order_action = 'Sale'
OR
DATE(CONCAT(shop_order_year,'-',shop_order_month,'-',shop_order_day))
BETWEEN '".$start."' and '".$end."' AND brand = '".$bid."'
AND category = 'Ladies' AND shop_order_location = 'Chain' AND shop_order_action = 'Sale'
OR
DATE(CONCAT(shop_order_year,'-',shop_order_month,'-',shop_order_day))
BETWEEN '".$start."' and '".$end."' AND brand = '".$bid."'
AND category = 'Mens' AND shop_order_location = 'Grantham' AND shop_order_action = 'Sale'
OR
DATE(CONCAT(shop_order_year,'-',shop_order_month,'-',shop_order_day))
BETWEEN '".$start."' and '".$end."' AND brand = '".$bid."'
AND category = 'Ladies' AND shop_order_location = 'Grantham' AND shop_order_action = 'Sale'
OR
DATE(CONCAT(shop_order_year,'-',shop_order_month,'-',shop_order_day))
BETWEEN '".$start."' and '".$end."' AND brand = '".$bid."'
AND category = 'Mens' AND shop_order_location = 'Internet' AND shop_order_action = 'Sale'
OR
DATE(CONCAT(shop_order_year,'-',shop_order_month,'-',shop_order_day))
BETWEEN '".$start."' and '".$end."' AND brand = '".$bid."'
AND category = 'Ladies' AND shop_order_location = 'Internet' AND shop_order_action = 'Sale'" ;
我的新问题是如何绘制语句中每个OR变体的各个数字并将它们反映到HTML表格中?
由于
答案 0 :(得分:0)
您无法合并SELECT
,因为每个人都获取了一定数量的数据 - 如果您将它们合并 - 只需返回所有查询的总和。
但是,不是抓取所有元素而是抓取mysql_num_rows()
,而是可以像这样更改您的查询:
$sql2="SELECT COUNT(*) FROM shop_orders WHERE brand = '".$bid."' AND shop_order_month = '".$m."' AND shop_order_year = '".$y."' AND shop_order_action = 'Sale' ";
$total = mysql_fetch_array(mysql_query($sql2));
您不需要该行的所有元素(SELECT *
),但您似乎只需要受影响的元素数量。所以COUNT(*)
是您的选择!