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

时间:2015-10-13 11:20:51

标签: php sql

我真的想让这个SQL工作。我没有专家,所以真的无法弄清楚这一点。

            $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";

这给了我:

      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
    )

 )

但是,它没有给我任何其他没有搜索的结果,会返回类似[searchresult] => 0.意思是他们没有按照我的意愿分组,因为company_data表中没有这样的搜索词。

我该如何解决这个问题,请帮忙:(

编辑:

以下是完整代码:

      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 个答案:

答案 0 :(得分:2)

使用LEFT JOIN以便任何与连接条件中的搜索条件不匹配的行都将为null,因此count将为0.这样的内容:

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;

有关SQL Join类型的更多详细信息,请参阅this

答案 1 :(得分:0)

您需要LEFT JOIN,而不是INNER JOIN。但是,您还可以进行其他简化:

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

例如,如果不更改列名,则不需要列别名。您只是在searchword汇总;我认为这在search_words中是唯一的。否则,s.ids.region。而s.date会有不确定的价值。

LIKEJOIN的使用会影响效果。如果您只有少量数据,这很好。否则,您可能想要考虑其他数据结构。

答案 2 :(得分:0)

我认为你想要的也许是这样:

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
WHERE s.userId='"+$this->userid+"'  
LEFT 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);

$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);
}