SQLSTATE [42S22]:未找到列:1054 laravel 4 中“字段列表”中的未知列“id”

Posted

技术标签:

【中文标题】SQLSTATE [42S22]:未找到列:1054 laravel 4 中“字段列表”中的未知列“id”【英文标题】:SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'field list' in laravel 4 【发布时间】:2016-07-12 22:00:11 【问题描述】:

登录后重定向页面显示以下错误。 In 将未知列显示为“id”。登录后控制器移至支付功能。在这个函数中,我使用 DB 进行支付过程。任何人都可以帮助纠正错误吗?

这里的 usp_payment_detail 是我的数据库的过程。

错误:

SQLSTATE[42S22]:未找到列:1054 '字段列表'中的未知列 'id'(SQL:调用 usp_payment_details(null,null))

程序:

BEGIN   
-- Ashok Kumar, 25/11/2015 
-- V1.3
-- Parameters are optional.
-- Parameter values for "is_completed" are :
-- 0 - incomplete , 1 - complete , null - for complete/incomplete
-- Usage
-- call usp_payment_details(null,'babyname');
-- call usp_payment_details(null,null);

    DECLARE input_is_completed tinyint(4) default null;
    DECLARE input_service_name varchar(200) default null;
    DECLARE dynamic_where varchar(3000);

    set input_is_completed=(select is_completed);
    set input_service_name=(select service_name);
    set dynamic_where='';

    if input_is_completed is null then
        set dynamic_where=concat(' where paid_at is not null');
    else if input_is_completed=0 then
        set dynamic_where=concat(' where is_completed is null and paid_at is not null');
    else if input_is_completed=1 then
        set dynamic_where=concat(' where is_completed is not null and paid_at is not null');
    end if;
    end if;
    end if;

    DROP TEMPORARY TABLE IF EXISTS tmpprocess_tables;
    CREATE TEMPORARY TABLE tmpprocess_tables 
    (row_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,tablename varchar(100));

    set @db_name=(SELECT DATABASE());

    if input_service_name is null then
        begin
            insert into tmpprocess_tables(tablename)
            SELECT table_name FROM information_schema.tables 
            WHERE table_schema=@db_name and table_name not in('logs','users');
        end;
    else
        begin
            set @insert_sql=concat('insert into tmpprocess_tables(tablename)
            SELECT table_name FROM information_schema.tables 
            WHERE table_schema=','',@db_name,'',' and table_name like concat('%',',input_service_name,','%')');

            Prepare stmt from @insert_sql;
            Execute stmt;

        end;    
    end if;

    DROP TEMPORARY TABLE IF EXISTS tmppopulate_payments;
    CREATE TEMPORARY TABLE tmppopulate_payments 
    (sno INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,primary_id int(11),tab_name varchar(200)
    ,is_plan varchar(100),is_name varchar(100),amount decimal(10,2),is_completed datetime,paid_at datetime,first_name varchar(50),
    middle_name varchar(50),last_name varchar(50),initial varchar(50),full_name varchar(50));

    set @count=(select count(row_id) from tmpprocess_tables);
    set @ctr=1;

    WHILE(@ctr<=@count) DO

        set @tableName=(select tableName from tmpprocess_tables where row_id=@ctr);

        SET @insert_sql=concat('insert into tmppopulate_payments(primary_id,tab_name,is_plan,is_name,amount,is_completed,paid_at,first_name,middle_name,last_name,initial,full_name)
        select id,','', @tableName,'',' ,is_plan,is_name,amount,is_completed,paid_at,first_name,middle_name,last_name,initial,full_name from ', @tableName,' ',dynamic_where);

        PREPARE stmt1 FROM @insert_sql;
        EXECUTE stmt1;

        SET @ctr=@ctr+1; 

    END WHILE;

    set @select_sql=concat('select sno,primary_id,tab_name,is_plan,is_name,amount,is_completed,paid_at,first_name,middle_name,last_name,initial,full_name from tmppopulate_payments order by paid_at desc');

    PREPARE stmt1 FROM @select_sql;
    EXECUTE stmt1;
    End

登录控制器:

public function payment($t)

    $showFilter = 0;
    $table = [];

    $status = "null";
    $service_name= "null";

    // $serviceArr= $tab_name;
    if(Input::has('status'))
        
            $status = "'" .Input::get('status') . "'";
        
    if(Input::has('service_name'))
        
            $service_name = "'" . Input::get('service_name') . "'";
        

        $table =array(''=>'- Select -');

        $data = DB::select("select TABLE_NAME from information_schema.tables where TABLE_SCHEMA = 'urbanedg_mn'");
        foreach ($data as $key => $value) 
            
                $table[$value->TABLE_NAME] = $value->TABLE_NAME;    
            

    $data = DB::select("call usp_payment_details($status,$service_name)");



    return View::make('backoffice/loginpage/payment')->with('table',$table)
                                         ->with('data',$data)
                                         ->with('statusArr',CustomClass::$StatusArr)
                                         ->with('serviceArr',CustomClass::$ServiceArr)
                                         ->with('s',Input::has("s")?Input::get("s") : 0)
                                         ->with('msg',Input::has("msg")?Input::get("msg") : '')
                                         ->with('t',$t);





【问题讨论】:

【参考方案1】:

看看你的程序,

if input_service_name is null then
    begin
        insert into tmpprocess_tables(tablename)
        SELECT table_name FROM information_schema.tables 
        WHERE table_schema=@db_name and table_name not in('logs','users');
    end;
else
    begin
        set @insert_sql=concat('insert into tmpprocess_tables(tablename)
        SELECT table_name FROM information_schema.tables 
        WHERE table_schema=','',@db_name,'',' and table_name like concat('%',',input_service_name,','%')');

        Prepare stmt from @insert_sql;
        Execute stmt;

    end;    
end if;

如果传递给此过程的参数为空,则将架构中的所有表名插入到临时表中。

可能有任何一个表没有名为“id”的列。确保您只插入所需的表或所有具有 "id"

列的表

【讨论】:

如何查看临时表? 临时表只是sql检索的表名,“SELECT table_name FROM information_schema.tables WHERE table_schema=@db_name and table_name not in('logs','users');”。跨度> 我在哪里添加 'id' 列? SET @insert_sql=concat('插入 tmppopulate_payments(primary_id,tab_name,is_plan,is_name,amount,is_completed,paid_at,first_name,middle_name,last_name,initial,full_name) 选择 id,','' , tableName,'',' ,is_plan,is_name,amount,is_completed,paid_at,first_name,middle_name,last_name,initial,full_name from ', tableName,' ',dynamic_where); 通过 select (id,....) 插入 tmppopulate_payments。记下 select 语句

以上是关于SQLSTATE [42S22]:未找到列:1054 laravel 4 中“字段列表”中的未知列“id”的主要内容,如果未能解决你的问题,请参考以下文章

SQLSTATE [42S22]:未找到列:1054 laravel 4 中“字段列表”中的未知列“id”

SQLSTATE [42S22]:未找到列:1054 'where 子句'中的未知列 'id'(SQL:select * from `songs` where `id` = 5 limit 1)

SQLSTATE [42S22]:找不到列:1054 未知列 - Laravel

Laravel 删除数据 - SQLSTATE [42S22]:找不到列:1054 未知列

SQLSTATE [42S22]:找不到列:1054未知列material_tags.material_uuid

SQLSTATE [42S22]:找不到列:1054 未知列 laravel 5.1