dpw70180 2010-11-22 04:06
浏览 63
已采纳

SQL查询还是php代码?

Hello i am in a delima
Suppose that i have 50 products in a category and i want to get all the features of my products...
Ofcourse a SQL join wont help because joining would return product info many times!

So here is the question.. what is better
PLAN A
Get all the products' features in category with one SQL query and then iterate with php each feature and put it in Product.

PLAN B
For each product get the features by calling a query

Other solutions accepted!

EDIT

My table schema outline is..
A table Product which has product info(per row)
A table features which has features (feature id )
A table for features' values
And a table that has Products with their features and values

  • 写回答

4条回答 默认 最新

  • duanhuren5581 2010-11-22 05:04
    关注
    $sql1 = "SELECT * FROM products P,  ". //don't use star, make sure no fields are overwritten
             INNER JOIN products_to_features PTF on P.id = PTF.project_id
             INNER JOIN features F F.id = PTF.feature_id
             ORDER BY P.id";
    $r = mysql_query($sql1, $conn);
    
    $arr = array();
    $lastProductId = -1;
    while ($row = mysql_fetch_assoc($r))
    {
       if ($row[p_id] != $lastProductId)
       {
           $lastProductId = $row['p_id'];
           $arr['p_id'] = array('productName' => $row['p_name'], 
                                'productPrice' = $row['p_price'],
                                'productFeatures' = array(),
                                 //other fields from product table);
       }
       $arr['p_id']['productFeatures']['f_id'] = array('featureName' => $row['f_name'], blah...);
    }
    

    I don't know your fields obviously, and you may want to join on feature_values so that will be more work. You can do keys/values different (ie - product names as keys. Feature-name as keys with feature-value as values, whatever you want) but the point is this is doable (and recommended) in one query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测