从三个表中获取数据但出现 SQL 语法错误

Posted

技术标签:

【中文标题】从三个表中获取数据但出现 SQL 语法错误【英文标题】:Fetching data from three tables but getting error of SQL syntax 【发布时间】:2019-03-24 11:27:02 【问题描述】:

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

$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 语法有错误;检查与您的 mysql 服务器版本相对应的手册,以在第 7 行的 'SELECT first_name, last_name, user_mobile, useralt_mobile from users where id = ' 附近使用正确的语法 选择meetings.*,follow_up.id 作为follow_up_id,follow_up.comment 作为follow_up_comment,follow_up.date 作为follow_up_date,follow_up.time 作为follow_up_time,从用户中选择first_name、last_name、user_mobile、useralt_mobile 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 meeting LEFT JOIN follow_up ON meetings.id = follow_up.meeting_id WHERE follow_up.date BETWEEN '2018-10-01' AND '2018-10-31' AND meetings.assigned_to_id = '1' ORDER BY follow_up.id DESC

你能帮忙吗?

【问题讨论】:

在此处复制/粘贴错误文本,而不是发布图像。 这个SELECT first_name, last_name,...应该做什么? 你应该使用JOIN's 而不是这些子查询。 看起来您将一堆随机选择扔到一个语句中,这完全弄乱了语法。 您应该添加适当的数据样本和预期结果 【参考方案1】:

你需要替换这个:

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

【讨论】:

现在收到此错误Operand should contain 1 column(s) 那是因为您在另一个选择语句中使用了多重选择语句:first_name、last_name、user_mobile、useralt_mobile。您应该 CONCAT() 那些或复制每个选定列的选择。【参考方案2】:

要获取用户信息,您应该使用联接

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

【讨论】:

以上是关于从三个表中获取数据但出现 SQL 语法错误的主要内容,如果未能解决你的问题,请参考以下文章

创建 pl/sql 过程以从不同的表中获取不同的列

Java JDBC 程序在从表中获取特定行时抛出 SQL 语法错误

SQL如何从有条件的2个表中获取数据

SQL - 如何从多个可能的列名中进行选择?

SQL Server:如何从只有 ID 的联结表中获取数据?

使用 SQLKorma 获取语法异常:使用 SQL 执行查询失败