php - GROUP_BY 中的错误,查询来自两个数据库 CODEIGNITER

标签 php mysql codeigniter activerecord

我有一个连接来自两个不同数据库的表的查询。它已经在显示结果,但后来我只想显示独特的结果,因为有些结果是多余的。因此,我添加了一个 GROUP BY 以仅获取唯一结果,但出现错误。

这是我的代码:

public function search_results_accommodations($location,$from_date,$to_date,$bedroom,$guests)
{
    $this->db->select('*, akzapier.bookings.id as BOOKING_ID, akzapier.properties.id as PROPERTY_ID, ci_alexandrohomes.assigned_property.ID as ASSIGNED_PROPERTY_ID, ci_alexandrohomes.listings.ID as LISTING_ID');
    $this->db->from('akzapier.bookings');
    $this->db->join('akzapier.properties', 'akzapier.properties.id=akzapier.bookings.property_id', 'inner');
    $this->db->join('ci_alexandrohomes.assigned_property', 'ci_alexandrohomes.assigned_property.property_id=akzapier.properties.id', 'inner');
    $this->db->join('ci_alexandrohomes.listings', 'ci_alexandrohomes.listings.ID=ci_alexandrohomes.assigned_property.listing_id');
    $this->db->where('akzapier.bookings.check_in !=', $from_date);
    $this->db->where('akzapier.bookings.check_out !=', $to_date);
    $this->db->where('ci_alexandrohomes.listings.city', $location);
    $this->db->where('ci_alexandrohomes.listings.bedrooms', $bedroom);
    $this->db->where('ci_alexandrohomes.listings.guests', $guests);
    $this->db->group_by('akzapier.properties.id', 'ASC')
    $query = $this->db->get();
    return $query->result();
}

页面中没有显示错误,所以我将其转换为 SQL 以查看实际情况:

SELECT * akzapier.bookings.id as BOOKING_ID, akzapier.properties.id as PROPERTY_ID, ci_alexandrohomes.assigned_property.ID as ASSIGNED_PROPERTY_ID, ci_alexandrohomes.listings.ID as LISTING_ID
FROM akzapier.bookings 
INNER JOIN akzapier.properties ON akzapier.properties.id=akzapier.bookings.property_id
INNER JOIN ci_alexandrohomes.assigned_property ON ci_alexandrohomes.assigned_property.property_id=akzapier.properties.id
INNER JOIN ci_alexandrohomes.listings ON ci_alexandrohomes.listings.ID=ci_alexandrohomes.assigned_property.listing_id
WHERE akzapier.bookings.check_in != '2019-09-21'
AND akzapier.bookings.check_out != '2019-09-30'
AND ci_alexandrohomes.listings.city = ‘1’
AND ci_alexandrohomes.listings.bedrooms = '2'
AND ci_alexandrohomes.listings.guests = '4'
GROUP BY akzapier.bookings.property_id ASC

错误提示:

1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'akzapier.bookings.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

最佳答案

通常,当您使用 GROUP BY 时,您的 SELECT 语句必须包含聚合(例如 MAX(...)、COUNT(...) 等)或者列必须出现在 GROUP BY 中。您已选择所有带星号 * 的非聚合字段。在这种情况下,它提示字段 akzapier.bookings.id,它既没有聚合,也不在您的 GROUP BY 中。

如果您确实需要唯一值,请尝试使用 SELECT DISTINCT,这将从结果中删除重复的行。

关于php - GROUP_BY 中的错误,查询来自两个数据库 CODEIGNITER,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58480926/

相关文章:

php - symfony2 空 jsonResponse

php - 检查用户是否在线。ajax 请求太多

JavaScript/AJAX 在应该显示图片时显示 "undefined"

php - $_POST[] - 不同的提交按钮不是恒定的

mysql - SequelizeEagerLoadingError : associated not working in sequelize

php - jqGrid 集成工具栏搜索不起作用

php - 使用Codeigniter尝试从数据库显示图像时出现错误消息

mysql - 如何在同一查询中的一个表中获取不同类型表中的行数

codeigniter - Codeigniter 中的 order by 子句

php - .htaccess 文件在 LAMP 中不起作用