简体   繁体   中英

MYSQL, PHP) get values from all columns from multiple tables using UNION ALL

I need to loop two different tables and get values from all columns.

I tried use UNION ALL to solve my problem.

Code looks like this:

if(isset($_GET['country'])){
        $sql = "SELECT name, capital, population,description,null FROM countries WHERE name ='France'

        UNION ALL
             SELECT null,null,null,null,attraction_name FROM attraction_list WHERE country_name='France'
        ";
        mysqli_select_db($conn,'travelapp') or die("database not found");
        $result = mysqli_query($conn,$sql);
        if($result){
            $countryName;$capital;$population;$description;$attraction;
            while($row = $result->fetch_row()){
                $countryName=$row[0];
                $capital=$row[1];
                $population=$row[2];
                $description=$row[3];
                $attraction=$row[4];
            }
            $resultArray = array(
                'country'=>$countryName,
                'capital'=>$capital,
                'population'=>$population,
                'description'=>$description,
                'attraction'=>$attraction
            );
            echo json_encode($resultArray);
        }
    }

However, this code doesn't work as I expected. I expected to print out

name, capital, population,description from countries table and attraction_name from attraction_list table.

So the output which I expected should look like this:

{"country":France, "capital":Paris, "population": 64750000, "description":Some text, "attraction":La Vieille Charite}

but what does my php print out now: 在此处输入图片说明

Is it issue with while loop? or maybe there are much better solution than UNION ALL?

What you actually want is a LEFT JOIN . Use a LEFT JOIN in case there are countries with no attractions. Try this:

$sql = "SELECT c.name, c.capital, c.population, c.description, a.attraction_name 
        FROM countries c
        LEFT JOIN attraction_list a ON a.country_name = c.name
        WHERE c.name ='France'";

This query will give you multiple rows if there are multiple attractions in a country eg

{"country":France, "capital":Paris, "population": 64750000, "description":Some text, "attraction":La Vieille Charite},
{"country":France, "capital":Paris, "population": 64750000, "description":Some text, "attraction":Eiffel Tower}

If you would like only one row eg

{"country":France, "capital":Paris, "population": 64750000, "description":Some text, "attraction":La Vieille Charite, Eiffel Tower}

you could use GROUP BY to aggregate eg

$sql = "SELECT c.name, c.capital, c.population, c.description, GROUP_CONCAT(a.attraction_name)
        FROM countries c
        LEFT JOIN attraction_list a ON a.country_name = c.name
        WHERE c.name ='France'
        GROUP BY c.name";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM