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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:15:20+00:00 2026-05-23T07:15:20+00:00

I have to combine these two mySQL queries into one. I duplicated a solution

  • 0

I have to combine these two mySQL queries into one. I duplicated a solution and used it on a join table. I am querying a join table that has two columns (labeled “to_” and “from_”). Both ‘to_’ and ‘from_’ hold an id number for the same table. I need to combine these queries in such a way that I get results based on: [(‘from_’ + ‘to_’) > 3], where ‘from_’ and ‘to_’ have the same value (i.e., they refer to the same id).

$query = "select * from nodes where nodeID in (
    select to_ from joinTable group by to_ having count(*) > 3
)";

...
$query = "select * from nodes where nodeID in (
    select from_ from joinTable group by from_ having count(*) > 3
)";

Acknowledgement: I’m using a query based very closely on a solution ‘Mr E’ helped me with earlier.

  • 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-23T07:15:20+00:00Added an answer on May 23, 2026 at 7:15 am

    You can try (see important notice at the last paragraph regarding to_ and from_ matching requirements):

    SELECT X.to_, COUNT(*) 
    FROM joinTable X, joinTable Y
    WHERE
        X.to_ = Y.from_
    GROUP BY X.to_
    HAVING COUNT(*) > 2
    

    Or

    SELECT X.to_, COUNT(*) 
    FROM joinTable X LEFT JOIN joinTable Y ON X.to_ = Y.from_
    GROUP BY X.to_
    HAVING COUNT(*) > 2
    

    Using Mr E‘s test data:

    CREATE TABLE `foo` (
      `id` int(1) DEFAULT NULL,
      `to_` varchar(5) DEFAULT NULL,
      `from_` varchar(5) DEFAULT NULL
    );
    
    INSERT INTO `foo` VALUES (1,'A','B'),(2,'B','A'),(3,'B','C'),(4,'X','C'),(4,'X','B');
    

    It will work, half-way, by issuing:

    SELECT X.to_, Y.from_
    FROM foo X LEFT JOIN foo Y ON X.to_ = Y.from_
    

    which will then yield:

    mysql> SELECT X.to_, Y.from_ /*--, COUNT(*) */
        -> FROM foo X LEFT JOIN foo Y ON X.to_ = Y.from_;
    +------+-------+
    | to_  | from_ |
    +------+-------+
    | A    | A     | 
    | B    | B     | 
    | B    | B     | 
    | B    | B     | 
    | B    | B     | 
    | X    | NULL  | 
    | X    | NULL  | 
    +------+-------+
    7 rows in set (0.00 sec)
    

    and by running in full:

    mysql> SELECT X.to_, COUNT(*)
        -> FROM foo X LEFT JOIN foo Y ON X.to_ = Y.from_
        -> GROUP BY X.to_
        -> HAVING COUNT(*) > 2;
    +------+----------+
    | to_  | COUNT(*) |
    +------+----------+
    | B    |        4 | 
    +------+----------+
    

    Basically, join the table with itself and then generate an N:N list of matching records from both tables where to_ and from_ match (whether or not on the same row), then work with a single column and aggregate its values for the final COUNT(*).

    And, most importantly, why have I lowered the number on the HAVING COUNT(*) from 3 to 2? The N:N relationship will issue N1 * N2 records (where N1 is the count of matching records on the first table and N2 on the second). So if the lower bound is three, we can only have over 3 records on these two tables by having one record in one of them and then 3 on the other (in whatever order) or 2 in one and 2 on the other (and then up from there) – otherwise there will be no matches on the to_ and from_ fields and this is something I am not sure about – whether the OP wants only records whose values appear on both fields or if having a COUNT(*) from a single side would suffice. If the latter is the case, however, I don’t see any other option apart from separating the queries to deal with each column individually, as some people already have posted since that’s an isolated sum we’re dealing with. This will be slow if running against large tables.

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

Sidebar

Related Questions

How can I combine these two functions into one recursive function to have this
I have 3 byte arrays in C# that I need to combine into one.
I have two mysql queries: $sql = SELECT * FROM content WHERE threadName LIKE
I am writing my first little Access 2003 application. I have two queries that
I have a script that appends some rows to a table. One of the
I have a PHP/MySQL e-commerce site that is placing order detail information into a
So I have been trying to combine the answers of these two questions: C#
I have some old C code that I would like to combine with some
I have a MySql database with three tables I need to combine in a
Say i have a table named tbl1 in mysql :- bookid int name varchar(20)

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.