我有一张桌子,显示一种产品,但供应商不同。
SQL:
$q=3000;
$sql="SELECT product_id, product_reference AS kood,product_name AS nimetus,product_quantity AS nr, pl.link_rewrite,psa.quantity as kogukogus,ps.product_supplier_reference as supp_ref,ps.product_supplier_url as supp_url, p.location AS asukoht,
CONCAT(c.link_rewrite,'/',p.id_product,'-',pl.link_rewrite,'.html') link
FROM ps_order_detail o
JOIN ps_product_lang pl on o.product_id = pl.id_product
JOIN ps_product p on p.id_product = pl.id_product
JOIN ps_stock_available psa on p.id_product = psa.id_product
JOIN ps_category_lang c on c.id_category = p.id_category_default
JOIN ps_product_supplier ps on p.id_product = ps.id_product
WHERE pl.id_lang=2 AND c.id_lang=2 AND id_order= '".$q."'";
PHP:
$rs=$ib->query($sql);
if (PEAR::isError($rs)) die($rs->getMessage());
if($rs) {
while($r = $rs->fetchRow(MDB2_FETCHMODE_ASSOC)) {
$supref=$r["supp_ref"];
if($supref!="" || $supref!=null){
$suppref=$supref;
}
$out .= "\n".'<tr>';
//Pildi lingi leidmine
$rs2=$ib->query("SELECT id_image FROM ps_image WHERE id_product=".$r["product_id"]." AND cover=1");
while($r2 = $rs2->fetchRow(MDB2_FETCHMODE_ASSOC)) { $ids=$r2["id_image"]; }
//Kui on lisame pildid
if (!isset($_GET["ni"]))
$out .= '<td><a href="'.getImageLink(STRIPSLASHES($r["link_rewrite"]),$ids, 'large').'"><img src="'.getImageLink(STRIPSLASHES($r["link_rewrite"]),$ids, 'small').'" /></a></td>';
//Tabeli tekstiosa
$out .= '<td><b><font size=+1>'.$r["kood"].'</font></b> - <a href="'.$vmpath.$r["link"].'" target=_new>'.$r["nimetus"].'</a><br> '.$r["tootja_kood"].' </td><td><a>'.$suppref.'</a></td><td>'.$r["nr"].'</td><td>'.$r["kogukogus"].'</td><td><b>'.$r["asukoht"].'</b></td></tr>';
}
$out .="\n</table>\n";
}
如何将两个不同的供应商合并为一个列?像这样:
答案 0 :(得分:1)
您可以在product_supplier_reference上使用group_concat,这将获取group by子句中的所有值,并从中创建单个字段。
$sql="SELECT product_id, product_reference AS kood,product_name AS nimetus,product_quantity AS nr, pl.link_rewrite,psa.quantity as kogukogus,group_concat(ps.product_supplier_reference) as supp_ref,ps.product_supplier_url as supp_url, p.location AS asukoht,
CONCAT(c.link_rewrite,'/',p.id_product,'-',pl.link_rewrite,'.html') link
FROM ps_order_detail o
JOIN ps_product_lang pl on o.product_id = pl.id_product
JOIN ps_product p on p.id_product = pl.id_product
JOIN ps_stock_available psa on p.id_product = psa.id_product
JOIN ps_category_lang c on c.id_category = p.id_category_default
JOIN ps_product_supplier ps on p.id_product = ps.id_product
WHERE pl.id_lang=2 AND c.id_lang=2 AND id_order= '".$q."'
GROUP BY product_id, kood,nimetus,nr,pl.link_rewrite, kogukogus,supp_url,asukoht, link";
虽然您可能需要删除ps.product_supplier_url作为supp_url - 请先了解它的工作原理。
我还建议将链接之类的东西转移到前端,因为这样可以让程序员在事情发生变化时更灵活 - 而不是搞乱复杂的SQL语句。