Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9194201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:21:06+00:00 2026-06-17T21:21:06+00:00

In the below sql statement: SELECT `keywords`.keyID, count(`keywords-occurencies`.keyID) as countOccurencies FROM `keywords-occurencies` LEFT JOIN

  • 0

In the below sql statement:

 SELECT `keywords`.keyID, count(`keywords-occurencies`.keyID) as countOccurencies 
                    FROM `keywords-occurencies`  
                    LEFT JOIN `keywords` 
                    ON `keywords-occurencies`.keyID = `keywords`.keyID 
                    WHERE `keywords-occurencies`.`keyID` IN (1,2,3) AND date BETWEEN '2013/01/25' AND '2013/01/27'
                    GROUP BY `keywords`.`keyID`

If keyID 3 has no return values it is not counted as 0 and it is not included in the result set and a result like this is displayed

keyID countOccurencies
1       3
3       5

I would like to display the zero results like

keyID countOccurencies
1       3
2       0
3       5

Sample data to test with:

--
-- Table structure for table `keywords`
--

CREATE TABLE IF NOT EXISTS `keywords` (
  `keyID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `keyName` varchar(40) NOT NULL,
  PRIMARY KEY (`keyID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `keywords`
--

INSERT INTO `keywords` (`keyID`, `keyName`) VALUES
(1, 'testKey1'),
(2, 'testKey2');

-- --------------------------------------------------------

--
-- Table structure for table `keywords-occurencies`
--

CREATE TABLE IF NOT EXISTS `keywords-occurencies` (
  `occurencyID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `keyID` int(10) unsigned NOT NULL,
  `date` date NOT NULL,
  PRIMARY KEY (`occurencyID`),
  KEY `keyID` (`keyID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `keywords-occurencies`
--

INSERT INTO `keywords-occurencies` (`occurencyID`, `keyID`, `date`) VALUES
(1, 1, '2013-01-27'),
(2, 1, '2013-01-26');

--
-- Constraints for table `keywords-occurencies`
--
ALTER TABLE `keywords-occurencies`
  ADD CONSTRAINT `keywords@002doccurencies_ibfk_1` FOREIGN KEY (`keyID`) REFERENCES `keywords` (`keyID`) ON DELETE CASCADE ON UPDATE CASCADE;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:21:08+00:00Added an answer on June 17, 2026 at 9:21 pm

    things to do

    • you should be grouping it with GROUP BY keywords-occurencies.keyID
    • and you must be displaying keywords-occurencies.keyID not the keywords.keyID
    • COUNT keywords.keyID
    • (optional) use ALIAS so you can get rid of the backticks other than tableNames

    query,

    SELECT  a.keyID,
            count(b.keyID) AS countOccurencies
    FROM    `keywords - occurencies` a
            LEFT JOIN `keywords` b
                ON a.keyID = b.keyID
    WHERE   a.keyID IN ( 1, 2, 3 ) AND 
            DATE BETWEEN '2013/01/25' AND '2013/01/27'
    GROUP   BY a.keyID
    

    UPDATE 1

    Based on the example records, you need to do the following,

    • interchange the tableNames
    • put this condition DATE BETWEEN '2013-01-25' AND '2013-01-27' on the ON clause of join.
    • (optional) use ALIAS so you can get rid of the backticks other than tableNames

    query,

    SELECT  a.keyID,
            count(b.keyID) AS countOccurencies
    FROM    `keywords` a
            LEFT JOIN `keywords-occurencies` b
                ON a.keyID = b.keyID AND
                   b.DATE BETWEEN '2013-01-25' AND '2013-01-27'
    WHERE   a.keyID IN ( 1, 2, 3 ) 
    GROUP   BY a.keyID
    
    • SQLFiddle Demo
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The sql statement below will not run in SQLite: select * from A left
I'm trying to do the below sql statement in GORM select * from table1
I want to write an SQL statement like below: select * from tbl where
Can anyone please explain the below SQL statement from Oracle DB: select decode(TRIM(to_char(SYSDATE,'Day')),'Monday','3','1') from
I am running the following SQL statement SELECT su.Id ,rp.QuestionId ,rp.AnswerId FROM SiteUser su
empty (not null!)I want to create sql statement as this: SELECT * FROM table
I tried the below sql query INSERT INTO test.ACT_QUERY values(2139,2,'SELECT ''D'',ORDER_ID from dual'); When
If i have a parameterized SQL statement like this: SELECT * FROM table WHERE
I have issued the SQL statement below : SELECT GATNR ,GITNO ,GBANO ,MAX (CASE
In the below sql statement i get the following error Cannot use an aggregate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.