首页 > 解决方案 > 如何从 SQL 数据库中删除记录

问题描述

我有这两个变量:一个存储表中的内容,第二个存储数据库中的查询。我也在做一个比较,看看 2 变量与 $ diff 之间是否存在差异。这是我的代码:

public function destroy()
    {
       
        $indexes = DB::connection('sqlsrv2')->select("select * from indexes");
        
        $dbIndexes = DB::connection('sqlsrv')->select
        ("
                select 
                    schema_name(t.[schema_id]) + '.' + t.[name] as table_view,
                    substring(column_names, 1, len(column_names)-1) as [columns],
                    case when i.is_primary_key = 1 then 'Primary_key'
                    when i.is_unique = 1 then 'Unique'
                    else 'Not_unique' end as [type],
                    i.[name] as index_name,
                    i.index_id
                    from sys.objects t
                    inner join sys.indexes i
                    on t.[object_id] = i.[object_id]
                    cross apply 
                    (
                            select col.[name] + ', '
                            from sys.index_columns ic
                        inner join sys.columns col on ic.[object_id] = col.[object_id] and ic.column_id = col.column_id
                        where ic.[object_id] = t.[object_id]
                        and ic.index_id = i.index_id
                        order by col.column_id
                        for xml path ('')
                    ) D (column_names)
                    where t.is_ms_shipped <> 1
                    and index_id > 0
        ");

        $indexes = array_map('serialize', $indexes);
           
        $dbIndexes = array_map('serialize', $dbIndexes);

        $diff = array_merge(array_diff($indexes,$dbIndexes),array_diff($dbIndexes,$indexes));
        dd($diff);

    }

如何从 $indexes 中删除 diff 的结果?

标签: phplaravel

解决方案


推荐阅读