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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:37:15+00:00 2026-05-15T15:37:15+00:00

i was looking for a way to combine different mysql queries in a php

  • 0

i was looking for a way to combine different mysql queries in a php file so this is my code :

 $sql_query = "SELECT b.*, 
u.username AS MY_Sender
  FROM table_users u, 
       table_blogs b  
 WHERE b.reciever = '0'  
   AND 
   u.user_id = b.sender  

UNION  

SELECT b.*,
u2.username AS MY_Recipient
  FROM table_users u2, 
       table_blogs b  
 WHERE b.reciever != '0'  
  AND 
  u2.user_id = b.reciever  
";

this code works fine unless it cant fetch MY_Recipient

in the above code i need to fetch both sender of blog post and the receiver

is it wrong to use Union to do so ?!

  • 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-15T15:37:15+00:00Added an answer on May 15, 2026 at 3:37 pm

    I have made a guess at your table structure, and produced something similar. Right or wrong, it might at least help arrive at a suitable solution for you.

    Two tables, users and blogs:

    CREATE TABLE `users` (
        `id` int(11) NOT NULL auto_increment,
        `username` varchar(255) NOT NULL,
        PRIMARY KEY  (`id`)
    ) ENGINE=InnoDB;
    
    CREATE TABLE `blogs` (
        `id` int(11) NOT NULL auto_increment,
     `sender` int(11) NOT NULL,
     `receiver` int(11) NOT NULL,
        PRIMARY KEY  (`id`)
    ) ENGINE=InnoDB;
    

    Add some users:

    INSERT INTO `users` (username) VALUES
    ('Alice'), ('Bob'), ('Carol'), ('Eve');
    

    Add blog entries for some users:

    INSERT INTO `blogs` (sender, receiver) VALUES
    (1,2), (2,1), (3,4), (4,3), (1,4), (4,1);
    

    For each blog entry, list the sender and receiver:

    SELECT
        b.id,
     b.sender AS sender_id,
     b.receiver AS receiver_id,
        us.username AS sender_name,
        ur.username AS receiver_name
    FROM blogs AS b
    JOIN users AS us ON us.id = b.sender
    JOIN users AS ur ON ur.id = b.receiver
    ORDER BY b.id;
    
    +----+-----------+-------------+-------------+---------------+
    | id | sender_id | receiver_id | sender_name | receiver_name |
    +----+-----------+-------------+-------------+---------------+
    |  1 |         1 |           2 | Alice       | Bob           |
    |  2 |         2 |           1 | Bob         | Alice         |
    |  3 |         3 |           4 | Carol       | Eve           |
    |  4 |         4 |           3 | Eve         | Carol         |
    |  5 |         1 |           4 | Alice       | Eve           |
    |  6 |         4 |           1 | Eve         | Alice         |
    +----+-----------+-------------+-------------+---------------+
    

    UPDATE 1
    table_blogs should probably look like this:

    CREATE TABLE IF NOT EXISTS `table_blogs` (
      `bid` int(10) NOT NULL AUTO_INCREMENT,
      `content` varchar(255) DEFAULT NULL,
      `date` varchar(14)  DEFAULT NULL,
      `sender` int(10) NOT NULL,
      `reciever` int(10) NOT NULL,
      CONSTRAINT `fk_sender`
        FOREIGN KEY (`sender` )
        REFERENCES `table_users` (`id` )
        ON DELETE CASCADE
        ON UPDATE CASCADE,
      CONSTRAINT `fk_receiver`
        FOREIGN KEY (`receiver` )
        REFERENCES `table_users` (`id` )
        ON DELETE CASCADE
        ON UPDATE CASCADE,
      PRIMARY KEY (`bid`)
    ) ENGINE=MyISAM AUTO_INCREMENT=1 ;
    

    The CONSTRAINT clauses will prevent inserting values for users which don’t exist, and will delete entries when users are deleted from the user table.

    UPDATE 2
    I think this is what you want, but as KM and bobince have stated in the comments, it violates foreign key constraints, which is not really a good idea. So, assuming no foreign key constraints, here’s some additional inserts and a modified query:

    INSERT INTO `blogs` (sender, receiver) VALUES
    (1,0), (0,1), (4,0), (0,4), (2,0), (0,2);
    
    SELECT
        b.id,
     b.sender AS sender_id,
     b.receiver AS receiver_id,
        IFNULL(us.username, ur.username) AS sender_name,
        IFNULL(ur.username, us.username) AS receiver_name
    FROM blogs AS b
    LEFT JOIN users AS us ON us.id = b.sender
    LEFT JOIN users AS ur ON ur.id = b.receiver
    ORDER BY b.id;
    
    +----+-----------+-------------+-------------+---------------+
    | id | sender_id | receiver_id | sender_name | receiver_name |
    +----+-----------+-------------+-------------+---------------+
    |  1 |         1 |           2 | Alice       | Bob           |
    |  2 |         2 |           1 | Bob         | Alice         |
    |  3 |         3 |           4 | Carol       | Eve           |
    |  4 |         4 |           3 | Eve         | Carol         |
    |  5 |         1 |           4 | Alice       | Eve           |
    |  6 |         4 |           1 | Eve         | Alice         |
    |  7 |         1 |           0 | Alice       | Alice         |
    |  8 |         0 |           1 | Alice       | Alice         |
    |  9 |         4 |           0 | Eve         | Eve           |
    | 10 |         0 |           4 | Eve         | Eve           |
    | 11 |         2 |           0 | Bob         | Bob           |
    | 12 |         0 |           2 | Bob         | Bob           |
    +----+-----------+-------------+-------------+---------------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for way to PHP to detect if a script was run from
I'm looking for a way to stream a PDF file from my server to
I looking for a way, specifically in PHP that I will be guaranteed to
I'm looking for a way to poll different servers and check that SQL server
I'm looking for a way to delete a file which is locked by another
Good Morning, I was looking for a way to combine two integers to create
I am using the jquery-ui-dialog plugin I am looking for way to refresh the
I looking for a way to programmatically start a VOIP call using the SIP
I'm looking for some way to effectively hide inherited members. I have a library
I'm looking for any way that I can improve the selector performance of a

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.