php sql合并2个查询

时间:2012-01-15 02:32:52

标签: sql join union

我一个接一个地查询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);

我需要将哪些查询合并这些?某种加入?联盟?

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

试试这个

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

答案 2 :(得分:0)

如果您只想找出两个查询返回的数字或行,而不显示实际列,您可以使用:

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