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

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

DB::select 的輸出與 phpMyAdmin 中的原始查詢不同

DB::select 的輸出與 phpMyAdmin 中的原始查詢不同

PHP
繁花如伊 2023-10-15 15:46:04
我有一個在 Laravel 應用程序中使用的 MariaDB 查詢,我希望它返回 7 列。當我使用 PHP 轉儲結果數組時,它似乎只返回 4。但是,當我采用相同的查詢并在 PhpMyAdmin SQL 編輯器中運行它時,它返回的全部 7 與我的預期完全一致。只是想知道是什么導致 Laravel 將結果過濾為只有四列:狀態、姓名、電話號碼和描述?謝謝!拉拉維爾查詢:    $entries = DB::select('SELECT status, contacts.name, contacts.telephone_number, companies.name, roles.name, stages.description, actions.description FROM entries JOIN contacts ON entries.contact_id = contacts.id JOIN companies ON contacts.id = companies.contact_id JOIN roles ON companies.id = roles.company_id JOIN stages ON roles.id = stages.role_id JOIN actions ON stages.id = actions.stage_id');PHPMyAdmin SQL 編輯器的查詢:    SELECT status, contacts.name, contacts.telephone_number, companies.name, roles.name, stages.description, actions.description FROM entries JOIN contacts ON entries.contact_id = contacts.id JOIN companies ON contacts.id = companies.contact_id JOIN roles ON companies.id = roles.company_id JOIN stages ON roles.id = stages.role_id JOIN actions ON stages.id = actions.stage_id
查看完整描述

2 回答

?
一只萌萌小番薯

TA貢獻1795條經驗 獲得超7個贊

兩種情況的結果相同。他們只是使用相同的屬性,因此雄辯的結果相互覆蓋。使用別名來解決問題

SELECT status, contacts.name as contact_name, contacts.telephone_number, companies.name as company_name, roles.name as role_name , stages.description, actions.description FROM entries JOIN contacts ON entries.contact_id = contacts.id JOIN companies ON contacts.id = companies.contact_id JOIN roles ON companies.id = roles.company_id JOIN stages ON roles.id = stages.role_id JOIN actions ON stages.id = actions.stage_id


查看完整回答
反對 回復 2023-10-15
?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

您應該設置別名以避免覆蓋。另一件事是,如果你使用 Laravel,那么 eloquent 就是你的朋友:


$entries = DB::table('entries')

    ->join('contacts', 'entries.contact_id', '=', 'contacts.id')

    ->join('companies', 'contacts.id', '=', 'companies.contact_id')

    ->join('roles', 'companies.id', '=', 'roles.company_id')

    ->join('stages', 'roles.id', '=', 'stages.role_id')

    ->join('actions', 'stages.id', '=', 'actions.stage_id')

    ->select('status', 'contacts.name AS contact_name', 'contacts.telephone_number', 'companies.name AS company_name', 'roles.name AS role_name', 'stages.description AS stage_description', 'actions.description AS action_description')

    ->get();

更好的方法是使用模型和集合關系。


查看完整回答
反對 回復 2023-10-15
  • 2 回答
  • 0 關注
  • 148 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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