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 7278205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:50:35+00:00 2026-05-28T22:50:35+00:00

I am trying to retrieve a few things from the database with just one

  • 0

I am trying to retrieve a few things from the database with just one single query:
– the number of friend requests
– the number of new private messages
– the number of notifications
– and username of the user who eg. sent the PM to you or invited you to friends.

The user id specified below in the query is the user who is checking to see if there are any notifications for this user.

The problem is: if there are no notifications it will not return the number of friend requests or private messages as well.

This is the query I have build, and it works apart from the problem above:

SELECT 
    n.id, 
    n.type,
    u.username,
    COUNT(fr.friend_id) as frc,
    COUNT(pm.pm_id) as pmc
FROM 
    notifications as n,
    users as u
LEFT JOIN
    private_messages as pm
ON
    pm.to_user = '1'
AND 
    pm.status = 'unread'
LEFT JOIN
    friends as fr
ON  
    fr.friend_id = '1'
AND
    fr.status = 'pending'
WHERE 
    n.user_id='1'
AND
    u.id = n.from_id
ORDER BY
    n.id ASC

I am not sure what I am missing, so if anyone can share their sql knowledge and help me out I would deeply appreciate it.

Thanks!

Ps. I am still very new to more complex queries, so if I am completely off track with my above query, please do let me know :).

Table Structures:

Sample friends table structure
+--------------------------+-----------------------------+
| Field                    | Type                        |
+--------------------------+-----------------------------+
| user_id                  | int(10)                     |
| friend_id                | int(10)                     |
| status                   | ENUM('pending','accepted')  |
| user_id                  | int(10)                     |
+--------------------------+-----------------------------+

Sample notifications table structure
+--------------------------+---------------------+
| Field                    | Type                |
+--------------------------+---------------------+
| id                       | int(10)             |
| type                     | ENUM('pm','friend') |
| user_id                  | int(10)             |
| from_id                  | int(10)             |
+--------------------------+---------------------+

Sample private message table structure
+--------------------------+-----------------------+
| Field                    | Type                  |
+--------------------------+-----------------------+
| pm_id                    | int(12)               |
| to_user                  | int(10)               |
| from_user                | int(10)               |
| status                   | ENUM('read','unread') |
+--------------------------+-----------------------+

Sample users table structure
+--------------------------+---------------+
| Field                    | Type          |
+--------------------------+---------------+
| id                       | int(10)       |
| username                 | varchar(50)   |
+--------------------------+---------------+
  • 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-28T22:50:36+00:00Added an answer on May 28, 2026 at 10:50 pm

    Your query mixes up unrelated things – a list of notifications, and a scalar set of counts. It also ties whether or not you get any results to the requirement to have at least one notification. From a design standpoint, these two should not be mixed, so I would re-write it as two queries.

    You should use a correlated subquery for your counts:

    select
        u.username,
        (select COUNT(fr.friend_id) from friends as fr where fr.friend_id = u.id AND fr.status = 'pending') as frc,
        (select COUNT(pm.pm_id) from private_messages as pm where pm.to_user = u.id AND pm.status = 'unread') as pmc
    from users u
    where u.id='1'
    

    The query for notifications is a simplified version of your original query:

    SELECT 
        n.id, 
        n.type,
        u.username
    FROM 
        users as u
    LEFT JOIN
        notifications as n ON n.from_id=u.id
    WHERE
        u.id = '1'
    ORDER BY
        n.id ASC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Iam trying to retrieve data from mysql database into stylesheet.php but it is not
I was trying to retrieve value from two tables by joining, but few values
I'm trying to retrieve a file from a server using SFTP (as opposed to
I'm trying to retrieve an XML stream from a URL. For most URLs my
I'm trying to retrieve data from an SQL Server 2000 server, and place into
I'm trying to retrieve the SubItem in my ListView from another thread, but I
I am trying to retrieve the correct value from an ArrayList of objects (.NET
I'm trying to retrieve binary data using AS3 from a PHP server via HTTP,
I am trying to retrieve all cards from Mongoid that have request=preview I'm doing
I have been trying to figure out how to retrieve (quickly) the number of

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.