2012-01-15 308 views
0

我有兩個疑問一個接直另:PHP SQL合併2查詢

$sql_result8 = mysql_query("SELECT * FROM properties WHERE c4='$id'", $db); $charges = mysql_num_rows($sql_result8); 
$sql_result8 = mysql_query("SELECT * FROM vehicles WHERE c4='$id'", $db); $charges = $charges + mysql_num_rows($sql_result8); 

我需要什麼樣的查詢合併這些?某種JOIN?聯盟?

回答

1
SELECT * FROM properties p, vehicles v WHERE p.c4 = v.c4 AND p.c4 = '$id' 
+0

着得到這個工作:o – user1022585 2012-01-15 13:01:07

0

試試這個

SELECT * FROM properties JOIN vehicles USING (c4) WHERE c4='$id' 
0

如果你只想找出行數從兩個查詢返回,而不是顯示實際列,您可以使用此:

$sql_result8 = mysql_query(
    "SELECT 
     (SELECT COUNT(*) FROM properties WHERE c4='$id') 
     + (SELECT COUNT(*) FROM vehicles WHERE c4='$id') 
    ", $db);