在codeigniter上的select查询中选择错误

时间:2014-07-05 21:10:49

标签: php sql codeigniter postgresql

我有问题。这是我的代码。当我在 navicat 无错误中运行此代码以及我想要执行的结果时。

SELECT DISTINCT
    ON (t1.rl3_11_tahun) t1.rl3_11_tahun,
    t1.rl3_11_id,
    t2.rl3_11_terapi
FROM
    formulir_rl3_11 t1
JOIN (
    SELECT
        rl3_11_tahun,
        SUM (rl3_11_terapi) rl3_11_terapi
    FROM
        formulir_rl3_11
    GROUP BY
        rl3_11_tahun
) t2 ON t1.rl3_11_tahun = t2.rl3_11_tahun
WHERE
    t1.rl3_11_tahun = '2014'

但是当我在CodeIgniter中输入此代码时,我遇到了很多错误。这是我在codeigniter中的代码。

> $this->db->select("DISTINCT ON(t1.rl3_11_tahun)
> t1.rl3_11_tahun,t1.rl3_11_id, t2.rl3_11_terapi ",FALSE)
>        ->FROM ('formulir_rl3_11 t1')
>        ->JOIN('(SELECT rl3_11_tahun, SUM(rl3_11_terapi) rl3_11_terapi FROM formulir_rl3_11 GROUP BY rl3_11_tahun ) t2','t1.rl3_11_tahun =
> t2.rl3_11_tahun WHERE ti.rl3_11_tahun=2014');
> $query = $this->db->get();     
> return $query->result();

我在codeigniter

中编写此代码时出错
A Database Error Occurred

Error Number:

ERROR: missing FROM-clause entry for table "ti" LINE 3: ...N "t1"."rl3_11_tahun" = "t2"."rl3_11_tahun" WHERE ti.rl3_11_... ^

SELECT DISTINCT ON(t1.rl3_11_tahun) t1.rl3_11_tahun, t1.rl3_11_id, t2.rl3_11_terapi FROM "formulir_rl3_11" t1 JOIN (SELECT rl3_11_tahun, SUM(rl3_11_terapi) rl3_11_terapi FROM formulir_rl3_11 GROUP BY rl3_11_tahun ) t2 ON "t1"."rl3_11_tahun" = "t2"."rl3_11_tahun" WHERE ti.rl3_11_tahun=2014

Filename: C:\xampp\htdocs\rsaj_report_ci\system\database\DB_driver.php

Line Number: 330

我的代码出了什么问题?如何将该代码从navicat转换为codeigniter?     有谁能够帮我?对不起我的英语不好。     非常感谢你的回答!

问候,拉赫马特!

1 个答案:

答案 0 :(得分:0)

如何使用codeigniter查询功能?不是活动记录类?

$this->db->query('YOUR QUERY HERE');

http://ellislab.com/codeigniter/user-guide/database/queries.html