[英]SQL Query explanation with IF and INNER JOIN
Could someone please explain this SQL to me, I'm trying to edit code but I don't understand this. 有人可以向我解释一下此SQL,我正在尝试编辑代码,但我听不懂。
This is the whole sql: 这是整个SQL:
SELECT SQL_CACHE COUNT(c.conversation_id) AS num_messages
FROM table1 AS c
INNER JOIN (
SELECT message_id,conversation_id FROM table2
WHERE recipient_id=:userid ORDER BY created DESC
) AS m ON(m.conversation_id=c.conversation_id)
WHERE (c.initiator_id=:userid OR c.interlocutor_id=:userid)
AND (c.bm_read & IF(c.initiator_id=:userid, :bminit, :bminter)) = 0
AND (c.bm_deleted & IF(c.initiator_id=:userid, :bminit, :bminter)) = 0
GROUP BY c.conversation_id
I don't understand this part: 我不明白这部分内容:
INNER JOIN (
SELECT message_id,conversation_id FROM table2
WHERE recipient_id=:userid ORDER BY created DESC
) AS m ON(m.conversation_id=c.conversation_id)
WHERE (c.initiator_id=:userid OR c.interlocutor_id=:userid)
AND (c.bm_read & IF(c.initiator_id=:userid, :bminit, :bminter)) = 0
AND (c.bm_deleted & IF(c.initiator_id=:userid, :bminit, :bminter)) = 0
In case you don't understand INNER JOIN
, here's a good explanation . 如果您不了解
INNER JOIN
, 这是一个很好的解释 。 It's basically an intersection, produced by doing a CROSS JOIN
(ie Cartesian Product if you think of the tables as sets) and then filtering based on a condition specified in an ON
clause. 它基本上是一个交集,它是通过执行
CROSS JOIN
(如果您将表视为集合,即笛卡尔乘积 ),然后根据ON
子句中指定的条件进行过滤而生成的。
The queries are using aliases: c
and m
respectively to make the overall query shorter. 查询使用别名:
c
和m
分别使整体查询更短。 So, c
is the result of: 因此,
c
是以下结果:
SELECT SQL_CACHE COUNT(c.conversation_id) AS num_messages
FROM table1
and m
is the result of: m
是以下结果:
SELECT message_id,conversation_id FROM table2
WHERE recipient_id=:userid ORDER BY created DESC
and the ON
clause is filtering the results of the CROSS JOIN
of the two queries based on whether the conversation_id
column's value from query c
is equal to the conversation_id
column's value from query m
. 和
ON
子句过滤的结果CROSS JOIN
基于所述是否两个查询的conversation_id
从查询列的值c
等于conversation_id
从查询列的值m
。
The WHERE
, AND
and GROUP BY
clauses after the INNER JOIN
are simply part of the SELECT
that are filtering the results after the INNER JOIN
occurs. INNER JOIN
之后的WHERE
, AND
和GROUP BY
子句只是SELECT
一部分,它们在发生INNER JOIN
之后过滤结果。
The IF
is pretty simple too: IF
也很简单:
IF(expr1,expr2,expr3)
If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL) then IF() returns expr2 ; 如果expr1为TRUE(expr1 <> 0且expr1 <> NULL),则IF()返回expr2 ;否则为false 。 otherwise it returns expr3 .
否则返回expr3 。
https://dev.mysql.com/doc/refman/5.1/en/control-flow-functions.html#function_if https://dev.mysql.com/doc/refman/5.1/en/control-flow-functions.html#function_if
Seems like this would do the same thing... 似乎这样会做同样的事情...
SELECT COUNT(c.conversation_id) num_messages
FROM conv c
JOIN TBL_MSG m
ON m.conversation_id = c.conversation_id
WHERE m.recipient_id = :userid
AND :userid IN (c.initiator_id,c.interlocutor_id)
AND c.bm_read & IF(c.initiator_id=:userid, :bminit, :bminter)) = 0 -- I don't understand
AND c.bm_deleted & IF(c.initiator_id=:userid, :bminit, :bminter)) = 0 -- this bit.
GROUP
BY c.conversation_id
...although you won't know which conversation_id attained which count!?! ...虽然您不知道哪个session_id达到了哪个数量!?!
It is doing a subquery (after the keywords INNER JOIN
), its result is like "a table" but it is created "on the fly", and its alias is "m". 它正在执行子查询(在关键字
INNER JOIN
),其结果类似于“表”,但它是“即时”创建的,别名为“ m”。
Once you know how this subquery works, you are doing an inner join of your table "c" with the new table "m" joining by keys m.conversation_id
and c.conversation_id
. 一旦知道了该子查询的工作原理,就可以通过键
m.conversation_id
和c.conversation_id
来对表“ c”与新表“ m”进行m.conversation_id
c.conversation_id
。 So you are joining conversations from different sources. 因此,您正在加入来自不同来源的对话。
The where part is just a filter applied to the output, it depends on the content of the fields. where部分只是应用于输出的过滤器,它取决于字段的内容。
If you have any other question, just leave a comment ;-) 如有其他问题,请发表评论;-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.