CodeIgniter Forums
Database error #1066 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Database error #1066 (/showthread.php?tid=65170)



Database error #1066 - waptik - 05-10-2016

guys please help me with this error:

A Database Error Occurred
Error Number: 1066
Not unique table/alias: 'ms_sms'
SELECT *, `ms_tags`.`id` as `tag_ids` FROM `ms_sms` LEFT JOIN `ms_sms` ON `ms_sms`.`id` = `ms_tags_meta`.`sms_id` LEFT JOIN `ms_tags_meta` ON `ms_tags_meta`.`tag_id` = `ms_tags`.`id` GROUP BY `id` ORDER BY `id` DESC
Filename: controllers/me/do.php
Line Number: 70
 --------------------------------------------
and here is my code


$this->db->from('sms');
        
        $this->db->select('tags.id as tag_ids', 'tags.name as tag_names');
        $this->db->join('sms', 'sms.id = tags_meta.sms_id', 'left');
        $this->db->join('tags_meta', 'tags_meta.tag_id = tags.id', 'left');
        $this->db->order_by('id', 'DESC');
        $this->db->group_by('id');

    //$sms = $this->db->get();
        $pages =  new Paginator(SETTING_SMS_PER_PAGE, 'page');
    $pages->setTotal(count($this->db->count_all()));
    $data['pageLinks'] = $pages->pageLinks();
        $data['smss'] = $this->db->get()->result();


RE: Database error #1066 - Narf - 05-10-2016

What kind of help do you need? The error message says exactly what the problem is.


RE: Database error #1066 - waptik - 05-10-2016

i don't know how to fix it. I don't have much knowledge in sql queries usage. So any guide on how to fix it?


RE: Database error #1066 - Narf - 05-10-2016

You don't know enough SQL to fix a name conflict, but you're doing JOINs?

That query has more than one problem, it's not just the fatal error you're getting ... I can give you a cop-out answer that effectively hides the error message, but that won't be enough to fetch the correct result set.

In fact, I probably would've done just that, but I see identifiers named "sms" and such ... Dealing with mobile messages is not homework given by a school teacher, nor something to just play with. I urge you to put an effort into learning basic SQL before continuing your work.


RE: Database error #1066 - waptik - 05-10-2016

this is not mobile network stuffs. Its a kinda typo of www.freshsms.in www.spicysms.in which is why i have a table called sms. Right now you are not helping. I am here to seek for solution or an approach of solution to my problem.


RE: Database error #1066 - salain - 05-11-2016

Narf is right you should work on your SQL skills before you carry on.

Your error is very basic you can not use the same table in a JOIN and a FROM clause unless you use an ALIAS.

change your join for

PHP Code:
$this->db->join('tags_meta''sms.id = tags_meta.sms_id''left');
$this->db->join('tags''tags_meta.tag_id = tags.id''left'); 



RE: Database error #1066 - waptik - 05-11-2016

@Narf sorry about my behaviour.
@salain thanks for the help.
I thinks i will need to start learning sql basics.