Mysql - 如果不为null则选择distinct

时间:2023-02-10 04:26:20

I need to give my client the option to publish some posts with same name in different categories. the CMS is closed and I can't add anymore fields... There is a spare field which I can use to identify posts as identical, but I don't won't a situation where to exact posts will load (posts in my case are images).

我需要让我的客户选择在不同的类别中发布一些同名的帖子。 CMS关闭,我不能再添加字段...有一个备用字段,我可以用来识别相同的帖子,但我不会在一个情况下确切的帖子将加载(我的帖子案例是图像)。

So I figured my client can specify identical "field" for all identical posts.

所以我认为我的客户可以为所有相同的帖子指定相同的“字段”。

The problem is that the default for the field is null. if i'll use distinct, only one result will come from all the non-identical posts (all of them by default = null)...

问题是该字段的默认值为null。如果我将使用distinct,只有一个结果将来自所有不相同的帖子(默认情况下全部= null)...

Is there a way to select distinct "field" if not null?

有没有办法选择不同的“字段”,如果不是空?

I tried looking and trial and error with the above but it didn't work for me...

我尝试用以上方法查看和试错,但它对我不起作用......

The query itself is quite simple...

查询本身很简单......

SELECT id,introtext,publish_up,publish_down 
from #__content 
WHERE catid IN(936,937,940,959,972,988,991) AND state = 1 
  AND ( publish_up = "0000-00-00 00:00:00" OR publish_up <= "2011-10-30 12:12:59" )
  AND ( publish_down = "0000-00-00 00:00:00" OR publish_down >= "2011-10-30 12:12:59" ) 

--removed duplicated query

Will appreciate any clue or directions... Thanks! Yanipan

将欣赏任何线索或方向......谢谢! Yanipan

2 个解决方案

#1


1  

Which field is by default NULL? I'll just name it X and you can change it accordingly:

哪个字段默认为NULL?我只是将其命名为X,您可以相应地更改它:

SELECT DISTINCT id,introtext,publish_up,publish_down,IFNULL(X,id) AS distinct_field 
from #__content 
WHERE catid IN(936,937,940,959,972,988,991) AND state = 1 
AND ( publish_up = "0000-00-00 00:00:00" OR publish_up <= "2011-10-30 12:12:59" )
AND ( publish_down = "0000-00-00 00:00:00" OR publish_down >= "2011-10-30 12:12:59" ) 

I hope I got it right!

我希望我做对了!

#2


2  

select introtext from #__content group by introtext where introtext is not null;

#1


1  

Which field is by default NULL? I'll just name it X and you can change it accordingly:

哪个字段默认为NULL?我只是将其命名为X,您可以相应地更改它:

SELECT DISTINCT id,introtext,publish_up,publish_down,IFNULL(X,id) AS distinct_field 
from #__content 
WHERE catid IN(936,937,940,959,972,988,991) AND state = 1 
AND ( publish_up = "0000-00-00 00:00:00" OR publish_up <= "2011-10-30 12:12:59" )
AND ( publish_down = "0000-00-00 00:00:00" OR publish_down >= "2011-10-30 12:12:59" ) 

I hope I got it right!

我希望我做对了!

#2


2  

select introtext from #__content group by introtext where introtext is not null;