1

I have an SQL query that selects a minimum value from a column and requires another value to be equal to 1. I am using a nested select and attempting to use an AND statement. However, after adding the AND, the query results with zero results.

SELECT writer_email FROM writerdata_tb WHERE writer_active = 1 AND pending = (SELECT MIN(pending) FROM writerdata_tb) LIMIT 1

How should I fix this code to select WHERE that satisfies both?

writer_email  |  writer_active | pending
-------------   ---------------  --------
email1@mail            0            1
email2@mail            1            3
email3@mail            1            2

The query should result in email3@mail because the record has the least pending and has an active equal to 1.

4

2 回答 2

0

尝试这个。

SELECT writer_email FROM writerdata_tb WHERE writer_active = 1 AND pending = (SELECT MIN(pending) FROM writerdata_tb WHERE writer_active = 1) LIMIT 1

这是由于您的第二个选择命令,它只会选择最小值,而不管 writer_active 值

于 2015-11-24T03:29:07.883 回答
0

这应该给你你所需要的,

SELECT writer_email FROM writerdata_tb WHERE writer_active=1 AND pending = (SELECT MIN(pending) FROM writerdata_tb WHERE writer_active=1) LIMIT 1

您的内部选择查询给出了1待处理的结果,并且由于没有记录 wherewriter_active=1pending=1,因此您得到零行。

于 2015-11-24T03:26:27.627 回答