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

  • SEARCH
  • Home
  • 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 5929361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:24:43+00:00 2026-05-22T14:24:43+00:00

I have a mySQL table called entry. It has 2 test records in it:

  • 0

I have a mySQL table called entry. It has 2 test records in it:

SELECT * FROM entry WHERE typeId = 2   

SELECT * FROM entry WHERE typeId = 2

I have different types. For example when typeId = 2 that is a question. When typeId = 3 that is an answer. When an answer is inserted into this table the linkId column gets populated with the entryId of the question it is answering.

For example:

SELECT * FROM entry WHERE typeId = 3

SELECT * FROM entry WHERE typeId = 3

As you can see both of these records are typeId = 3 and the linkId both point to entryId = 1.

In the following query I’m only getting the question that has answers associated to it.

SELECT SQL_CALC_FOUND_ROWS
entry.entryId, entry.entryText, entry.voteCount AS voteCount,
USER.userName, USER.fbId, COUNT(e2.entryId) AS answerCount
FROM entry
INNER JOIN USER ON entry.userId = USER.userId
INNER JOIN entry AS e2 ON e2.linkId = entry.entryId AND e2.active=1
WHERE entry.active = 1 
AND entry.typeId = 2
ORDER BY entry.voteCount DESC

above sql statement results

According to the above statements I have 2 entries that are of typeId = 2. So I would really like this statement to return both entries but obviously the answerCount for the entry with out any answers would be 0.

Does anyone have any ideas with what I’ve shown you to fix this query to get the desired results?

UPDATE:

Based off of the suggestion below. I changed the inner join to a left join:

SELECT SQL_CALC_FOUND_ROWS DISTINCT
entry.entryId, entry.entryText, entry.voteCount AS voteCount,
USER.userName, USER.fbId
FROM entry
INNER JOIN USER ON entry.userId = USER.userId
LEFT JOIN entry AS e2 ON e2.linkId = entry.entryId AND e2.active=1
WHERE entry.active = 1 
AND entry.typeId = 2
ORDER BY entry.voteCount DESC

In this statement I took out the count statement (COUNT(e2.entryId) AS answerCount).

This is the result:

SQL Left Join no COUNT

When I put in the COUNT statement it only returns the 1 record that returned when it was the inner join:

with COUNT and INNER JOIN


UPDATE:

I was able to get the rows to return properly by making sure the LEFT JOIN was correct and when I added a GROUP BY, then the row with 0 answers returned properly.

enter image description here

See my query below.

  • 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-05-22T14:24:43+00:00Added an answer on May 22, 2026 at 2:24 pm

    I actually got it to work…

    Here is my updated statement:

    SELECT  e1.entryId, e1.entryText, e1.voteCount AS voteCount,
    USER.userName, USER.fbId, COUNT(e2.linkId)
    FROM    entry e1 
    INNER JOIN USER ON e1.userId = USER.userId
    LEFT JOIN entry e2 
    ON  e2.linkId = e1.entryId
    WHERE e1.active = 1
    AND e1.typeId = 2
    GROUP BY e1.entryId
    ORDER BY e1.voteCount DESC
    

    I added the LEFT JOIN as suggested but I also made sure the join was correct and the final piece of the puzzle was the GROUP BY. Once I put that in, I got my 2 rows that I was originally looking for.

    enter image description here

    Thanks for all the help!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a table in MySQL called articles, that has a column
I have a MySQL table called items that contains thousands of records. Each record
I have a MySQL table that has about 30k records, of which 2k I
I have a mysql table called books that has a votes column, and another
I have the following mysql table called test. lets say it has the data
I have a mysql table called pollOfTheWeek. It has a column pollDate of type
Suppose I have a MySQL table called MyTable, that looks like this: +----+------+-------+ |
I have a MYSQL table called 'categories' from a project I inherited from someone
I have a MySQL table that I have imported from a CSV file. In
I have a MySQL table called bb_posts used by a bbPress forum. It has

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.