'How can I count number of record in each table and add them all together?

I have 5 tables. I want to find the number of used cars, new cars, and cpo cars in each of the tables. At the same time, I want to be able to find the total number of new, used, and cpo. I want to be able to store the count in variables so I can use in my php code. I am able to count for each table using the following queries but it seems repetitive.

$sql = "SELECT UN, count(*) as count from tbl_hm_sales_data " . $period . $retail_condition ." GROUP BY UN ORDER BY UN";                
$result = mysqli_query($conn,$sql) or die(mysqli_error());
// Group and Count by condition
while($row = mysqli_fetch_array($result)) { 
    if ($row['UN'] == "Used") {$HM_current_retail_used=$row['count'];}
    if ($row['UN'] == "New") {$HM_current_retail_new=$row['count'];} 
    if ($row['UN'] == "Certified") {$HM_current_retail_cpo=$row['count'];} 
}           

$sql = "SELECT UN, count(*) as count from tbl_sln_sales_data " . $period . $retail_condition ." GROUP BY UN ORDER BY UN";               
$result = mysqli_query($conn,$sql) or die(mysqli_error());
// Group and Count by condition
while($row = mysqli_fetch_array($result)) { 
    if ($row['UN'] == "Used") {$SLN_current_retail_used=$row['count'];}
    if ($row['UN'] == "New") {$SLN_current_retail_new=$row['count'];} 
    if ($row['UN'] == "Certified") {$SLN_current_retail_cpo=$row['count'];} 
}   

$sql = "SELECT UN, count(*) as count from tbl_slhk_sales_data " . $period . $retail_condition ." GROUP BY UN ORDER BY UN";              
$result = mysqli_query($conn,$sql) or die(mysqli_error());
// Group and Count by condition
while($row = mysqli_fetch_array($result)) { 
    if ($row['UN'] == "Used") {$SLHK_current_retail_used=$row['count'];}
    if ($row['UN'] == "New") {$SLHK_current_retail_new=$row['count'];} 
    if ($row['UN'] == "Certified") {$SLHK_current_retail_cpo=$row['count'];} 
}   

$sql = "SELECT UN, count(*) as count from tbl_rwc_sales_data " . $period . $retail_condition ." GROUP BY UN ORDER BY UN";               
$result = mysqli_query($conn,$sql) or die(mysqli_error());
// Group and Count by condition
while($row = mysqli_fetch_array($result)) { 
    if ($row['UN'] == "Used") {$RWC_current_retail_used=$row['count'];}
    if ($row['UN'] == "New") {$RWC_current_retail_new=$row['count'];} 
    if ($row['UN'] == "Certified") {$RWC_current_retail_cpo=$row['count'];} 
}   

$sql = "SELECT UN, count(*) as count from tbl_sj_sales_data " . $period . $retail_condition ." GROUP BY UN ORDER BY UN";                
$result = mysqli_query($conn,$sql) or die(mysqli_error());
// Group and Count by condition
while($row = mysqli_fetch_array($result)) { 
    if ($row['UN'] == "Used") {$SJ_current_retail_used=$row['count'];}
    if ($row['UN'] == "New") {$SJ_current_retail_new=$row['count'];} 
    if ($row['UN'] == "Certified") {$SJ_current_retail_cpo=$row['count'];} 
 }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source