亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

檢查兩個集合的差異

檢查兩個集合的差異

PHP
幕布斯6054654 2023-05-12 16:02:17
我有 2 個變量:一個存儲表的內容,一個存儲檢查數據庫中所有鍵的 sql 腳本。$indexes = DB::connection('sqlsrv2')->table('indexes')->get();$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        ");我需要查看表中的內容是否也在 sql 腳本中,如果沒有則刪除該行。我已經嘗試過“diff”和“diffAssoc”方法,但顯示所有記錄都不同。$diff = $indexes->diffAssoc(get_object_vars(collect($dbIndexes)));
查看完整描述

1 回答

?
肥皂起泡泡

TA貢獻1829條經驗 獲得超6個贊

我所做的是,而不是:


$indexes = DB::connection('sqlsrv2')->table('indexes')->get();

誰返回一個對象集合,我用過:


$indexes = DB::connection('sqlsrv2')->select("select * from indexes");

誰返回一個數組。而兩者的對比是這樣的:


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


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


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

通過這種方式,它檢查兩種方式的差異。如果您僅以一種方式進行檢查,它將顯示差異。


查看完整回答
反對 回復 2023-05-12
  • 1 回答
  • 0 關注
  • 128 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號