Php Mysql动态值作为别名

Php Mysql动态值作为别名,php,mysql,entity-attribute-value,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_i

所以下面的工作

$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\u type\u 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,
    ...

这样的事情可能吗?还是我的做法不对?

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

CREATE DEFINER=`YourDBUSER`@`localhost`PROCEDURE`rtYourProcedureName`IN variable1 int,variable2d varchar100,sortBy varchar50,startRow int,ThisSQL varchar5000,OUT totalRows int 开始 开始:MySQL是哑的,不允许默认参数值,设置如下: 设置@variable1=IFNULLvariable1,'0'; 设置@variable2=IFNULLvariable2,; 设置@sortBy=IFNULLsortBy,; 设置@startRow=IFNULLstartRow,1; 结束:MySQL是哑的,不允许默认参数值 设置@htwsql=ThisSQL; set@htwsql='SELECT SQL\u CALC\u FOUND\u ROWS stright\u JOIN t1.field1、t2.field2'; 如果@variable1=0,则 set@htwsql:=concat@htwsql“来自表1 t1”; 其他的 set@htwsql:=concat@htwsql,'来自表2 t2 w内部连接表1 t1'; 如果结束; set@htwsql:=concat@htwsql,'其中1=1'; 排序顺序 如果rtrimltrim@sortBy然后 set@htwsql:=concat@htwsql,第13章,'订购人',cast@sortBy如char50; 如果结束; 限制分页的记录 set@htwsql:=concat@htwsql,char13,'极限',StartRow-1,',',,24; 只需调试生成的SQL 在rtlogtmp日志中插入选择concat'rtYourProcedureName SQL=',@htwsql; 从@htwsql准备stmt1; 执行stmt1; 解除分配准备stmt1; 将找到的行选择为totalRows;
END为什么要使用左连接而不是内连接?另外,如果您正在执行动态查询。您应该考虑使用存储过程。您可以将SQL语句装入变量中,然后执行。下面是一个简单的例子:

CREATE DEFINER=`YourDBUSER`@`localhost`PROCEDURE`rtYourProcedureName`IN variable1 int,variable2d varchar100,sortBy varchar50,startRow int,ThisSQL varchar5000,OUT totalRows int 开始 开始:MySQL是哑的,不允许默认参数值,设置如下: 设置@variable1=IFNULLvariable1,'0'; 设置@variable2=IFNULLvariable2,; 设置@sortBy=IFNULLsortBy,; 设置@startRow=IFNULLstartRow,1; 结束:MySQL是哑的,不允许默认参数值 设置@htwsql=ThisSQL; set@htwsql='SELECT SQL\u CALC\u FOUND\u ROWS stright\u JOIN t1.field1、t2.field2'; 如果@variable1=0,则 set@htwsql:=concat@htwsql“来自表1 t1”; 其他的 set@htwsql:=concat@htwsql,'来自表2 t2 w内部连接表1 t1'; 如果结束; set@htwsql:=concat@htwsql,'其中1=1'; 排序顺序 如果rtrimltrim@sortBy然后 set@htwsql:=concat@htwsql,第13章,'订购人',cast@sortBy如char50; 如果结束; 限制分页的记录 set@htwsql:=concat@htwsql,char13,'极限',StartRow-1,',',,24; 只需调试生成的SQL 在rtlogtmp日志中插入选择concat'rtYourProcedureName SQL=',@htwsql; 从@htwsql准备stmt1; 执行stmt1; 解除分配准备stmt1; 将找到的行选择为totalRows; 终止