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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:00:34+00:00 2026-06-13T09:00:34+00:00

I have this table: id | id_user | id_user_stalkers | date 0 | 0222222

  • 0

I have this table:

id  | id_user  |  id_user_stalkers | date
0   |  0222222 |  032332         | 32234234

so, i want to know count of all users that are in my friends list, and cout of all users that have me in their friend list.
at the moment, i’ve made this query :

SELECT (
                    SELECT COUNT(id_user)
                    FROM   stalkers WHERE id_user =  ".$id."
                    ) AS user_stalkers,
                    (
                    SELECT COUNT(id_user_stalkers)
                    FROM   stalkers WHERE id_user_stalkers = ".$id."
                    ) AS user_is_stalked
            FROM stalkers  

but it returns this:

Array
(
    [0] => Array
        (
            [user_stalkers] => 7
            [user_is_stalked] => 2
        )

    [1] => Array
        (
            [user_stalkers] => 7
            [user_is_stalked] => 2
        )

    [2] => Array
        (
            [user_stalkers] => 7
            [user_is_stalked] => 2
        )

    [3] => Array
        (
            [user_stalkers] => 7
            [user_is_stalked] => 2
        ))

it’s all right, but i need only one row, not four.

Can somebody help me please ?

  • 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-13T09:00:35+00:00Added an answer on June 13, 2026 at 9:00 am

    Since all your fields are subqueries you can drop the from clause altogether. Otherwise you get a subquery for every single row in the table. This not only gets the results you want but reduces total hit on the database by a potentially very large amount (increasing performance).

    SELECT 
    (
     SELECT COUNT(id_user)
     FROM   stalkers WHERE id_user =  ".$id."
    ) AS user_stalkers,
    (
     SELECT COUNT(id_user_stalkers)
     FROM   stalkers WHERE id_user_stalkers = ".$id."
    ) AS user_is_stalked
    

    If this is going to be a potentially large table you’ll want to make sure id_user and id_user_stalkers are both indexed. If an index can be used these sub-queries will be much much faster.

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

Sidebar

Related Questions

I have this mysql orders table: (id,user_id,skin,item,date,reference,price,to_who) I need to select all users, with
i have this table: post{id, user_id, event_id} i want to fetch all posts thats
I have this table for users that stores their usernames and other data, thats
If i have this structure: table users id_user 1 2 table lists id_lists name_list
I have this table: User id INT PK login VARCHAR UNIQUE I want to
let's assume I have this numrequest table. it has these fields: id , id_user
Hi i have database like this Users Table user_id | username | branch_id(foreignkey)| Approved_id(foreignkey)
i have a table like this: ID ID_USER SOURCE CONTENT or just: CREATE TABLE
i have 4 tables: questions, answers, users, and inquiries (relationship-table between all of them)
I have this table on MySQL. I want to query the port number for

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.