为了账号安全,请及时绑定邮箱和手机立即绑定

while 的困擾,如何改善成只要一個查詢?

while 的困擾,如何改善成只要一個查詢?

PHP
繁星点点滴滴 2019-03-10 08:56:19
$product_data = $mysqli->query( "SELECT p.url as url, p.description as description, p.info as info, p.note as note, p.content as content, p.name as pname, p.icon as icon, p.price as price, p.original_price as original_price, p.prod_id as prod_id, p.stock as stock, p.views as views, s.name as sname, c.name as cname, p.add_time as add_time FROM `product` as p JOIN `store` as s ON p.store_id = s.store_id JOIN `category` as c ON c.category_id = p.category_id WHERE p.active = 1 ORDER BY `prod_id` DESC while($row = mysqli_fetch_array($product_data)){ $theme = $mysqli->query( "SELECT t.name as tname FROM `product` as p JOIN `product_theme` as pt ON p.prod_id = pt.prod_id JOIN `theme` as t ON t.theme_id = pt.theme_id WHERE p.prod_id = '{$row['prod_id']}' " ); $buyTotal = mysqli_fetch_array($mysqli->query( "SELECT sum(quan) as quan FROM `stock` WHERE `prod_id` = '{$row['prod_id']}' " )); $viewsIp = mysqli_num_rows($mysqli->query( "SELECT * FROM `product_view` WHERE `prod_id` = '{$row['prod_id']}' " )); $pre = mysqli_num_rows($mysqli->query( "SELECT sum(quan) as quan FROM `user_pre` WHERE `prod_id` = '{$row['prod_id']}' " )); } 直接不刪減,給大神們看看我的困擾while 裡面的四個查詢都需要 $product_data 的 $row['prod_id']然後這這四個查詢都會有其他不同的作用例如顯示符合條件的有幾筆、印出某個欄位的加總 <?=$pre;?> <?=$buyTotal['quan'];?> 都是透過這樣的方式。當然希望大神可以教教我,幫我簡化簡化,感激不盡。
查看完整描述

2 回答

?
慕标琳琳

TA贡献1830条经验 获得超9个赞

不要在循环里面放Sql语句,可以把$row['prod_id']单独拿出来,sql语句可以写成wherein

查看完整回答
反对 回复 2019-03-18
  • 2 回答
  • 0 关注
  • 458 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信