php—从三个表中获取数据,但sql语法错误

0h4hbjxa  于 2021-06-19  发布在  Mysql
关注(0)|答案(2)|浏览(322)

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

$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
");

错误:
发生数据库错误
错误号:1064
sql语法有错误;在第7行的“select first\u name,last\u name,user\u mobile,useralt\u mobile from users where id=”附近,检查与您的mysql服务器版本对应的手册,以获得正确的语法
选择 meetings .*, follow_up .id作为后续id, follow_up .注解作为后续注解, follow_up .日期作为跟进日期, follow_up .time作为follow\u up\u time,选择first\u name、last\u name、user\u mobile、useralt\u mobile from users where id=user\u id,(选择address from day\u location where meetings .assigned_to_id=用户\u id和 follow_up .date=日期限制1)作为地点\u名称从会议左侧加入后续\u meetings .id= follow_up . meeting_id 你在哪里跟进。 date 在“2018-10-01”和“2018-10-31”之间,以及 meetings .assigned \u to \u id='1'订购者 follow_up .id描述
你能帮忙吗?

vxf3dgd4

vxf3dgd41#

您需要替换此:

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),
8yoxcaq7

8yoxcaq72#

要获取用户信息,应该使用join

$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,
            users.first_name, 
            users.last_name, 
            users.user_mobile,
            users. useralt_mobile, 
            (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`
        LEFT JOIN users on users.id = `meetings`.assigned_to_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");

相关问题