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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Pj Rigor
  • 391
  • 6
  • 17
  • I would expect this to work, though it's not the most efficient way. Can you post a sample of table rows together with a sample of what you would expect the query result to be for those same rows? – Michael Berkowski Nov 24 '15 at 03:15
  • Yes I can, I'll append it to my question. – Pj Rigor Nov 24 '15 at 03:18

2 Answers2

0

This should give you what you need,

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

Your inner select query gives the result 1 for pending and since there is no record where writer_active=1 and pending=1,you get zero rows.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Venkata Krishna
  • 1,768
  • 2
  • 14
  • 21
0

Try This.

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

It's due to your second select command it will only select the minimum value regardless the writer_active value

Dharman
  • 30,962
  • 25
  • 85
  • 135
E.Solicito
  • 101
  • 9
  • This works!! So was the difference because I should have selected the minimum were the writer active was equal to one? – Pj Rigor Nov 24 '15 at 03:32
  • yes, that was absolutely right, there is more detailed explanation to this but i make it much simpler for us to understand each other – E.Solicito Nov 24 '15 at 03:38