doudongdang4483 2015-10-13 11:20
浏览 39
已采纳

SQL组内连接无法获得所有结果

I really trying to get this SQL to work. Im no expert so really cant figure this out.

            $sqlquery = " SELECT 
    s.searchword AS searchword, 
    s.id AS id, 
    COUNT( c.id ) AS searchresult, 
    s.region AS region 
    FROM search_words AS s 
    INNER JOIN company_data AS c ON 
    c.branch_text LIKE CONCAT(  '%', s.searchword,  '%' )       
    GROUP BY 1 ORDER BY s.date DESC";

This gives me:

      Array
    (
[0] => Array
    (
        [searchword] => WHOLESALE
        [searchid] => 427
        [searchresult] => 98
        [region] => stockholm
    )

[1] => Array
    (
        [searchword] => cars
        [searchid] => 426
        [searchresult] => 26
        [region] => 
    )

[2] => Array
    (
        [searchword] => Retail
        [searchid] => 342
        [searchresult] => 41
        [region] => stockholm
    )

[3] => Array
    (
        [searchword] => Springs
        [searchid] => 339
        [searchresult] => 4
        [region] => stockholm
    )

[4] => Array
    (
        [searchword] => Leasing
        [searchid] => 343
        [searchresult] => 2
        [region] => stockholm
    )

[5] => Array
    (
        [searchword] => Food
        [searchid] => 340
        [searchresult] => 37
        [region] => stockholm
    )

 )

But, it does not give me any of the other results where there are no searchhits, would retur something like [searchresult] => 0. Meaning they do not group as I wish, because there are no such searchwords within the company_data table.

How can I fix this, please help :(

EDIT:

Here is the full code:

      public function getUserSearches()
    {

    $sqlquery = " SELECT 
    s.searchword AS searchword, 
    s.id AS id, 
    s.userId AS userid, 
    COUNT( c.id ) AS searchresult, 
    s.region AS region 
    FROM search_words AS s 
    INNER JOIN company_data AS c ON 
    c.branch_text LIKE CONCAT(  '%', s.searchword,  '%' )       
    GROUP BY 1 ORDER BY s.date DESC";

     // IS THERE ANYTHING WRONG HERE?? LIKE IT DOES NOT MATCH AGAINST THE USER?
    $result = $this->dbh->query($sqlquery, array(":userId" => $this->user_id));

    $arr = array();
    foreach ($result as $item)  {
        array_push($arr, array('searchword' => $item['searchword'], 'searchid' => $item['id'], 
    'searchresult' => $item['searchresult'], 'userid' => $item['userid'],
    'region' => $item['region']));
    }

        return json_encode($arr);
    return print_r($arr);
    }
  • 写回答

3条回答 默认 最新

  • douke1942 2015-10-13 11:37
    关注

    Use LEFT JOIN so that any row that doesn't match the search criteria in the join condition will come with null, therefore count will be 0. Something like this:

    SELECT 
      s.searchword AS searchword, 
      s.id AS id, 
      s.region AS region,
      COUNT(COALESCE(c.id, 0)) AS searchresult
    FROM search_words AS s 
    LEFT JOIN company_data AS c 
            ON c.branch_text LIKE CONCAT(  '%', s.searchword,  '%' )       
    GROUP BY s.searchword, s.id, s.region
    ORDER BY s.date DESC;
    

    See this for more details about the SQL Join types.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分