SQL 空值内连接
我
SELECT * FROM Y
INNER JOIN X ON ISNULL(X.QID, 0) = ISNULL(y.QID, 0)
在像这样的连接中有一个连接 Isnull
使它变慢。这就像有一个条件连接。 对于这样的事情有什么解决办法吗? 我有很多 QID
为 Null 的记录
任何人都可以找到不需要修改数据的解决方法
I have a Join
SELECT * FROM Y
INNER JOIN X ON ISNULL(X.QID, 0) = ISNULL(y.QID, 0)
Isnull
in a Join like this makes it slow. It's like having a conditional Join.
Is there any work around to something like this?
I have a lot of records where QID
is Null
Anyone have a work around that doesn't entail modifying the data
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您有两个选择
或更简单的选择
You have two options
or easier
如果您希望从 Y.QID 中包含空值,那么最快的方法是
SELECT * FROM Y
左连接 X ON y.QID = X.QID
注意:仅当您需要左表中的空值(即 Y)(在上述情况下)时,此解决方案才适用。
否则
<代码>
x.qid 上的内连接 x 与 y.qid 不同
是正确的做法
If you want null values to be included from Y.QID then Fastest way is
SELECT * FROM Y
LEFT JOIN X ON y.QID = X.QID
Note: this solution is applicable only if you need null values from Left table i.e. Y (in above case).
Otherwise
INNER JOIN x ON x.qid IS NOT DISTINCT FROM y.qid
is right way to do
这篇文章对这个问题进行了很好的讨论。你可以使用
This article has a good discussion on this issue. You can use
您是否致力于使用内连接语法?
如果没有,您可以使用以下替代语法:
Are you committed to using the Inner join syntax?
If not you could use this alternative syntax:
我很确定连接甚至没有达到您想要的效果。如果表 a 中有 100 条记录的 qid 为空,表 b 中有 100 条记录的 qid 为空,则所编写的联接应进行交叉联接,并为这些记录提供 10,000 个结果。如果您查看以下代码并运行示例,我认为最后一个可能更符合您想要的结果集:
I'm pretty sure that the join doesn't even do what you want. If there are 100 records in table a with a null qid and 100 records in table b with a null qid, then the join as written should make a cross join and give 10,000 results for those records. If you look at the following code and run the examples, I think that the last one is probably more the result set you intended:
嘿,回答这个问题有点晚了,但我也遇到了同样的问题,我意识到你的第二个表中必须有一条 ID 为
0
的记录才能做到这一点:要发生,它实际上是说
如果没有,则使用0
。 但是如果Y
表没有有 ID 为0
的记录怎么办?所以,我找到了这个方法(并且适用于我的案例):
我的案例的快照
这样,您不需要在第二个表中需要 ID 值为
0
的记录(在本例中为Y
,而Customers
在我的例子中),或者任何记录更新:
您还可以查看这篇文章以便更好地理解。
Hey it is kind of late to answer that but I got the same question, what I realized is that you must have a record with the ID of
0
in you second table to make this :to happen, it actually says
if there is none, then use 0
. BUT what ifY
table does NOT have a record with the ID of0
?So, I found this method, (and worked for my case):
A snapshot of my case
This way you DON'T need a record with
0
ID value in your second table (which isY
in this case andCustomers
in my case), OR any record at allUPDATE:
You can also take a look at this post for better understanding.
基本上,您想要将两个表连接在一起,其中它们的 QID 列都不为空,对吗?但是,您没有强制执行任何其他条件,例如两个 QID 值(这对我来说似乎很奇怪,但没关系)。像下面这样简单的东西(在 MySQL 中测试)似乎可以满足您的要求:
这使您可以将 Y 中的每个非空行连接到 X 中的每个非空行。
更新: Rico 说他还想要具有 NULL 值的行,为什么不只是:
Basically you want to join two tables together where their QID columns are both not null, correct? However, you aren't enforcing any other conditions, such as that the two QID values (which seems strange to me, but ok). Something as simple as the following (tested in MySQL) seems to do what you want:
This gives you every non-null row in Y joined to every non-null row in X.
Update: Rico says he also wants the rows with NULL values, why not just:
您还可以使用 coalesce 函数。我在 PostgreSQL 中对此进行了测试,但它也应该适用于 < a href="http://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_coalesce" rel="nofollow">MySQL 或 MS SQL 服务器。
在评估之前,这会将
NULL
替换为-1
。因此,qid
中一定不能有-1
。You could also use the coalesce function. I tested this in PostgreSQL, but it should also work for MySQL or MS SQL server.
This will replace
NULL
with-1
before evaluating it. Hence there must be no-1
inqid
.