使用MySQL和PHP执行查询

时间:2013-06-25 23:46:53

标签: php mysql sql

我该怎么做:

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

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

非常感谢

REAL CASE

$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";

1 个答案:

答案 0 :(得分:2)

用括号括起来:

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

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

MySQl Subquery Documentation

REAL CASE

$ 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";