我不想写两个不同的查询,而是想在一个
中完成$query = $pdo->prepare("
SELECT Car_Name
FROM Car_data
WHERE Car_Code = :carCode
SELECT Fruit_Name
FROM Fruit_Data
WHERE Fruit_Code = :fruitCode;
");
$query->bindParam(':carCode', $header['Car_Code']);
$query->bindParam(':fruitCode', $header['Fruit_Code']);
$query->execute();
$result = $query->fetchAll(PDO::FETCH_ASSOC);
if (!empty($result)) {
}
我希望以下列格式接收数据:
如果两个表都有数据:
Car_Name = 'Golf'
Fruit_Name = 'Banana'
如果只有一个表有数据:
Car_Name = 'Golf'
Fruit_Name = ''
OR
Car_Name = 'Golf'
我怎样才能实现?
答案 0 :(得分:0)
$query = $pdo->prepare("
SELECT (SELECT Car_Name
FROM Car_data
WHERE Car_Code = :carCode ) as Car_Name ,
(SELECT Fruit_Name
FROM Fruit_Data
WHERE Fruit_Code = :fruitCode) as Fruit_Name;
");
使用子查询,这应该可行