从MySQL的test2和test3表中查询和选择数据?

时间:2013-08-10 23:19:45

标签: php mysql sql database

我希望从test2test3表中获取数据以显示在网页上,但我一直看到一个空白屏幕。

$query = "INSERT INTO test2,test3 VALUES(' ','$title','$stdate','$endate',' ',' ',' ',' ',' ','$fileName','$fileSize','$fileType',' ','$desc',' ','$postuser')";

} } }
else {   
$query = "INSERT INTO test2,test3 VALUES(' ','$title','$stdate','$endate',' ',' ',' ',' ',' ','$fileName','$fileSize','$fileType',' ','$desc',' ','$postuser')";

}    
mysql_query($query) or die('Error, query failed.         '. mysql_error());

}   

$query4=" SELECT * FROM test2,test3";

3 个答案:

答案 0 :(得分:1)

你的方法本质上是错误的,但不过,这里有更正的代码:

$query = "INSERT INTO test2 VALUES(' ','$title','$stdate','$endate',' ',' ',' ',' ',' ','$fileName','$fileSize','$fileType',' ','$desc',' ','$postuser')";

} } }
else {   
$query = "INSERT INTO test3 VALUES(' ','$title','$stdate','$endate',' ',' ',' ',' ',' ','$fileName','$fileSize','$fileType',' ','$desc',' ','$postuser')";

}    
mysql_query($query) or die('Error, query failed.         '. mysql_error());

}   

$query4="SELECT * FROM test2 UNION ALL SELECT * FROM test3";

答案 1 :(得分:0)

这个怎么样

$dh = mysql_connect('localhost','root','') or die(mysql_error());

$db1 = mysql_select_db('test2',$dh);
mysql_query("SELECT * FROM tableFromTest2",$db1);

$db2 = mysql_select_db('test3',$dh);
mysql_query("SELECT * FROM tableFromTest3",$db2);

mysql_close($dh);

mysql_connect('localhost','root','') or die(mysql_error());

mysql_query("USE test2");
mysql_query("SELECT * FROM tableFromTest2");

mysql_query("USE test3");
mysql_query("SELECT * FROM tableFromTest3");

mysql_close($dh);

答案 2 :(得分:0)

看起来你想要一个简单的联盟:

SELECT * FROM test2
UNION
SELECT * FROM test3

假设test2和test3的表结构相似(就像它们在您的示例代码中一样)。

当然,因为你要插入两次(每次进入一次),你可能想要两次回来 - 所以使用UNION ALL。您还可以使用ORDER BY和DISTINCT优化​​。