<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # MySQL用union把兩張沒有關聯的表合并,并使用條件查詢排序分頁 補充:(ps:調頭回來研究研究,補充) 1,簡單sql語句,先把2張表集合在一起,你發現news表數據在上面,接著才到card, 有沒有發現card 的name字段沒有,只顯示 title 字段 所以以前面的數據和字段優先 SELECT id,title,created_at FROM news UNION SELECT id,`name`,created_at FROM card ; ![](https://img.kancloud.cn/f4/24/f4244b57c3923a5d2e9314aa38010508_904x488.png) 2,進一步優化,第一步看到2張表數據都在里面區分不出來是哪張表,所以改了下sql語句: SELECT id,title,created_at ,0 as type FROM news UNION SELECT id,`name`,created_at ,1 as type FROM card ; ![](https://img.kancloud.cn/f4/58/f4584bbc92f84057ab72f1ba506aedff_931x519.png) 3,進一步優化,增加了個排序功能,如下: ![](https://img.kancloud.cn/57/35/57356eefaa062bf4f273a771ce0fbbe0_1186x530.png) ### 場景應用: 類似某團的搜索,既可以搜索店鋪,也可以搜索商品; 類似某名片的搜索,既可以搜索企業,也可以搜索名片; ### 實現: 我用下面在laravel中實現的代碼案例說下: ~~~ $test1= UserHistoryCompany::orWhere(function ($query) use($title) { $query->where('user_history_company.user_id', '=', $this->user_id)->where('is_delete',0) ->where('company.name', 'like', '%'.$title.'%'); }) ->join('company','company.id','=','user_history_company.company_id') ->select('user_history_company.id','user_history_company.user_id','user_history_company.company_id as c_id', 'user_history_company.updated_at','company.name as company_name','company.id as company_id','logo', DB::raw('2 as type , 0 as card_id , 0 as head_img , 0 as job_name , 0 as department_name , 0 as name ')); $result= UserHistoryCard::orWhere(function ($query) use($title) { $query->where('user_history_card.user_id', '=', $this->user_id)->where('is_delete',0) ->where('company.name', 'like', '%'.$title.'%'); }) ->orWhere(function ($query) use($title) { $query->where('user_history_card.user_id', '=', $this->user_id)->where('is_delete',0) ->where('card.name', 'like', '%'.$title.'%'); }) ->orWhere(function ($query) use($title) { $query->where('user_history_card.user_id', '=', $this->user_id)->where('is_delete',0) ->where('card.mobile', 'like', '%'.$title.'%'); }) ->join('card','card.id','=','user_history_card.card_id') ->join('company','company.id','=','card.company_id') ->select('user_history_card.id','user_history_card.user_id','user_history_card.card_id as c_id', 'user_history_card.updated_at','company.name as company_name','company.id as company_id','logo', DB::raw('1 as type , user_history_card.card_id , card.head_img , card.job_name , card.department_name , card.name ')) ->unionAll($test1); $sql = $result->toSql(); $result = DB::table(DB::raw("($sql) as a ")) ->mergeBindings($result->getQuery()) ->orderBy('updated_at', 'desc') ->paginate(request()->input('page_num') ?? 50); dd($result); //上面 xxxxxxxxxx->unionAll($test1); 其實發現也可以直接 ->unionAll($test1 ->orderBy('updated_at', 'desc') ->paginate(request()->input('page_num') ?? 50); 然后下面的 $sql = $result->toSql();$result = DB::table(DB::raw("(.........操作都不需要了, ~~~ 什么 ?上面的看不懂?好吧,我簡單列下: 1.當兩張表屬性完全相同時,可以直接合并(union會刪除重復數據,union all會返回所有結果集) (1)將兩個表合并 ~~~ select * from 表1 union select * from 表2 select * from 表1 union all select * from 表2 ~~~ (2)將兩個表合并,并使用條件查詢 ~~~ select * from ( select * from 表1 union select * from 表2) as 新表名 where 條件1 and 條件2 ~~~ 2、當兩個表的屬性不同時,要分別查詢每個屬性,給不同屬性名取一個別名,例如收入和支出表:(表中的id和remark是相同屬性名,其他的屬性名都不同) ~~~ select * from( (select id, a1 as a, b1 as b, c1 as c, d1 as d, e1 as e, updated_at from a1_table) union all (select id, a2 as a, b2 as b, c2 as c, d2 as d, e2 as e, updated_at from a2_table) ) as c ~~~ ![](https://img.kancloud.cn/70/6e/706e4acb3e3340f40bdaebb32e6e6336_875x693.png) 溫馨提示: 坑1:雖然2個表結構要整合再一起排序分頁,就要通過 as 別名來轉換相同的字段,不然就被前面一個查詢條件的字段給覆蓋了, 坑2:上面的a1,b1,c1 順序要對著a2,b2,c2,才行,否則就被前面一個查詢條件的字段給覆蓋了, 錯誤示范: 1,a1,b1,c1 順序要對著c2,b2,a2,你查詢出來的a1值就會到c2里面,c1到a2里面,不信你可以試試。 坑3:2個查詢字段數量必須一致,否則就會報錯。 完事了,比較懶,想看詳情的話,看下面2個鏈接,前人寫的 ———————————————— 參考鏈接:https://blog.csdn.net/qq_43341807/article/details/120115151 參考鏈接:https://www.cnblogs.com/zhengchuzhou/p/10262260.html
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看