简体   繁体   English

当根本没有行时,为什么group_concat带一行具有空值

[英]Why does the group_concat bring one row with null values when there are no rows at all

I have a table post as p, table tags as pt and table tagsref as ptr. 我有一个表帖子作为p,表标签作为pt,表标签ref作为ptr。

tagsref will have the id's of post and tags table. tagsref将具有post和tags表的ID。

i used group_concat and i did not use group by clause because in the condition i am using an if condition to filter only one row (p.slug = 'once') assuming that since i had added the condition only one row will return. 我使用了group_concat,并且我没有使用group by子句,因为在这种情况下,我假设条件是我添加了条件,因此仅返回一行,所以我使用if条件仅过滤一行(p.slug ='once')。 yes it did. 是的,它确实。

The problem is when there are no rows still i got a row with null values. 问题是当仍然没有行时,我得到了一个具有空值的行。 in the following sql the last condition will not fetch any result because there are no rows with slug as 'none' so i should not get any result at all but i got one row with null values. 在以下sql中,最后一个条件将不会获取任何结果,因为没有没有slug的行为“ none”,所以我根本不会得到任何结果,但是我得到的行中包含空值。

SELECT p.name, p.slug, GROUP_CONCAT(pt.name) as catz 
FROM post_tags pt, post_tagsref ptr, post p  
WHERE p.id = ptr.tid AND ptr.cid = pt.id  AND p.slug = 'none'

People suggested to use group by clause and mentioned that group_by aggregate functions will work even the group by clause is not specified but the result may be unpredictable. 人们建议使用group by子句,并提到即使未指定group by子句,group_by聚合函数也将起作用,但结果可能无法预测。

i used group by and is working. 我使用分组依据,并且正在工作。 yet. 然而。

post
-----
id  |   name    |   slug
1   |   sam     |   sam
2   |   do do   |   do-do

post_tags
--------------
id  |   name    |   slug
1   |   music   |   music
2   |   movies  |   movies
3   |   tv      |   tv

post_tagsref
------------    
pid |   pcid
1   |   2
1   |   1
2   |   1

The above is the table structure. 以上是表格结构。

If there are no rows at all then why is group_concat returning one row with null values? 如果根本没有行,那为什么group_concat返回一个空值的行?

The reason for a question like this is to make sure what is what... :) 这样的问题的原因是要确保什么是... :)

Reference: 参考:

Query is resulting in 1 row with null values when id is not found 当没有找到ID时,查询结果将在1行中包含空值

Aggregate functions with no input tend to produce output nonetheless: the sum of no rows is zero, the count is zero as well, and the average is NULL . 没有输入的聚合函数仍然会产生输出:没有行的总和为零,计数也为零,平均值为NULL This kind of thing makes a lot of queries consistent, and should be the cause why you get NULL . 这种事情使许多查询保持一致,并且应该是导致获得NULL的原因。

i did not use group by clause because in the condition i am using an if condition to filter only one row […] 我没有使用group by子句,因为在这种情况下,我正在使用if条件来仅过滤一行[…]

You filter to only one post , but I assume there might be multiple tags for each post (else why would you use group_concat ), so the join will result in more than one row. 您只过滤了一个帖子 ,但是我假设每个帖子可能有多个标签(否则您将为什么使用group_concat ),因此group_concat将导致多于一行。 Therefore you'll probably want to group by the post id. 因此,您可能需要按帖子ID进行分组。

Also note that as you are doing inner joins at the moment, a post with no tag at all won't be reported, so consider using left joins there. 另请注意,由于您目前正在执行内部联接,因此根本不会报告没有标签的帖子,因此请考虑在此处使用左联接。

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

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