Jump to content

Recommended Posts

I want to join two tables together, consisting of the same columns with different records.

 

INSERT INTO table(ID, col1, col2) values

(1, 'rec', '')

(2, 'rec2', '')

(3, '', '')

(4, '', '')

 

table 2:

 

INSERT INTO table(ID, col1, col2) values

(1, 'rec', '')

(2, 'rec2', '')

(3, '', '')

(4, '', '')

 

I know I will get SQL error, duplicated entry (ID)

is there a way it can insert a non existing ID in the table instead of saying there is a duplicate?

Link to comment
https://forums.phpfreaks.com/topic/207061-joining-tables/
Share on other sites

Your request isn't making sense. You say you want to JOIN tables but then you are asking if you can INSERT. Based upon your examples you are trying to run the same insert statement twice.

 

...is there a way it can insert a non existing ID in the table instead of saying there is a duplicate?

 

If the ID is non existent you won't get a duplicate error. I *think* you are trying to run an insert of multiple records where some of those records may be duplicates. But your question and example aren't clear. Please explain again.

Link to comment
https://forums.phpfreaks.com/topic/207061-joining-tables/#findComment-1082712
Share on other sites

Your request isn't making sense. You say you want to JOIN tables but then you are asking if you can INSERT. Based upon your examples you are trying to run the same insert statement twice.

 

Basically I am trying to insert rows into an existing table which has hundreds of ID duplicates therefore it won't insert. Same IDs but different records so my question is if I can insert and it changes the ID duplicates to another ID that does not exist in the table?

Link to comment
https://forums.phpfreaks.com/topic/207061-joining-tables/#findComment-1082722
Share on other sites

Why are you trying to do that? = I'm trying to combine both my user tables together but some have the same ID so it won't execute.

What is there now = the new records

what are you trying to insert = keeping the old records and inserting the new ones

what is the end result you're hoping to achieve? = just to put all records together and each having different ID

Link to comment
https://forums.phpfreaks.com/topic/207061-joining-tables/#findComment-1082729
Share on other sites

Ok, yes ID is the primary key and you say I should not insert it.

 

(7,1,8,0,1,0),

(8,1,9,0,1,0),

(9,1,10,0,1,0),

(31,1,29,0,0,0),

(26,1,27,0,1,0),

(17,1,17,0,1,0),

(13,1,15,0,1,0),

(107,4,38,0,1,0),

(18,1,18,0,1,0),

(19,1,19,0,1,0),

(20,1,20,0,1,0),

(22,1,21,0,1,0),

(23,1,22,0,1,0),

 

The list above is hundreds and hundreds. Is there a quick way to remove the first column?

 

(7,1,8,0,1,0) == (1,8,0,1,0) etc

Link to comment
https://forums.phpfreaks.com/topic/207061-joining-tables/#findComment-1082739
Share on other sites

Ok, this is an example where more information would have been better. It wasn't until your third post that what you are trying to achieve made sense:

I'm trying to combine both my user tables together but some have the same ID so it won't execute...

 

I was going to suggest ON DUPLICATE KEY in your query (as harristweed suggested) until I read that. The solution to this should be very simple. I will assume that the ID field for your tables is also an auto-increment field. So, the easy solution is DON'T INCLUDE THE ID FIELD IN THE INSERT QUERY.

 

In fact, if both tables are in the same database (copy one into a temp table in the other if they aren't) then you only need a single query to combine the records into one table:

 

Not tested, but this should be about right

INSERT INTO users (col1, col2)
VALUES (SELECT col1, col2 FROM users_temp)

Link to comment
https://forums.phpfreaks.com/topic/207061-joining-tables/#findComment-1082751
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.