mysql - Overcoming Extra Rows -
i have insert multiple columns different tables keep having same problem.
the problem this: can insert column tablea.columna tableb.columna no problem. next need insert tablec.columna tableb.columnb , insert command adds rows change id's of data.
my insert commands, plus update command executing id's match (unsuccessfully) below.
i know insert command can add rows how passed id's match?
i thought maybe columns had auto increment set not -- not seem problem. appreciated.
(1) insert users_posts (author_id, post_id, post_title) select contents.user_id, contents.id, contents.title contents contents.id = id (2) insert users_posts (post_content) select content_bodies.content content_bodies content_bodies.id = id (3) update users_posts set post_content = (select content_bodies.content content_bodies content_bodies.id = users_posts.post_id)
if understand question correct, not want second , third statements, rather update statement update users_posts.post_content
if insert (assuming doesn't fail other reasons i.e. constraints) insert new rows, hence keyword insert
.
try statement adds post content users_posts
table content_bodies
table based on ids!
update users_posts u join content_bodies b on b.id = u.post_id set u.post_content = b.content;
see in action sqlfiddle
edit
please run query , let me know if matches data correctly
select * users_posts u join content_bodies b on b.id = u.post_id
Comments
Post a Comment