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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:52:35+00:00 2026-06-17T00:52:35+00:00

I am have two tables which I have left joined like so below to

  • 0

I am have two tables which I have left joined like so below to show me the pads by id added in descending order which works fine.

SELECT p.* FROM ruj_users_pad 
AS p LEFT JOIN ruj_users
AS u ON p.user_id = u.id 
WHERE u.status !=0 
AND 1 
AND p.status = 1 
GROUP BY p.added_date
DESC LIMIT 0, 20

However, now I would like to retrieve a third table which also as an ‘added_date’ column combine it with the previous query to show the new descending order. The data from the third table is a generated from a button when users click to favorite the current item on the pad.

Here is what I have but it is not working.

SELECT p.*,f.added_date FROM ruj_users_pad
AS p LEFT JOIN ruj_users 
AS u ON b.user_id=u.id 
LEFT JOIN ruj_users_fave 
AS f ON f.brag_id = u.id 
WHERE u.status !=0 
AND 1 
AND p.status = 1 
GROUP BY f.added_date DESC, b.added_date DESC 
LIMIT 0, 20

The result returns the same as the first result. I don’t understand what could be wrong. I would like the result to take into consideration that there is an entry in the ruj_users_fave and combine it with the first result to bring the favorited pad to the top.

Help is greatly appreciated.

  • 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-17T00:52:36+00:00Added an answer on June 17, 2026 at 12:52 am

    Here is a solution to your problem using UNION to merge two subqueries. It is not the full query of three tables but only the essence with two tables to demonstrate the concept. I use a table pad and a table likes. The first subquery selects the pad’s id and its most recent “like” using MAX together with GROUP BY id. Note that I rename the column MAX(liked) to timestamp.

    SELECT pad.id, MAX(liked) AS timestamp FROM
    pad INNER JOIN likes WHERE likes.pad_id = pad.id
    GROUP BY pad.id
    

    The other subquery selects the pad ids that were not liked yet, i.e. no record in the likes table exists for that specific pad id. In this case, you mention that the pad’s creation date should be used for determining the order of records returned. Thus, we rename added to timestamp.

    SELECT id, added AS timestamp FROM pad
    WHERE NOT EXISTS
      (SELECT liked FROM likes WHERE likes.pad_id = pad.id)
    

    We have two subqueries now that return the pad’s id and the timestamp so we can combine them to a single result set using UNION, and then ORDER it BY timestamp. The combined query for you looks like this.

    SELECT combined.id, combined.timestamp FROM
    (
      SELECT pad.id, MAX(liked) AS timestamp FROM
        pad INNER JOIN likes WHERE likes.pad_id = pad.id
        GROUP BY pad.id
      UNION
      SELECT id, added AS timestamp FROM pad
        WHERE NOT EXISTS
          (SELECT liked FROM likes WHERE likes.pad_id = pad.id)
    )
    combined ORDER BY combined.timestamp DESC;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables which I want to join together using a left outer
I have two tables which looks something like this Table Queue int ID; string
I have two tables, tblClient and tblBill which are joined on cltID. Some cltID
i have two mysql tables which are linked together and i need to show
I have two tables which I am left joining together. SELECT Col3, tab1.Col1, tab1.Col2
I have two tables which are used to store details of different types of
I have two tables which have some transactional stuff stored in them. There will
I use Hibernate with Java. I have two tables which are associated with foreign
I have two different tables from which I need to pull data blogs which
I have two or more tables which hold values under the same column name.

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.