AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 300600
Accepted
user237107
user237107
Asked: 2021-10-05 09:40:22 +0800 CST2021-10-05 09:40:22 +0800 CST 2021-10-05 09:40:22 +0800 CST

左连接中的列冲突与许多列与 PHPMyAdmin 结果集

  • 772

我有 4 个表(每个表大约 75 列),但它们共享具有相同名称的列:

表大师_产品

Master_Product.PD_Prof_21

表产品_结构

Product_Structure.PD_Prof_21

我正在做左连接;发生的情况是,当它将它们带到后端(php)时,它们会被覆盖……预期为 2 PD_Prof_21,但在 php 中只有一个是 retrive。

基于这个解决方案(它不能解决我的场景,因为我有大约 300 列)https://stackoverflow.com/a/10530252/4717133

我不相信使用超过 300 列的别名的想法......我知道列中的别名类似于:

SELECT Columns as column1 ... // this would be very heavy to do for each column ...

此其他选项不会分隔结果集中的表

SELECT 
Master_Product.*,
Product_Structure.*,
Supplier_Product.*,
Product_Price.*,

或者至少在 php 中我有问题,事实证明在 300 列中我只收到那些不重复的列,如果重复列,它只会显示最后一个具有最后一个值的列。

第一个疑问

所以我想知道mysql中是否有任何方法允许自动为结果集查询中的所有列添加前缀,例如:

SELECT PREFIX ('Master_', Master_Product. *), PREFIX ('Structure_', Product_Structure. *) ...

并在 PHP 中访问:

Master_PD_Prof_21
Structure_PD_Prof_21

用于检索信息的 PHP 脚本:

连接

function Open_Con_DB2($dbUsing) {
        $dblink = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASS, DB_PRE . $dbUsing);
        
        if ($dblink) {
            $dblink->query("SET NAMES 'utf8'"); # Enlace de Coneccion UTF-8
            return $dblink;
        } else {
            #No conviene mostrar errores internos
            #Cámbialos por mensajes personalizados en producción
            die( 'Error de Conexión ('
                 .mysqli_connect_errno(). ') '
                 . mysqli_connect_error()
                );
            return null;
        }
    }

获取信息。

function BDquery2($dbquery, $dbUsing) {
        $dblink = $this->Open_Con_DB2($dbUsing);
        if ($dblink) {
            if ( $datos = mysqli_query($dblink, $dbquery) ) {
                $outPut=array();
                while ($row = mysqli_fetch_assoc($datos)) {
                    $outPut[]=$row;
                }
            } else {
                $outPut['error']=$this->errorDB($dblink);
            }
            $this->Close_Con_DB($dblink);
        } else {
            $outPut['error']='No hay conexión';
        }
        return $outPut;
    }

查询已执行:( 对不起,数据库是西班牙语 XD)

SELECT 
Master_Producto.*, 
Producto_Estructura.*,
Producto_Proveedor.*,
Producto_Precio.*,
FROM Master_Producto
LEFT JOIN(SELECT * FROM Producto_Estructura) AS Producto_Estructura ON ( Master_Producto.Prod_Code = Producto_Estructura.Prod_Code AND Master_Producto.Prod_PF = Producto_Estructura.Prod_PF)
LEFT JOIN(SELECT * FROM Producto_Proveedor) AS Producto_Proveedor ON ( Master_Producto.Prod_Code = Producto_Proveedor.Prod_Code AND Master_Producto.Prod_PF = Producto_Proveedor.Prod_PF)
LEFT JOIN(SELECT * FROM Producto_Precio ORDER BY Prod_DateUpd DESC) AS Producto_Precio ON ( Master_Producto.Prod_Code = Producto_Precio.Prod_Code AND Master_Producto.Prod_PF = Producto_Precio.Prod_PF)

第二个疑问

奇怪的是,PHPmyadmin 如果它向我显示完整的结果......它为每个表显示 1 个部分......但我不能在 php.ini 中自己做。此示例是执行相同查询管的结果,该查询管在图像中对其进行裁剪,以便他们欣赏列的重复。

在此处输入图像描述

mysql query
  • 1 1 个回答
  • 121 Views

1 个回答

  • Voted
  1. Best Answer
    user237107
    2021-10-05T17:53:13+08:002021-10-05T17:53:13+08:00

    除了为结果分析创建适当的解决方案外,我别无选择。老实说,数据库设计不好并不是不给它解决方案的借口……最好的解决方案是使用后端的数据:

    
        // Conection to database
        function Open_Con_PDO($dbUsing){
            try {
                $conn = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_PRE . $dbUsing, DB_USERNAME, DB_PASS);
                $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $conn->exec("set names utf8");
                return $conn;
            } catch (PDOException $e) {
                die( $e->getMessage());
            }
        }
        
        // function to excute the query
        function BDquery2($dbquery, $dbUsing) {
           $dblink = $this->Open_Con_PDO($dbUsing);
           $resultset = $dblink->prepare($dbquery);
           $resultset->execute();
           $outPut=[];
           $rowc =0;
           while ($row = $resultset->fetch(PDO::FETCH_NUM)) {
                $outPut[$rowc]=[];
                foreach($row as $key => $value){
                    $outPut[$rowc][$key]=[
                        'ColumnName'=>$resultset->getColumnMeta($key)['name'],
                        'value'=>$value
                    ];
                }
                $rowc++;
           }
           return $outPut;
        }
    
        // USage Example:
        $dbquery = "SELECT 
    Master_Producto.*, 
    Producto_Estructura.*,
    Producto_Proveedor.*,
    Producto_Precio.*,
    FROM Master_Producto
    LEFT JOIN(SELECT * FROM Producto_Estructura) AS Producto_Estructura ON ( Master_Producto.Prod_Code = Producto_Estructura.Prod_Code AND Master_Producto.Prod_PF = Producto_Estructura.Prod_PF)
    LEFT JOIN(SELECT * FROM Producto_Proveedor) AS Producto_Proveedor ON ( Master_Producto.Prod_Code = Producto_Proveedor.Prod_Code AND Master_Producto.Prod_PF = Producto_Proveedor.Prod_PF)
    LEFT JOIN(SELECT * FROM Producto_Precio ORDER BY Prod_DateUpd DESC) AS Producto_Precio ON ( Master_Producto.Prod_Code = Producto_Precio.Prod_Code AND Master_Producto.Prod_PF = Producto_Precio.Prod_PF)";
    
    
        $result = BDquery2($dbquery,'database_name');
    
        var_dump($result);
    
    
    

    输出示例:

    $result=[
        10=>[
            'ColumnName'='Prod_D_1',
            'value'=21
        ],
        ...
        24=>[
            'ColumnName'='Prod_D_1',
            'value'=1
        ],
    ];
    

    pieda没有写任何获取数据的函数提供的结构可能无法解决其他复杂的场景,但它是那些发现这篇文章的人在面对列名的重复/冲突时有一个起点的基础。

    • 0

相关问题

  • 是否有任何 MySQL 基准测试工具?[关闭]

  • 我在哪里可以找到mysql慢日志?

  • 如何优化大型数据库的 mysqldump?

  • 什么时候是使用 MariaDB 而不是 MySQL 的合适时机,为什么?

  • 组如何跟踪数据库架构更改?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve