egolive egolive - 11 days ago 5
MySQL Question

How to combine two SQL SELECT statements in one?

I have these two SELECT statements:

SELECT Termin FROM crSpielTermine WHERE Status = 1


and this

SELECT TerminID FROM crSpielTermine WHERE Termin > ".$AlterTermin." ORDER BY Termin LIMIT 1


Right now, the first select supplies me
$AlterTermin
which I'm using in the
second statement.

Can I combine these two statements and if yes, how I'm going to do this?

Answer

Supposing that your first select returns only a single item, you'd have something like that:

SELECT TerminID
FROM crSpielTermine
WHERE Termin > (SELECT Termin FROM crSpielTermine WHERE Status = 1)
ORDER BY Termin
LIMIT 1

Or this just to make sure it returns only one item

SELECT TerminID
FROM crSpielTermine
WHERE Termin > (SELECT Termin FROM crSpielTermine WHERE Status = 1 LIMIT 1)
ORDER BY Termin
LIMIT 1