php小編魚仔在這裡為大家介紹一個有關資料處理的問題。當我們需要從一個表中排除另一個表中已經存在的id時,我們可以透過特定的方法來實現。這個問題在數據處理中是非常常見的,解決它可以幫助我們更好地整理和處理數據,並提高數據處理的效率和準確性。在接下來的文章中,我們將詳細介紹如何透過使用特定的查詢語句來實現這個目標。
問題內容
夥計們,我再次需要你們的幫忙。 我的大腦壞了,我不懂我做錯了什麼。 我有 2 張桌子
requests id string `json:"id"` userid string `json:"user_id"` status string `json:"status"`
students id string `json:"id"` userid string `json:"user_id"` requestid string `json:"request_id"` startdate time.time `json:"start_date"` enddate time.time `json:"end_date"`
作為接收者,我有一個使用者 id,我需要尋找開始日期和結束日期之間的所有使用者請求,但應排除狀態為「已取消」或「拒絕」的請求。 我以為我可以使用 left join 但它不能按我的需要工作。 目前我有以下查詢:
status := []string{"canceled", declined"} type Result struct { tableName struct{} `pg:"students"` ID string `json:"id"` UserID int `json:"user_id"` RequestID string `pg:"fk:request_id"` Status string `json:"status"` } var res []Result err := Model(&res). ColumnExpr("id, user_id, requests.status"). Where("user_id = ?", UseID). Where("start_date >= ? AND end_date <= ?", startDate, endDate). Join("LEFT JOIN requests ON requests.id = request_id"). WhereIn("requests.status IN (?)", status). Select()
目前,我從表學生那裡收到了所需日期的所有數據,但即使請求表中的狀態為“已取消”或“拒絕”,也沒有從結果中排除。 如果您需要我提供任何其他資訊來幫助我,請告訴我。 感謝您的任何意見和建議。
解決方法
您需要確保正確應用 where
子句來排除狀態為「已取消」或「已拒絕」的請求。
type Result struct { TableName struct{} `pg:"students"` ID string `json:"id"` UserID string `json:"user_id"` RequestID string `pg:"fk:request_id"` Status string `json:"status"` } var res []Result status := []string{"canceled", "declined"} err := Model(&res). ColumnExpr("students.id, students.user_id, students.request_id, requests.status"). Where("students.user_id = ?", UserID). Where("students.start_date >= ? AND students.end_date <= ?", startDate, endDate). Where("NOT EXISTS (SELECT 1 FROM requests WHERE students.request_id = requests.id AND requests.status IN (?))", status). Select()
我使用not exists
子查詢來檢查在requests
表中具有相同request_id
的請求,並且其狀態存在於status
切片中。如果在子查詢中找到此類請求,則它們將從最終結果中排除.
以上是需要從第二個表中排除第一個表中存在的 id的詳細內容。更多資訊請關注PHP中文網其他相關文章!

goisidealforbuildingscalablesystemsduetoitssimplicity,效率和建築物內currencysupport.1)go'scleansyntaxandaxandaxandaxandMinimalisticDesignenhanceProductivityAndRedCoductivityAndRedCuceErr.2)ItSgoroutinesAndInesAndInesAndInesAndineSandChannelsEnablenableNablenableNableNablenableFifficConcurrentscorncurrentprogragrammentworking torkermenticmminging

Initfunctionsingorunautomationbeforemain()andareusefulforsettingupenvorments和InitializingVariables.usethemforsimpletasks,避免使用輔助效果,andbecautiouswithTestingTestingTestingAndLoggingTomaintAnainCodeCodeCodeClarityAndTestesto。

goinitializespackagesintheordertheordertheyimported,thenexecutesInitFunctionswithinApcageIntheirdeFinityOrder,andfilenamesdetermineTheOrderAcractacractacrosmultiplefiles.thisprocessCanbeCanbeinepessCanbeInfleccessByendercrededBydeccredByDependenciesbetenciesbetencemendencenciesbetnependendpackages,whermayleLeadtocomplexinitialitialializizesizization

CustomInterfacesingoarecrucialforwritingFlexible,可維護,andTestableCode.TheyEnableDevelostOverostOcusonBehaviorBeiroveration,增強ModularityAndRobustness.byDefiningMethodSigntulSignatulSigntulSignTypaterSignTyperesthattypesmustemmustemmustemmustemplement,InterfaceSallowForCodeRepodEreusaperia

使用接口進行模擬和測試的原因是:接口允許定義合同而不指定實現方式,使得測試更加隔離和易於維護。 1)接口的隱式實現使創建模擬對像變得簡單,這些對像在測試中可以替代真實實現。 2)使用接口可以輕鬆地在單元測試中替換服務的真實實現,降低測試複雜性和時間。 3)接口提供的靈活性使得可以為不同測試用例更改模擬行為。 4)接口有助於從一開始就設計可測試的代碼,提高代碼的模塊化和可維護性。

在Go中,init函數用於包初始化。 1)init函數在包初始化時自動調用,適用於初始化全局變量、設置連接和加載配置文件。 2)可以有多個init函數,按文件順序執行。 3)使用時需考慮執行順序、測試難度和性能影響。 4)建議減少副作用、使用依賴注入和延遲初始化以優化init函數的使用。

go'SselectStatementTreamLinesConcurrentProgrambyMultiplexingOperations.1)itallowSwaitingOnMultipleChannEloperations,執行thefirstreadyone.2)theDefirstreadyone.2)thedefefcasepreventlocksbysbysbysbysbysbythoplocktrograpraproxrograpraprocrecrecectefnoopeready.3)

contextancandwaitgroupsarecrucialingoformanaginggoroutineseflect.1)context contextsallowsAllowsAllowsAllowsAllowsAllingCancellationAndDeadLinesAcrossapibiboundaries,確保GoroutinesCanbestoppedGrace.2)WaitGroupsSynChronizeGoroutines,確保Allimizegoroutines,確保AllizeNizeGoROutines,確保AllimizeGoroutines


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 Linux新版
SublimeText3 Linux最新版

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。