I want to insert the user_Id from one table into the parent_id of another table, but there are no common values between the two tables.
P粉323224129
2023-07-30 13:35:25
<p style="white-space:normal;">I currently have three tables: </p><p style="white-space:normal;">users table//Data is inserted during registration <pre class="brush:php;toolbar:false;">usersId //PRIMARY KEY
userfName
userlName
userUid
email
userPwd
dateTime</pre>
<p><code>parent</code> table</p>
<pre class="brush:php;toolbar:false;">id //PRIMARY KEY
ikB
mLname
mEmail
mPhone
fname
fLname
fEmail
fPhone
addressL1
addressL2
city
stateAbbr
zip
created_at
user_id //FOREIGN KEY</pre>
<p>I used the email column from the users table and mEmail or fEmail to insert the usersId into the parents table. </p>
<p><code>Children</code> Table</p>
<pre class="brush:php;toolbar:false;">child_id //PRIMARY KEY
child1Name
dobChild1
ageChild1
child2Name
dobChild2
ageChild2
child3Name
dobChild3
ageChild3
child4Name
dobChild4
ageChild4
child5Name
dobChild5
ageChild5
child6Name
dobChild6
ageChild6
child7Name
dobChild7
ageChild7
child8Name
dobChild8
ageChild8
child9Name
dobChild9
ageChild9
child10Name
dobChild10
ageChild10
parent_id //FOREIGN KEY</pre>
<p>There are no common values between the children's table and the other two tables. My expectation is to do a validation between the users table and the parent table, and if the users table's usersId and the parent table's user_id are equal, insert that value into the children table's parent_id. Is this possible? I considered using a cross join, but I'm concerned that performance will suffer as the number of entries in the database grows. </p>
<pre class="brush:php;toolbar:false;">SELECT usersId FROM users;
SELECT user_id FROM parent;
UPDATE children (parent_Id) INNER JOIN parent ON children.parent_id = users.usersId
UPDATE children
SELECT usersId
FROM users
cross JOIN children.parent_id ON users.usersId = children.parent_id
UPDATE children
INNER JOIN users
(INNER JOIN parent ON users.userId = parent.userId)
ON users.usersId = parent.user_id
SET children.parent_id = parent.user_Id;</pre>
<p>I have tried every method with no success. I also tried changing the order of the connections but that didn't work either. Thank you very much for any help you can provide. </p>
According to your description, you want to update the parent_id column in the child table based on the matching value between the user table and the parent table. You want to update the parent_id in the child table with the usersId from the users table and the user_id from the parent table.
You can do this using the following SQL query:
Explanation:
Be sure to back up your database before running any update queries, just to be safe. ,
As for your concerns about performance as database entries grow, this query should be efficient as long as there are appropriate indexes on the relevant columns, such as email in the users table and mEmail and fEmail in the parent table . Indexes will significantly speed up the lookup process, especially when working with large data sets.