# Check duplication of two MySQL tables

**URL:** https://discourse.nodered.org/t/check-duplication-of-two-mysql-tables/68527
**Category:** General
**Tags:** database
**Created:** [4 October 2022 01:51 UTC](https://discourse.nodered.org/t/check-duplication-of-two-mysql-tables/68527 "2022-10-04T01:51:15Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![afafirmansyah](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afafirmansyah/32/70076_2.png) [@afafirmansyah](https://discourse.nodered.org/u/afafirmansyah)
#### Post date: [4 October 2022 01:51 UTC](https://discourse.nodered.org/t/check-duplication-of-two-mysql-tables/68527/1 "2022-10-04T01:51:15Z")

</div>

How do I prevent the driver and vehicle data from being duplicated?  
when the data has been used by the driver, the data cannot be used by the vehicle and vice versa. when checking is complete then just insert data into MySQL.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/0/801f4810f736e9f6a219bb9403c89fcd57fec6b6.jpeg)

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [4 October 2022 05:58 UTC](https://discourse.nodered.org/t/check-duplication-of-two-mysql-tables/68527/2 "2022-10-04T05:58:44Z")

</div>

> [@afafirmansyah](#):
>
> just insert data into MySQL

Which should have a constraint, which in turn should produce an error if they exist.

But you could also first perform a `select driver_id from drivers where driver_id = yourpayload` if no results, continue the flow.

---

<div class="post-metadata">

### Author: ![afafirmansyah](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afafirmansyah/32/70076_2.png) [@afafirmansyah](https://discourse.nodered.org/u/afafirmansyah)
#### Post date: [4 October 2022 06:14 UTC](https://discourse.nodered.org/t/check-duplication-of-two-mysql-tables/68527/3 "2022-10-04T06:14:54Z")

</div>

can you give an example of the function program?

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [4 October 2022 06:26 UTC](https://discourse.nodered.org/t/check-duplication-of-two-mysql-tables/68527/4 "2022-10-04T06:26:24Z")

</div>

Node-red does not know if there are duplicates in the database, first query the database, if there are no results, continue.

example:

 ![SCR-20221004-bob](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/9/195cdba154a77ca9df1d0b4aff65ebfac4c932b7.png)

---

<div class="post-metadata">

### Author: ![afafirmansyah](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afafirmansyah/32/70076_2.png) [@afafirmansyah](https://discourse.nodered.org/u/afafirmansyah)
#### Post date: [4 October 2022 06:56 UTC](https://discourse.nodered.org/t/check-duplication-of-two-mysql-tables/68527/5 "2022-10-04T06:56:46Z")

</div>

Hi bakman,

can you export an example?  
I did it like the picture below, and still confused about how to do it

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/3/a3a5768458f364dd214d8cace9ef0ff1c19083c2.png)

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [4 October 2022 07:34 UTC](https://discourse.nodered.org/t/check-duplication-of-two-mysql-tables/68527/6 "2022-10-04T07:34:00Z")

</div>

First make sure you understand how to do it in mysql, which is nothing to do with node red. Once you understand that then experiment with the mysql node so you understand how to use it. Then try to solve the problem.  
I thought mysql had an Insert Or Update command which might solve your problem, so check that out first.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [4 October 2022 08:40 UTC](https://discourse.nodered.org/t/check-duplication-of-two-mysql-tables/68527/7 "2022-10-04T08:40:23Z")

</div>

> [@Colin](#):
>
> Insert Or Update command

... search for UPSERT...

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [4 October 2022 09:30 UTC](https://discourse.nodered.org/t/check-duplication-of-two-mysql-tables/68527/8 "2022-10-04T09:30:14Z")

</div>

> [@dceejay](#):
>
> ... search for UPSERT...

I've never heard of UPSERT, so I did ...

But I don't think it's applicable to this case where @afafirmansyah wants to INSERT INTO **drivers** only if there is no matching record in **vehicles**.

You don't say what database engine it is but you may be able to do something like this (which works for me in MySQL)

```auto
INSERT INTO drivers (driver_id, driver_name)
SELECT a.driver_id, a.driver_name
FROM (SELECT 'abc123' as driver_id, 'Bijul Kaur' as driver_name) a
WHERE NOT EXISTS (SELECT 1 FROM vehicles b WHERE b.vehicle_id = 'abc123');

```

However, you presumably want to inform the user that the drivers record was not created. The simplest way to achieve that is a seperate SELECT query as @bakman2 suggests.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [4 October 2022 12:40 UTC](https://discourse.nodered.org/t/check-duplication-of-two-mysql-tables/68527/9 "2022-10-04T12:40:42Z")

</div>

> [@afafirmansyah](#):
>
> when the data has been used by the driver, the data cannot be used by the vehicle and vice versa.

Can you explain what you mean by that please? Do you mean that both vehicle and driver ids must come from one pool of ids? So a driver cannot have the same id as a vehicle? If that is the case then I would put them in one table, with a field to identify whether this is a driver or a vehicle.

---

<div class="post-metadata">

### Author: ![afafirmansyah](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afafirmansyah/32/70076_2.png) [@afafirmansyah](https://discourse.nodered.org/u/afafirmansyah)
#### Post date: [5 October 2022 03:53 UTC](https://discourse.nodered.org/t/check-duplication-of-two-mysql-tables/68527/10 "2022-10-05T03:53:01Z")

</div>

thanks for the help, I have solved the above problem after getting inspired by bakman2's answer.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/3/e3182d878520b2112c154cf31bbfe62dd574d41b.png)

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [19 October 2022 03:53 UTC](https://discourse.nodered.org/t/check-duplication-of-two-mysql-tables/68527/11 "2022-10-19T03:53:19Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
