用PHP和MySQL的数据透视表

时间:2014-04-03 15:44:50

标签: php mysql join pivot pivot-table

如何使用MySQL数据透视表与php为精炼搜索系统创建动态查询?

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'MAX(IF(ad.name = ''',
      name,
      ''', pa.text, NULL )) AS ',
      replace(name,' ', '_')
    )
  ) INTO @sql
FROM attribute_description, product_attribute;

SET @sql = CONCAT('SELECT p.id
                    , p.titlu
                    , p.descriere, ', @sql, ' 
                   FROM produse p
                   LEFT JOIN product_attribute AS pa 
                   ON p.id = pa.product_id
                   LEFT JOIN attribute_description AS ad
                   ON pa.attribute_id = ad.attribute_id where p.vizibil=''1'' 
                group by id having tara_de_origine = "romania" and instant = "da"');

PREPARE stmt FROM @sql;
EXECUTE stmt;

在这样的事情

$sql = sprintf("pivot table query ......", $var, $var2);

我以前的查询是

    $sql = sprintf("SELECT *, p.id as pid FROM produse p 
                    INNER JOIN categorii c on p.categorie=c.id
                    LEFT JOIN product_attribute pa on p.id=pa.product_id
                    LEFT JOIN attribute_description ad on pa.attribute_id=ad.attribute_id
                    WHERE c.seo = '%s' AND p.vizibil= 1", $_GET['c']);

0 个答案:

没有答案