Home > database >  Multiple Table Search and show the result
Multiple Table Search and show the result

Time:11-03

I was trying to fetch data from multiple tables in the Database and to Fetch data I tried using UNION ALL ... but I wasn't able to work on it.

$sqll = "SELECT * FROM msr_bills WHERE mobile='94825XXXX' UNION ALL 
SELECT * FROM hirisave_bills WHERE mobile='94825XXXX'";

$sql = mysqli_query($conn,$sqll);
while($row = mysqli_fetch_array($sql)){
    echo $row['name'];
    echo $row['mobile'];
}

I am getting this error:

The used SELECT statements have a different number of columns

CodePudding user response:

Maybe try to use LEFT JOIN like this:

$sqll = "SELECT * FROM `msr_bills` mb LEFT JOIN `hirisave_bills` hb ON hb.`mobile` = mb.`mobile` WHERE mb.`mobile` = '94825XXXX'";

You have all columns from two tables or nulls from hirisave_bills if there is no such mobile number.

CodePudding user response:

you have diferent number of columns in your tables. try to select same number of columns

For example:

$sqll = "SELECT id, mobile FROM msr_bills WHERE mobile='94825XXXX' UNION ALL SELECT id, mobile FROM hirisave_bills WHERE mobile='94825XXXX'";
  • Related