简体   繁体   English

选择具有相同字段名称的MYSQL行并添加前缀

[英]Selecting MYSQL rows with same field names and adding a prefix

I'm trying to make a mysql query to select several tables and LEFT join them, however they all have same columns names 'user' etc. I want to rename all the fields in this manner . 我正在尝试创建一个mysql查询来选择几个表并且LEFT加入它们,但它们都有相同的列名称'user'等。我想以这种方式重命名所有字段。 so I tried the following query 所以我尝试了以下查询

SELECT mod_backup_accounts . * AS account . * , mod_backup_subscriptions . *
FROM `mod_backup_accounts`
LEFT JOIN `mod_backup_subscriptions` ON `mod_backup_accounts`.subscription_id = `mod_backup_subscriptions`.package_id

However the mod_backup_accounts . * AS account . * 但是mod_backup_accounts . * AS account . * mod_backup_accounts . * AS account . * mod_backup_accounts . * AS account . * makes it fail, is there a way to do this? mod_backup_accounts . * AS account . *让它失败,有没有办法做到这一点? so it would be names as account. 所以这将是帐户的名称。

You cannot supply a shorthand to alias columns you must do it explicitly for each column name. 您无法提供别名列的简写,您必须为每个列名明确地执行此操作。 In general anyway, it is typically recommended to name all columns explicitly in the SELECT list rather than using SELECT * , since it allows you to deterministically specify the column order, and protects you against accidentally pulling in a large BLOB later on if one ever gets added to the table ( or any other schema changes ). 一般来说,通常建议在SELECT列表中明确命名所有列,而不是使用SELECT * ,因为它允许您确定性地指定列顺序,并保护您不会在以后偶然引入大型BLOB添加到表(或任何其他架构更改)。

SELECT
  mod_backup_accounts.user AS account_user,
  mod_backup_subscriptions.user AS subscription_user,
  ...
  ...
FROM
  mod_backup_accounts
  LEFT JOIN `mod_backup_subscriptions` ON `mod_backup_accounts`.subscription_id = `mod_backup_subscriptions`.package_id

I totally understand your problem about duplicated field names. 我完全理解你关于重复字段名称的问题。

I needed that too until I coded my own function to solve it. 我也需要它,直到我编写自己的函数来解决它。 If you are using PHP you can use it, or code yours in the language you are using for if you have this following facilities. 如果您使用的是PHP,则可以使用它,或者如果您有以下设施,则使用您所使用的语言编写代码。

The trick here is that mysql_field_table() returns the table name and mysql_field_name() the field for each row in the result if it's got with mysql_num_fields() so you can mix them in a new array. 这里的技巧是, mysql_field_table()返回的表名和mysql_field_name()如果是与拿到了场为结果的每一行mysql_num_fields()所以你可以在一个新的数组混合。

You can also modify the function to only add the "column." 您还可以修改该功能以仅添加“列”。 prefix when the field name is duplicated. 字段名称重复时的前缀。

Regards, 问候,

function mysql_rows_with_columns($query) {
    $result = mysql_query($query);
    if (!$result) return false; // mysql_error() could be used outside
    $fields = mysql_num_fields($result);
    $rows = array();
    while ($row = mysql_fetch_row($result)) { 
        $newRow = array();
        for ($i=0; $i<$fields; $i++) {
            $table = mysql_field_table($result, $i);
            $name = mysql_field_name($result, $i);
            $newRow[$table . "." . $name] = $row[$i];
        }
        $rows[] = $newRow;
    }
    mysql_free_result($result);
    return $rows;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM