简体   繁体   English

将多个查询组合到单个查询中

[英]Combining multiple queries into a single query

I have code that I've written in an ORM syntax. 我有用ORM语法编写的代码。 It reads blog comments data from a file, and inserts them into blogs and comments tables. 它从文件中读取博客评论数据,并将其插入到blogscomments表中。 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. 我想把这个ORM代码带回mysql,因为我需要将尽可能多的查询组合到一个查询中,而这种优化在ORM语言中并不容易。 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. 我在mysql伪代码中编写了下面的代码,因为我有点忘了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. from blog url告诉我这条评论属于哪个博客。

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. 这是我用来处理文件的ORM代码。 (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? 所以我的问题是:是否可以在单个mysql查询中编写它?

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 . 2.表是blogscomments ,其中每行comments有场blogid ,它链接到正确的博客blogs So it's basically a 1:many relationship where each blog row can be linked to many comment rows. 所以它基本上是1:多关系,其中每个blog行可以链接到许多comment行。 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

You can use this technique to insert a row if not exists: 如果不存在,您可以使用此技术插入行:

INSERT INTO blogs (link)
select 'first-blog-url' 
from dual
where not exists
( select 1
  from blogs
  where  link = 'first-blog-url'
);

As you can see, select clause will return a only one row with data to be inserted only when not yet exists in database. 如您所见,select子句只返回一行,只有当数据库中尚不存在时才会插入数据。 You can test it at SQLFIDDLE. 您可以在SQLFIDDLE上测试它。

To insert into comment table you can use same technique. 要插入comment表,您可以使用相同的技术。 You can get Blog id for second query with LAST_INSERT_ID() if inserted is dued (if not, you need a new query). 如果插入是dued,则可以使用LAST_INSERT_ID()获取第二个查询的Blog id (如果不是,则需要新查询)。

This is only a point to start, perhaps you can reduce to 3 your 4 queries. 这只是一个重要的开始点,也许你可以将你的4个查询减少到3个。 Any comment are welcome to figure up final solution. 欢迎任何评论以确定最终解决方案。

As you know, MySQL don't has MERGE statement. 如您所知,MySQL没有MERGE语句。 I think that replace don't match your requeriments. 我认为替换与你的requeriments不符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM