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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。