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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:38:37+00:00 2026-06-05T05:38:37+00:00

i have these subqueries in a main query used to fetch some events: SELECT

  • 0

i have these subqueries in a main query used to fetch some events:

SELECT [...],
(SELECT COUNT(*) FROM WEventUser WHERE WEventUser.eID=e.eID AND favorited=1) as numfavorited,
(SELECT COUNT(*) FROM WEventUser WHERE WEventUser.eID=e.eID AND subscribed=1) as numsubscribed,
(SELECT COUNT(*) FROM WEventUser WHERE eID=e.eID AND WEventUser.uID=2 AND favorited=1) as favorited,
(SELECT COUNT(*) FROM WEventUser WHERE eID=e.eID AND WEventUser.uID=2 AND subscribed=1) as subscribed,
[...] WHERE...etc.

structure of WEventUser is quite simple

CREATE TABLE IF NOT EXISTS `WEventUser` (
  `eID` int(10) unsigned NOT NULL auto_increment,
  `uID` int(10) unsigned NOT NULL,
  `favorited` int(1) unsigned default '0',
  `subscribed` int(1) unsigned default '0',
  PRIMARY KEY  (`eID`,`uID`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

These subqueries are really expensive. Can you help me finding an alternative (like a single join)?

Thanks in advance!

EDIT:
I’m selecting from a main WEvents table that is:

CREATE TABLE IF NOT EXISTS `wevents` (
`eID` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `uID` int(10) unsigned DEFAULT NULL,
 `ecID` int(10) unsigned NOT NULL,
 `eName` varchar(64) NOT NULL,
 `eDescription` longtext,
 `eIsActive` varchar(1) NOT NULL DEFAULT '0',
 `eIsValidated` tinyint(4) NOT NULL DEFAULT '-1',
 `eDateAdded` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
 `eDateModified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
 PRIMARY KEY (`eID`,`ecID`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
  • 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-05T05:38:39+00:00Added an answer on June 5, 2026 at 5:38 am

    You shouldn’t use a subqueries, it is enough to count values in the COUNT function, e.g. –

    SELECT [...],
    COUNT(IF(wu.favorited = 1, 1, NULL)) as numfavorited,
    COUNT(IF(wu.subscribed = 1, 1, NULL)) as numsubscribed,
    COUNT(IF(wu.uID=2 AND wu.favorited=1, 1, NULL)) as favorited,
    COUNT(IF(wu.uID=2 AND wu.favorited = 1, 1, NULL)) as subscribed,
    [...]
    FROM
      WEventUser wu
    WHERE...etc.
    

    You can easily use this one if you want to join WEventUser with another table.

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

Sidebar

Related Questions

I have very long select query which i need to filter based on some
I have a query in MS Access which creates a table from two subqueries.
Lets say I have two subqueries: SELECT Id AS Id0 FROM Table0 => Id0
I have this query (notice that all 3 subqueries are selecting different columns from
I have HQL like this: from Table1 t1 where t1.name not in (select t2.name
I have the query below (shorted up all the fields in the select to
I have a SQL query that's fairly complicated and involves multiple subqueries and unions.
I have a CakePHP query that uses subqueries. While I've found documentation that shows
Is it possible to have subqueries in the select field of Doctrine_RawSql? $q->select('{t.*}, {i.*},
I have these 3 icons enclosed in separate DIVs all of which are enclosed

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.