I have code that I've written in an ORM syntax. It reads blog comments data from a file, and inserts them into blogs
and comments
tables. I'd like to take this ORM code back to mysql, because I need to combine as many queries as possible into a single query, and this optimization wouldn't be easy in the ORM language. The reason I need this optimization is because I'm working with a remote server, so the fewer the queries, the better. I wrote the code below in mysql pseudo-code, because I somewhat forgot mysql.
This is the comments file that contains all the comments for all the blogs. from blog url
tells me which blog this comment belongs to.
comment text from blog url
------------------------------------------
first comment text first-blog-url
second comment text first-blog-url
third comment text first-blog-url
fourth comment text blog-2-url
fifth comment text blog-2-url
sixth comment text 3rd-blog-url
This is the ORM code that I use to process the file. (at the very bottom, I added the description of the tables).
//I read a comment from the comments file, `comment text` and `from blog url`
//does a blog exist that has 'link' that matches 'from blog url'
$blog = //SELECT FROM blogs where 'link' has value 'first-blog-url'
//if it doesn't exist, create it
if($blog == null){
$blog = INSERT INTO blogs a new record and set 'link' to 'first-blog-url'
}
//then read the id of the (existing or just-created) blog row
$blog_id = $blog->getId();
//then use the $blog_id to insert the comment into the 'comments' table.
//does this comment text already exist for this blog id?
$comment = SELECT FROM comments where `commenttext' has value 'whatever comment text' and 'blogid' has value $blog_id
//if it doesn't exist, create it
if($comment == null){
$comment = INSERT INTO comments a new record and set 'commenttext' to 'the comment text' and 'blogid' to $blog_id.
}
$comment_id = $comment->getId();
So my question: is it possible to write this in a single mysql query?
I found a similar question here but it doesn't fully solve my problem, and I'm not sure if it's the most efficient way to do it.
The 2 tables are blogs
and comments
where each row in comments
has a field blogid
that links it to the right blog in blogs
. So it's basically a 1:many relationship where each blog
row can be linked to many comment
rows. They look like this:
blogs:
id link other fields
--------------------------------------------
1 first-blog-url
2 blog-2-url
3 3rd-blog-url
comments:
id commenttext blogid
-----------------------------
1 random 1
2 comment 1
3 goes 1
4 here 2
5 any 2
6 thing 3