首页 > 解决方案 > 从三个表中获取数据但出现 SQL 语法错误

问题描述

我正在从三个表中获取数据:

$result = $this->db->query("
    SELECT 
        `meetings`.*,
        `follow_up`.id as follow_up_id,
        `follow_up`.comment as follow_up_comment,
        `follow_up`.date as follow_up_date,
        `follow_up`.time as follow_up_time,
        SELECT first_name, last_name, user_mobile, useralt_mobile from users where id = user_id,
        (SELECT address FROM day_location WHERE `meetings`.assigned_to_id = user_id AND `follow_up`.date = date LIMIT 1) AS location_name
        FROM meetings
        LEFT JOIN follow_up ON `meetings`.id = `follow_up`.`meeting_id`
        WHERE follow_up.`date` BETWEEN '{$fromDate_formated}' AND '{$toDate_formated}'
            " . ($user_id > 0 ? " AND `meetings`.assigned_to_id = '{$user_id}'" : '') . "
    ORDER BY `follow_up`.id DESC
");

错误:

A Database Error Occurred
错误编号:1064

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法,以便在第 7 行的 'SELECT first_name, last_name, user_mobile, useralt_mobile from users where id = ' 附近使用

SELECT meetings.*, follow_up.id as follow_up_id, follow_up.comment as follow_up_comment, follow_up。日期为 follow_up_date,follow_up.time 为 follow_up_time,从 id = user_id 的用户中选择 first_name、last_name、user_mobile、useralt_mobile,(从 day_location 选择地址 WHERE meetings.assigned_to_id = user_id AND follow_up.date = 日期限制 1)作为会议的 location_name 左加入 follow_up meetings.id = follow_up. meeting_id在哪里跟进。date在 '2018-10-01' AND '2018-10-31' AND meetings.assigned_to_id = '1' ORDER BY follow_up.id DESC之间

你能帮忙吗?

标签: phpmysqlsqlcodeigniter

解决方案


你需要替换这个:

SELECT first_name, last_name, user_mobile, useralt_mobile from users where id = user_id,

有了这个:

(SELECT first_name, last_name, user_mobile, useralt_mobile from users where id = user_id),

推荐阅读