Select MYSQL strings with the same field names and add a prefix

I am trying to make a mysql query to select multiple tables and LEFT to join them, however they all have the same column name names, etc. I want to rename all fields this way. 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, mod_backup_accounts . * AS account . *makes him fail, is there any way to do this? so these will be the names as the account.

+3
source share
2 answers

, . , , 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
+5

.

, , . PHP, , , .

, mysql_field_table() mysql_field_name() , mysql_num_fields(), .

, "". .

,

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;
}
0

All Articles