简体   繁体   English

使用的 SELECT 语句在 codeigniter 中具有不同的列数

[英]The used SELECT statements have a different number of columns in codeigniter

Error Number: 1222错误编号:1222
The used SELECT statements have a different number of columns使用的 SELECT 语句具有不同的列数

This is controller这是控制器

    <?php
    class Autocomplete extends CI_Controller{
        function __construct() {
            parent::__construct();
            $this->load->model('datacomplete');
        }

        public function index(){
            $this->load->view('view_demo');
        }
        public function GetCountryName(){
            $keyword=$this->input->post('keyword');
            $data=$this->datacomplete->GetRow($keyword); 

            echo json_encode($data);
        }

    }
    ?>

This is model这是模型

        <?php
    class Datacomplete extends CI_Model{

        public function GetRow($keyword) {        
           $this->db->select('collg_name,city,state,country as type');
           $this->db->from('tbl_college');
           $this->db->like("collg_name",$keyword);
           $this->db->or_like('city',$keyword,'after');
           $this->db->or_like('state',$keyword,'after');
           $this->db->or_like('country',$keyword,'after');
           $query1 = $this->db->get_compiled_select();
           $this->db->select('course_offrd_name,category_name,subcategory_name');
           $this->db->from('tbl_course_offered');
           $this->db->like("course_offrd_name",$keyword);
           $this->db->or_like('category_name',$keyword,'after');
           $this->db->or_like('subcategory_name',$keyword,'after');
           $query2 = $this->db->get_compiled_select();
           $result = $this->db->query($query1." UNION ".$query2);
           return $result->result();
        }
    }

This is view这是视图

    <!DOCTYPE html>
    <html>
        <head>
            <!-- Latest compiled and minified CSS -->
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css">
            <!-- Latest compiled and minified JavaScript -->
            <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
            <!-- Latest compiled and minified JavaScript -->
            <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
            <script src="<?php echo base_url(); ?>assets/custom.js"></script>
        </head>
        <body style="background-color: #000000;">
            <div class="row">
            <center><h2 style="color: #fff;">AUTOCOMPLETE FORM FROM DATABASE USING CODEIGNITER AND AJAX</h2></center>
                <div class="col-md-4 col-md-offset-4" style="margin-top: 200px;">

                        <label class="control-lable" style="color: #fff;">Country Name</label>
                        <input style="height:70px" type="text" id="country" autocomplete="off" name="country" class="form-control" placeholder="Type to get an Ajax call of Countries">        
                        <ul class="dropdown-menu txtcountry" style="margin-left:15px;margin-right:0px;" role="menu" aria-labelledby="dropdownMenu"  id="DropdownCountry"></ul>
    </div>
            </div>
        </body>
    </html>

    This is custom.js

    $(document).ready(function () {
        $("#country").keyup(function () {
            $.ajax({
                type: "POST",
                url: "http://localhost/codeajax/autocomplete/GetCountryName",
                data: {
                    keyword: $("#country").val()
                },
                dataType: "json",
                success: function (data) {

                    if (data.length > 0) {
                        $('#DropdownCountry').empty();
                        $('#country').attr("data-toggle", "dropdown");
                        $('#DropdownCountry').dropdown('toggle');
                    }
                    else if (data.length == 0) {
                        $('#country').attr("data-toggle", "");
                    }
                    $.each(data, function (key,value) {
                        if (data.length >= 0)
                            $('#DropdownCountry').append('<li role="displayCountries" ><a role="menuitem dropdownCountryli" class="dropdownlivalue">' + value['name'] + '</a></li>');
                    });
                }
            });
        });
        $('ul.txtcountry').on('click', 'li a', function () {
            $('#country').val($(this).text());
        });
    });

Here im trying to search a keyword from three table using codeigniter and ajax.我在这里尝试使用 codeigniter 和 ajax 从三个表中搜索关键字。
I'm getting that error in the model我在model遇到了那个错误

How can I solve this issue?我该如何解决这个问题?

Also, if the issue is solved whether my search will fetch the data from database.另外,如果问题得到解决,我的搜索是否会从数据库中获取数据。 What is wrong in the code?代码有什么问题?

You have to use same column number in your select statement as following您必须在select语句中使用相同的列号,如下所示

First select you use 4 columns :首先select您使用 4 列:

$this->db->select('collg_name,city,state,country as type');


second select you use 3 columns : 


$this->db->select('course_offrd_name,category_name,subcategory_name');


class Datacomplete extends CI_Model{

    public function GetRow($keyword) {        
       $this->db->select('collg_name,city,state,country as type');
       $this->db->from('tbl_college');
       $this->db->like("collg_name",$keyword);
       $this->db->or_like('city',$keyword,'after');
       $this->db->or_like('state',$keyword,'after');
       $this->db->or_like('country',$keyword,'after');
       $query1 = $this->db->get_compiled_select();
       $this->db->select('course_offrd_name,category_name,subcategory_name, null as type');
       $this->db->from('tbl_course_offered');
       $this->db->like("course_offrd_name",$keyword);
       $this->db->or_like('category_name',$keyword,'after');
       $this->db->or_like('subcategory_name',$keyword,'after');
       $query2 = $this->db->get_compiled_select();
       $result = $this->db->query($query1." UNION ".$query2);
       return $result->result();
    }
}

In '$result = $this->db->query($query1." UNION ".$query2);''$result = $this->db->query($query1." UNION ".$query2);' you are combining 2 table results but in 1st table you are selecting 4 columns & in 2nd table you are selecting 3 columns.您正在组合 2 个表格结果,但在第一个表格中您选择了4列,而在第二个表格中您选择了 3 列。

When You combine using UNION then it will result into records & here both table columns are different and also the column count is different.当您使用UNION组合时,它将导致记录,这里两个表列都不同,列数也不同。 To use UNION you have to select same column count & if possible then select same columns only.要使用 UNION,您必须选择相同的列数,如果可能,则只选择相同的列。

In above situation error occure bcz:在上述情况下发生错误 bcz:

                     + collg_name + city + state + type +
---------------------+------------+------+-------+------+
1st table record     + A          + b    + c     + d    +
---------------------+------------+------+-------+------+
2nd table record     + x          + y    + z     + -    +

2nd table don`t have last column value第二个表没有最后一列值

Union execute in below condition:联合在以下条件下执行:

SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;

Result :结果 :

        + City +
1st tbl | city1|
1st tbl | city2|
2st tbl | city3|

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM