sidorares / node-mysql2

:zap: fast mysqljs/mysql compatible mysql driver for node.js
https://sidorares.github.io/node-mysql2/
MIT License
4.06k stars 615 forks source link

duplicate column names #1342

Open sb2050 opened 3 years ago

sb2050 commented 3 years ago

If I have duplicate column names they will be overwritten. Is there a solution for this? For example. That with the column names duplicate results table names with indicates.

output:

[
  BinaryRow {
    id: 1,
    group: 1,
    name: 'user03',
    item1: 'sword',
    item2: 'fire',
    item3: 'spear',
    item4: 'switch'
  },
  BinaryRow {
    id: 2,
    group: 2,
    name: 'user04',
    item1: 'shield',
    item2: 'hat',
    item3: 'bow',
    item4: 'cap'
  }
]

output expected: image

database:

-- MySQL dump 10.13  Distrib 5.7.24, for Win32 (AMD64)
--
-- Host: 127.0.0.1    Database: test
-- ------------------------------------------------------
-- Server version   5.7.24

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `tabel1`
--

DROP TABLE IF EXISTS `tabel1`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `tabel1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `group` int(11) DEFAULT NULL,
  `name` text,
  `item1` text,
  `item2` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `tabel1`
--

LOCK TABLES `tabel1` WRITE;
/*!40000 ALTER TABLE `tabel1` DISABLE KEYS */;
INSERT INTO `tabel1` (`id`, `group`, `name`, `item1`, `item2`) VALUES (1,1,'user01','sword','fire'),(2,2,'user02','shield','hat');
/*!40000 ALTER TABLE `tabel1` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `tabel2`
--

DROP TABLE IF EXISTS `tabel2`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `tabel2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `group` int(11) DEFAULT NULL,
  `name` text,
  `item3` text,
  `item4` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `tabel2`
--

LOCK TABLES `tabel2` WRITE;
/*!40000 ALTER TABLE `tabel2` DISABLE KEYS */;
INSERT INTO `tabel2` (`id`, `group`, `name`, `item3`, `item4`) VALUES (1,1,'user03','spear','switch'),(2,2,'user04','bow','cap');
/*!40000 ALTER TABLE `tabel2` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2021-06-09 18:29:05
Badestrand commented 3 years ago

There are two ways you can go about this:

a) query "nested" objects, this will result in

[{
    tabel1: {
        id: 123,
        group: 'xyz",
        ...
    },
    tabel2: {
        id: 234,
        group: 'zyx",
        ...
    },
},
...
]

You can achieve this by passing options that contain nestTables: true.

b) Query results as array. This way you will get the result, well, as an array of values instead of a JSON object. I don't have the option name handy but a search in the docs should find it.