简体   繁体   English

在SQL查询中使用Max(date)聚合函数时,将返回所有列中都包含Null值的行

[英]Rows containing Null values in all columns are returned when Max(date) aggregate function is used in sql query

I am using Mysql database and having a problem with inner join. 我正在使用Mysql数据库,并且内部联接有问题。 I am using aggregate function max(dn.on_date) in the query to select only recent date rows. 我在查询中使用聚合函数max(dn.on_date)仅选择最近的日期行。 this is working fine when rows are found with matching criteria but when where clause condition becomes false it 1 row is returned containing NULL value for all columns. 当找到具有匹配条件的行时,这很好用,但是当where子句条件变为false时,将返回1行,其中所有列都为NULL。 I want to ignore that row to be returned as result. 我想忽略该行作为结果返回。

Table structures are as follows : view table structure and relation 表结构如下: 查看表的结构和关系

my query is like : 我的查询是这样的

SELECT d.first_name, d.last_name, d.mobile, dn.don_amount as amount, max(dn.on_date) as on_date 选择d.first_name,d.last_name,d.mobile,dn.don_amount作为金额,max(dn.on_date)作为on_date
FROM donors d 来自捐助者d
inner join donation dn on d.mobile = dn.don_id d.mobile上的内部加入捐赠dn = dn.don_id
WHERE LOWER(first_name) LIKE LOWER('dinesh%') order by d.mobile asc LIMIT 0,4 由d.mobile asc LIMIT在哪儿降低(first_name)降低('dinesh%')排序

Unwanted Results is returned like this : view results 不需要的结果将像这样返回查看结果
I want that it should not return anything when where condition is not matched 我希望当条件不匹配时,它不应该返回任何内容

I have tried like this but it doesn't work and result is same as previous 我已经尝试过了,但是没有用,结果和以前一样

SELECT d.first_name, d.last_name, d.mobile, dn.don_amount as amount, max(dn.on_date) as on_date 选择d.first_name,d.last_name,d.mobile,dn.don_amount作为金额,max(dn.on_date)作为on_date
FROM donors d 来自捐助者d
inner join donation dn on d.mobile = dn.don_id d.mobile上的内部加入捐赠dn = dn.don_id
WHERE d.mobile IS NOT NULL AND LOWER(first_name) LIKE LOWER('dinesh%') order by d.mobile asc LIMIT 0,4 d.mobile不为空且d.mobile asc限制为LOWER('dinesh%')的LOWER(first_name)顺序0,4

you used aggregate function but not seen group by in your query 您使用了汇总功能,但在查询中未看到分组依据

SELECT d.first_name, d.last_name, d.mobile, dn.don_amount as amount, max(dn.on_date) as on_date 
FROM donors d 
inner join donation dn on d.mobile = dn.don_id 
WHERE LOWER(first_name) LIKE 'dinesh%'
group by d.first_name, d.last_name, d.mobile, dn.don_amount
order by d.mobile asc 
LIMIT 0,4

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

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