[Active Record] Select AS Backticks problem - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: [Active Record] Select AS Backticks problem (/showthread.php?tid=39248) |
[Active Record] Select AS Backticks problem - El Forum - 03-05-2011 [eluser]Azhari Harahap[/eluser] Hi i'm using active record to make my SQL query works with all database egnine, but i have a problem on SELECT AS with PostgreSQL. For example i have this: Code: $this->db->select('Name as GroupName') In MySQL it'll create: Code: SELECT `Name` as GroupName FROM (`pbk`) In PostgreSQL: Code: SELECT "Name" as GroupName FROM "pbk" But, the result will be different, on MySQL it stay as GroupName, but on PostgreSQL it become groupname (It's because PostgreSQL case-sensitive behavior). The solution is to add backticks (based on driver) to as/alias field name. On MySQL: Code: SELECT `Name` as `GroupName` FROM (`pbk`) On PostgreSQL: Code: SELECT "Name" as "GroupName" FROM "pbk" Any way to do this? It will be nice if there is: Code: $this->db->select_as('Name', 'GroupName'); [Active Record] Select AS Backticks problem - El Forum - 03-08-2011 [eluser]vikascoollives[/eluser] You can use : $this->db->select('name', 'Groupname'); $query = $this->db->get('members'); // Produces: SELECT (name) as GroupnameFROM members in codeigniter [Active Record] Select AS Backticks problem - El Forum - 03-08-2011 [eluser]Azhari Harahap[/eluser] Hi [quote author="vikascoollives" date="1299588504"]You can use : $this->db->select('name', 'Groupname'); $query = $this->db->get('members'); // Produces: SELECT (name) as GroupnameFROM members in codeigniter[/quote] This not work, as tested on 1.7.2. I've check through the user guide and source code, select method only accept two parameter. Code: function select($select = '*', $escape = NULL) The second parameter only accept boolean value (TRUE or FALSE). [Active Record] Select AS Backticks problem - El Forum - 03-08-2011 [eluser]InsiteFX[/eluser] Try using double qoutes and not single qoutes. Active Record automatically escapes your queries. InsiteFX [Active Record] Select AS Backticks problem - El Forum - 03-11-2011 [eluser]Azhari Harahap[/eluser] [quote author="InsiteFX" date="1299603199"]Try using double qoutes and not single qoutes. Active Record automatically escapes your queries. InsiteFX[/quote] Do you mean like this? Code: $this->db->select("Name as GroupName") This works the same as single quotes, i got same result. Code: SELECT `Name` as GroupName FROM (`pbk`) [Active Record] Select AS Backticks problem - El Forum - 03-11-2011 [eluser]Azhari Harahap[/eluser] Finally get it working with little addition to the AR class. Code: // -------------------------------------------------------------------- And then you can do this: Code: $this->db->select_as('Name', 'GroupName'); It will produce: Code: SELECT `Name` as `GroupName` FROM (`pbk`) |