如何将两个SQL SELECT语句合二为一?

时间:2016-11-28 10:18:07

标签: php mysql

我有两个SELECT语句:

SELECT Termin FROM crSpielTermine WHERE Status = 1

和这个

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

现在,第一个选择为我$AlterTermin提供了我正在使用的 function tick() { path.attr("d", function(d) { var dx = d.target.x - d.source.x, dy = d.target.y - d.source.y, dr = Math.sqrt(dx * dx + dy * dy); return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y; }); 第二个陈述。

我可以将这两个陈述合并,如果是,我将如何做到这一点?

2 个答案:

答案 0 :(得分:3)

假设您的第一个选择仅返回一个项目,您就会有类似的内容:

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

或者这只是为了确保它只返回一个项目

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

答案 1 :(得分:2)

您可以使用如下的子查询,这将得到相同的结果:

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