Mysql dyanmic值为别名

时间:2016-01-06 04:36:17

标签: php mysql entity-attribute-value

以下作品

$query = "SELECT 
        A.entity_id,
        B.value AS variable_character,
        C.value AS text,
        D.value AS integer
    FROM customer_address_entity AS A 
    LEFT JOIN customer_address_entity_varchar AS B 
        ON B.entity_id = A.entity_id
    LEFT JOIN customer_address_entity_text AS C 
        ON C.entity_id = A.entity_id
    LEFT JOIN customer_address_entity_int AS D 
        ON A.entity_id = D.entity_id
    ORDER BY A.entity_id DESC
    LIMIT 100";

但是,有许多值需要连接到表A中的实体,现在它们都是基于新值创建自己的嵌套数组。

0 => 
array (size=4)
  'entity_id' => string '597424' (length=6)
  'variable_character' => string 'Dave' (length=4)
  'text' => string '45 Haven Rd' (length=11)
  'intiger' => string '43' (length=2)
1 => 
array (size=4)
  'entity_id' => string '597424' (length=6)
  'variable_character' => null
  'text' => string '45 Haven Rd' (length=11)
  'intiger' => string '43' (length=2)
2 => 
array (size=4)
  'entity_id' => string '597424' (length=6)
  'variable_character' => string 'Danielson' (length=9)
  'text' => string '45 Haven Rd' (length=11)
  'integer' => string '43' (length=2)
3 => etc
...

我认为这是由于尝试加入相同的密钥名称,因此我想使用更动态的内容,例如B.attribute_type_id的值等其他值。在哪里我可以得到一个看起来像

的数组
array (size=7)
  'entity_id' => string '597424' (length=6)
  '1' => null
  '2' => 'Dave'
  '3' => 'Danielson'
  '4' => '45 Haven Road'
  'text' => string '45 Haven Rd' (length=11)
  'intiger' => string '43' (length=2)

甚至更好: 属性的标题存在于另一个名为eav_attribute的表中,真正理想的是

$query = "SELECT
    A.entity_id,
    B.value AS (SELECT attribute_code FROM eav_attribute WHERE attribute_id = B.attribute_id),
    C.value AS text,
    ...

这样的事情可能吗?或者我是以错误的方式解决这个问题?

1 个答案:

答案 0 :(得分:0)

为什么使用左连接而不是内连接?此外,如果您正在进行动态查询。您应该考虑使用存储过程。您可以将SQL语句挂载到变量中,然后执行。这是一个简单的例子:



CREATE DEFINER=`YourDBUSER`@`localhost` PROCEDURE `rtYourProcedureName`(IN variable1 int,variable2d varchar(100), sortBy varchar(50), startRow int, ThisSQL varchar(5000), OUT totalRows int)
BEGIN
###start: MySQL is dumb and do not allow default parameter values, set here:
SET @variable1 = IFNULL(variable1 ,'0');
SET @variable2 = IFNULL(variable2 ,'');
SET @sortBy = IFNULL(sortBy ,'');
SET @startRow = IFNULL(startRow ,1);
###end: MySQL is dumb and do not allow default parameter values

set @htwsql = ThisSQL;

set @htwsql = ' SELECT SQL_CALC_FOUND_ROWS STRAIGHT_JOIN t1.field1 ,t2.field2 ';

if @variable1 = 0 then
	set @htwsql := concat(@htwsql, ' from table1 t1');
else 
	set @htwsql := concat(@htwsql,' from table2 t2 w INNER JOIN table1 t1 ');
end if;

set @htwsql := concat(@htwsql, ' where 1 = 1 ');


### sort order
if rtrim(ltrim(@sortBy)) <> '' then
	set @htwsql := concat(@htwsql,char(13), ' ORDER BY ' , cast(@sortBy as char(50)));
end if;

### limit records for pagination
set @htwsql := concat(@htwsql,char(13), ' LIMIT ', StartRow-1 ,',',24);

### just to debug the generated SQL
insert into rtlogtmp (log) select concat ('rtYourProcedureName SQL = ',@htwsql);


PREPARE stmt1 FROM @htwsql; 

EXECUTE stmt1; 

DEALLOCATE PREPARE stmt1;

SELECT FOUND_ROWS() into totalRows;

END
&#13;
&#13;
&#13;

p.s。:请注意您用作别名的名称。它们可能是保留的SQL语法。