-3

我该怎么做这样的事情:

$query = "SELECT a,b FROM c ORDER BY a";

$query1 = "SELECT a,b FROM '".$query."' WHERE a='".$number."'";

非常感谢

真实案例

$query2 = "SELECT numero,spartenza,sarrivo,opartenza,oarrivo FROM treni ORDER BY opartenza";

$query1 = "SELECT spartenza,sarrivo,opartenza,oarrivo,TIMEDIFF(oarrivo,opartenza) FROM (".$query2.") AS 'ordinata' WHERE numero = '".$id_treno."' ORDER BY opartenza";
4

1 回答 1

2

用括号括起来:

$query1 = "SELECT a,b FROM (".$query.") AS `alias` WHERE a='".$number."'";

像这样的子查询需要别名。

MySQl 子查询文档

真实案例

$query = "SELECT spartenza,sarrivo,opartenza,oarrivo,TIMEDIFF(oarrivo,opartenza) FROM treni WHERE numero = '".$id_treno."' ORDER BY opartenza";

为此,您根本不需要子查询。您可以ORDER BY选择未选择的列。一个建议是TIMEDIFF像这样为您的函数起别名,以便更容易检索。

$query = "SELECT spartenza,sarrivo,opartenza,oarrivo,TIMEDIFF(oarrivo,opartenza) AS `timediff_alias` FROM treni WHERE numero = '".$id_treno."' ORDER BY opartenza";
于 2013-06-25T23:51:52.830 回答