简体   繁体   English

不清楚的MySQL查询语法

[英]unclear mySql query syntax

A client requested me to perform some changes in an SQL query and sent me exactly the query he wanted. 一位客户要求我在SQL查询中执行一些更改,然后向我发送他想要的查询。 He usually works on msSql but says it should work also on MySql which is the platform i'm using. 他通常在msSql上工作,但说它也应该在MySql(我正在使用的平台)上工作。

This is the query : 这是查询:

INSERT INTO     Messages (Match, Message)
SELECT          [Match], MM.Message
FROM            MatchMessages AS MM
INNER JOIN  Matches AS M
ON      MM.ActionBy=M.ActionBy
AND     MM.Status=M.Status

I'm not sure where the insert get the correct values from the select and join, and how does it know where to enter which value. 我不确定插入在哪里从选择和联接中获取正确的值,以及它如何知道在哪里输入哪个值。 What is happening in this query? 此查询中发生了什么? There is no VALUES anywhere and there are more selected values then targeted in the insert... 在任何地方都没有VALUES,并且在插入中还有更多选择的值作为目标...

Does this query looks logical? 这个查询看起来合乎逻辑吗?

Yes - it is an absolutely normal query... Let's break this down: 是的-这是绝对正常的查询...让我们细分一下:

Part one 第一部分

SELECT          [Match], MM.Message
FROM            MatchMessages AS MM
INNER JOIN  Matches AS M
ON      MM.ActionBy=M.ActionBy
AND     MM.Status=M.Status

This gets executed first and returns 0-n rows of data... each row containing 2 columns (Match and Message) in this specific order. 这首先执行,并返回0-n行数据...每一行按此特定顺序包含2列(匹配和消息)。 If you want just execute it standalone (exactly as it is here without the INSERT part). 如果您只想独立执行它(正是此处没有INSERT部分)。

Part two 第二部分

INSERT INTO     Messages (Match, Message)

This part gets fed the result of Part one (beware that this part does not know and does not care about the column names from Part one, it just acts on the order of columns)... which means for every row returned by Part one it does the equivalent of 这部分得到了第一部分的结果(请注意,该部分不知道并且不关心第一部分中的列名,它只是作用于列的顺序)...这意味着对于第一部分返回的每一行它相当于

INSERT INTO Messages (Match, Message) VALUES (MatchFromPartOne, MessageFromPartOne)

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

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