Laravel困难的联合查询与分页

时间:2014-12-06 02:04:52

标签: php sql laravel eloquent union

我有这些表,可能在mysql或postgres数据库中:

                                                                           
|===============================================================================|
|                                      articles                                 |
|===============================================================================|
|id|user_id|state|       title        |text|created_at|updated_at|abstract|image|
|==|=======|=====|====================|====|==========|==========|========|=====|
|1 | 1     | 5   |L'hopital rule      |... |2014-12-05|          | ...    | ... |
|2 | 2     | 5   |Gaussian quadrature |... |2012-11-13|2014-12-05| ...    | ... |
|3 | 2     | 5   |Gauss elimination   |... |2013-07-18|2014-03-09| ...    | ... |
|4 | 3     | 5   |Plant cell structure|... |2013-09-10|2013-12-15| ...    | ... |
|5 | 4     | 5   |Game theory         |... |2011-02-15|2012-11-30| ...    | ... |
|                                       .  .  .                                 |
|===============================================================================|

|===========|      |====================|      |===========|                    
| tag_groups|      |       tags         |      |   users   |                    
|===========|      |====================|      |===========|                    
|id|  name  |      |id|id_article|id_tag|      |id|  name  |                    
|==|========|      |==|==========|======|      |==|========|                    
|1 |Math    |      |1 | 1        | 1    |      |1 |Tom Ther|                    
|2 |Calculus|      |2 | 1        | 2    |      |2 |May Nick|                    
|3 |Gradient|      |3 | 2        | 1    |      |3 |Math Ria|                    
|4 |Geometry|      |4 | 3        | 1    |      |4 |Gauss Li|                    
|  .  .  .  |      |      .  .  .       |      |  .  .  .  |                    
|===========|      |====================|      |===========|                    

让我们$query = "th"。让我们$like_query = "%th%" 我想在文章中用$like_query字符串搜索以下逻辑:

  1. 第一个结果应该是tag_group.name与$like_query字符串匹配的文章。这是包含id=1id=2id=3的文章。

  2. 下一个结果应该是与文章标题匹配的文章。因此添加了id=5

  3. 然后,添加由具有匹配名称的用户创建的文章。因此添加了id=3的文章。

  4. 最后,添加与文本匹配的文章。

  5. 所有文章都必须按此顺序排列,不得有任何重复。 我在laravel中使用build_sql函数创建了以下代码:

    function like($string, $escape_char = '\\') {
        return str_replace(
            array($escape_char, '_', '%'),
            array($escape_char.$escape_char, $escape_char.'_', $escape_char.'%'),
            $string
        );
    }
    
    function wild($string) {                                                
        return DB::connection()->getPdo()->quote('%'.like($string).'%');    
    }                                                                       
    
    function build_sql($quoted_query) {                                     
        return "                                                            
            SELECT DISTINCT                                                 
              articles.id, articles.user_id, articles.state, articles.title,
              articles.image, articles.abstract, articles.text, articles.created_at,
              articles.updated_at                                           
            FROM (                                                          
              SELECT articles.*, ord                                        
              FROM (                                                        
                SELECT articles.*, 1 ord                                    
                FROM articles                                               
                LEFT JOIN tags ON tags.id_article = articles.id             
                LEFT JOIN tag_groups ON tags.id_tag = tag_groups.id         
                WHERE articles.state = 5 AND tag_groups.name ILIKE $quoted_query
                UNION                                                       
    
                SELECT articles.*, 2 ord                                    
                FROM articles                                               
                WHERE articles.state = 5 AND articles.title ILIKE $quoted_query
                UNION                                                       
    
                SELECT articles.*, 3 ord                                    
                FROM articles                                               
                LEFT JOIN users ON users.id = articles.user_id              
                WHERE articles.state = 5 AND users.name ILIKE $quoted_query 
                UNION                                                       
    
                SELECT articles.*, 4 ord                                    
                FROM articles                                               
                WHERE articles.state = 5 AND articles.text ILIKE $quoted_query
              ) articles                                                    
              ORDER BY ord ASC, articles.updated_at DESC                    
            ) articles;                                                     
        "                                                                       
    }
    
    $articles = DB::table((DB::raw($build_sql(wild($_query)))))->distinct();
    
    $maxPages = ceil(count($articles->get()) / 9);                          
    return View::make('index', array(                                       
                'articles' => $articles->simplePaginate(9),
                'maxPages' => $maxPages,                                    
    ));                                                                     
    

    这不起作用,我不知道该怎么做。有没有办法如何使用雄辩的模型写得很好?或者如果没有,我应该如何更新build_sql字符串?

0 个答案:

没有答案