0

I have a query like

SELECT FirstColumn
WHERE SecondColumn = ?
AND ThirdColumn = ?

There may be situation that for one ? value is provided, but for another ? there is no value. Like

[0] => 123
[1] => 

So query may be SELECT FirstColumn WHERE SecondColumn = 123 AND ThirdColumn = ''

When experimented, see that for such query mysql returns nothing (no array).

But necessary is to return array with empty value or NULL or zero. Like

[0] => Array
    (
        [FirstColumn] => 0
    )

So trying to find solution.

One solution may be to create row in mysql where FirstColumn, SecondColumn and ThirdColumn is 0. And with php modify data for ? so if empty variable, then data for ? is 0. Then use the same mysql query. Seems this is not very good solution.

Other solution would be query something like this

SELECT FirstColumn
WHERE SecondColumn = IFNULL(?,0) 
AND ThirdColumn = IFNULL(?,0)

Created mysql row with 0, executed query. All works, but does not return array element where SecondColumn or ThirdColumn is 0. Something wrong with my query or may be IFNULL can not be used in such a way. Please, advice what need to correct? Or may be better solution?

Tried with

SELECT FirstColumn
WHERE SecondColumn = 0
AND ThirdColumn = 0`

and get array with 0 value. Possibly IFNULL used incorrectly...

4
  • 2
    I think you're going to need a FROM in there somewhere, so how about showing us your entire code, not just remnants. Commented Aug 25, 2013 at 14:57
  • SELECT CurrencyAbbreviation, TRIM(TRAILING '0' FROM CurrencyRate/Units) AS FinalCurrencyRate FROM 2013Currencies WHERE (DateOfCurrencyRate = IFNULL(?,0) AND CurrencyAbbreviation = IFNULL(?,0)) OR (DateOfCurrencyRate = IFNULL(?,0) AND CurrencyAbbreviation = IFNULL(?,0)) Commented Aug 25, 2013 at 14:58
  • ....and now some schema would be nice. Commented Aug 25, 2013 at 15:00
  • Do you mean what mysql table looks like? Yes, I am using php Commented Aug 25, 2013 at 15:01

1 Answer 1

1

This query will always return exactly one row:

SELECT * FROM (
  SELECT FirstColumn
  FROM mytable
  WHERE SecondColumn = ?
  AND ThirdColumn = ?
  UNION ALL
  SELECT NULL -- code your "not found" value here
) x
LIMIT 1

UNION ALL preserves the order rows are selected, so if the first query (your original one) returns a row, that is the one returned, otherwise the row created by the solitary select is returned.

You can choose any value for the "default" value as long as it is compatible with the data type of FirstColumn.

Sign up to request clarification or add additional context in comments.

2 Comments

Experimenting with query. If use WHERE and OR OR etc. then UNION ALL must use for each OR? And LIMIT 1 must use only once (at the end)?
Yes. UNION ALL may be used to merge the results of many queries, so you could code your query using a variety of comparisons and still have a final "default". And yes, the ZLIT 1 is applied to the result of the unioned rows (which I aliased as "x")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.