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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:31:10+00:00 2026-06-11T07:31:10+00:00

I was listing this two queriess as separate, but now I need the results

  • 0

I was listing this two queriess as separate, but now I need the results in the same list.

Right now I have it ‘fixed’ by ordering the items by id using javascript. But it would be great to get this data merged from the server so I would be able to page the results..

DATABASE SCHEMA

Table Friends:

enter image description here

Table helps

enter image description here

Query A (returns results from the current user)

   $sql = 'SELECT * FROM helps WHERE id_user ='.$value;

Query B (returns results from the current user’s friends)

$sql = 'SELECT
    h.*,
    f.* 
    FROM (
            SELECT
                     id  AS friendsId,
                     CASE followerid WHEN '.$value.' THEN followingid ELSE followerid END AS friend_id
                        FROM friends
                        WHERE acepted = 1
                        AND (followerid  = '.$value.' OR followingid = '.$value.')
                    ) AS f
                        INNER JOIN helps AS h ON h.id_user = f.friend_id
                        ORDER BY h.id DESC';

Is there a way to merge these queries? I honestly have no clue how to.

-EDIT-

Considering union, but I don’t know how to handle the order by thing..

$sql = '(SELECT * FROM helps WHERE id_user = '.$value.')
                    UNION
                    (SELECT
                                h.*,
                                f.* 
                            FROM (
                                SELECT
                                    id  AS friendsId,
                                    CASE followerid WHEN '.$value.' THEN followingid ELSE followerid END AS friend_id
                                FROM friends
                                WHERE acepted = 1
                                AND (followerid  = '.$value.' OR followingid = '.$value.')
                            ) AS f
                                INNER JOIN helps AS h ON h.id_user = f.friend_id
                                ORDER BY h.id DESC)';

As you can see in the second query helps table has been renamed to h. How can I do the same in the first? (both queries return the data in the same format)

  • 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-11T07:31:11+00:00Added an answer on June 11, 2026 at 7:31 am

    The basic rules of UNION are

    • the number of columns returned by each query must be equal and
    • the data types must match with one another.

    As you can see, i haven’t modified the queries you formulated. Only that I added a Virtual Column for each query. This column will be used for ordering all the records. The queries were wrap by another select statment so the virtual column won’t show on the result list but it will be the basis for ordering the records. The result of the query below contains the records of the user at the top most (because of ORDER BY OrderBy ASC) and the followed by the records of the user’s friends sorted by friend’s ID. If any clarification you want, please do inform me. One more thing, I removed f.* in your second query because it is not necessary and can cause error on your syntax due to column (number) mismatched.

    SELECT  ID, Title, Content, ID_User, ID_Group, 
            ID_Type, ID_Loc, Avatar, Attached, Fecha, Likes, 
            Lan, NeedsCount, RecivedCount
    FROM
        (
            SELECT *, 1 as OrderBy           -- first query you formulated
            FROM helps 
            WHERE id_user = '.$value.'
            UNION
            SELECT  h.*, 2 as OrderBy        -- second query you formulated
            FROM    (
                        SELECT  id  AS friendsId,
                                CASE followerid 
                                    WHEN '.$value.' THEN followingid 
                                    ELSE followerid 
                                END AS friend_id
                        FROM    friends
                        WHERE   acepted = 1 AND 
                                (followerid  = '.$value.' OR followingid = '.$value.')
                    ) AS f
                        INNER JOIN helps AS h 
                            ON h.id_user = f.friend_id
        ) x
    ORDER BY OrderBy ASC, ID DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query: @listings = Listing.where('city != ?', 'SommerVille') I was wondering if
I'm using this codes for listing custom field values into drop-down list element on
I have this simple code in my user_controller.rb file #listing all users def index
This is simple, but I am taking an entire directory listing (in PHP with
Recently I experienced this weird problem: while(list($key, $value) = each($array)) was not listing all
I have been racking my brains over this for a while now. Here is
I have two tables. One is a listing of products, and one is a
I have a django application which has two models like this: class MyModel(models.Model): name
I have two tables set up. One for student information, one for student/class listing.
I have two datasets listing the average voltage outputs of two assemblies of neural

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.