简体   繁体   中英

SQL group inner join cannot get all results

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

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;

Seethis for more details about the SQL Join types.

You need a LEFT JOIN , instead of an INNER JOIN . However, there are other simplifications you can make as well:

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

For instance, you don't need column aliases when you are not changing the column name. You are only aggregating on searchword ; I assume this is unique in search_words . Otherwise, s.id , s.region . and s.date would have indeterminate values.

The use of LIKE for the JOIN affects performance. If you only have a small amount of data, this is fine. Otherwise, you might want to think about other data structures.

I think what you want is perhaps this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM