gpt4 book ai didi

mysql - 内连接查询 - 3 个表

转载 作者:行者123 更新时间:2023-11-29 12:33:00 26 4
gpt4 key购买 nike

我正在尝试执行此查询以打印三个表中的数据。但不知何故,查询没有被执行。

SELECT l.locality_id, c.city_name, l.locality_name, cloud_site_location_outlet_localities.minimum_order, cloud_site_location_outlet_localities.delivery_time, cloud_site_location_outlet_localities.delivery_charge FROM `cloud_site_location_localities` l INNER JOIN `cloud_site_location_cities` c on l.city_id=c.city_id

执行上述查询时遇到的错误是:

1054 - Unknown column 'cloud_site_location_outlet_localities.minimum_order' in 'field list'

但是,当我必须仅显示两个表中的数据时,此查询运行良好。

SELECT l.locality_id, c.city_name, l.locality_name FROM `cloud_site_location_localities` l INNER JOIN `cloud_site_location_cities` c on l.city_id=c.city_id

我哪里出错了?

当我使用以下查询将其导出为 CSV 时,我得到了冗余数据。在 1000 个条目中,我只得到了 400 个,然后这 400 个被重复。

这是我正在实现的代码

$sql = "SELECT l.locality_id, c.city_name, l.locality_name, o.minimum_order, o.delivery_time, o.delivery_charge FROM `cloud_site_location_localities` l INNER JOIN `cloud_site_location_cities` c ON l.city_id = c.city_id INNER JOIN `cloud_site_location_outlet_localities` o ON l.locality_id = o.locality_id";
$result = $DB->query($sql);


//$output = fopen($filename, 'c+');
$output = fopen('php://output', 'c+');
fputcsv($output, array('Location Id', 'City Name', 'Location Name','Minimum Order Amount','Delivery Time','Delivery Charge'));
$row = array();
$csv_output = "";
$csv_output .= "Locality Id,City Name,Locality Name,Minimum Order Amount,Delivery Time,Delivery Charge\n";
foreach($result as $locality_value)
{
$row['locality_id'] = $locality_value->locality_id;
$row['city_name'] = $locality_value->city_name;
$row['locality_name'] = $locality_value->locality_name;
$row['minimum_order'] = $locality_value->minimum_order;
$row['delivery_time'] = $locality_value->delivery_time;
$row['delivery_charge'] = $locality_value->delivery_charge;
$csv_output .= trim($row['locality_id']).",".trim($row['city_name']).",".trim($row['locality_name']).",".trim($row['minimum_order']).",".trim($row['delivery_time']).",".trim($row['delivery_charge']).",,\n";
fputcsv($output, $row);

}
fclose($output);
print $csv_output;

最佳答案

您缺少 cloud_site_location_outlet_localitiesjoin 子句。例如:

SELECT      l.locality_id, 
c.city_name,
l.locality_name,
o.minimum_order,
o.delivery_time,
o.delivery_charge
FROM `cloud_site_location_localities` l
INNER JOIN `cloud_site_location_cities` c
ON l.city_id = c.city_id
INNER JOIN `cloud_site_location_outlet_localities` o
ON l.outlet_id = c.outlet_id --just guessing the column names

关于mysql - 内连接查询 - 3 个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27202071/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com