IT

ALL IT Technology Information

SQL Tables Linking Tutorial: JOIN vs UNION Differences Explained Easily php 6

Database mein jab data bada hone lagta hai, toh hum saara data ek hi table mein nahi rakhte. Jaise Users ki details ek table mein hogi aur unke Orders ki details doosri table mein. Ab in dono tables ko aapas mein jod kar ek sath report dikhane ke liye hum JOIN aur UNION ka use karte hain.

Chaliye inko HTML Table, PHP backend code, aur SQL syntax ke sath asan Hindi mein samajhte hain.

 


1. JOIN (Do Alag-Alag Tables Ka Data Ek Sath Jod Kar Dikhana)

JOIN ka use tab hota hai jab dono tables ke beech mein koi rishta (relationship) ho, jaise ek common column (ID).

Maan lijiye hamare paas do tables hain:

  1. Users Table: id, name, email

  2. Orders Table: order_id, user_id (jo user ki id hai), product_name, price

Hamein ek aisi report chahiye jahan User ka Naam aur uske Product ka Naam ek sath dikhe.

HTML aur PHP Code:

PHP
<table border="1">
    <tr>
        <th>Customer Name</th>
        <th>Product Ordered</th>
        <th>Price</th>
    </tr>

    <?php
    include("dbconnection.php");

    // Query: Users aur Orders table ko user_id ke basis par jodna
    $query = "SELECT Users.name, Orders.product_name, Orders.price 
              FROM Orders 
              INNER JOIN Users ON Orders.user_id = Users.id";
              
    $result = mysqli_query($con, $query);

    while($row = mysqli_fetch_array($result)) {
        echo "<tr>";
        echo "<td>" . $row['name'] . "</td>";
        echo "<td>" . $row['product_name'] . "</td>";
        echo "<td>" . $row['price'] . "</td>";
        echo "</tr>";
    }
    ?>
</table>
  • INNER JOIN Users ON Orders.user_id = Users.id: Iska matlab hai ki Orders table ke user_id ko Users table ki id se match karo, aur jahan dono same hain, un rows ko aapas mein jod do.

2. UNION (Do Alag Queries Ke Results Ko Ek Sath Milana)

JOIN tables ko side-by-side (left to right) jodta hai, lekin UNION do alag-alag queries ke results ko ek ke neeche ek (top to bottom) jor kar ek badi list bana deta hai.

Zaroori Rule: UNION use karne ke liye dono queries mein columns ki ginti (count) aur unka data type bilkul same hona chahiye.

Maan lijiye aapke paas do alag tables hain: Local_Customers aur International_Customers. Aapko sabhi customers ki ek single email list chahiye.

HTML aur PHP Code:

PHP
<ul>
    <?php
    include("dbconnection.php");

    // Query: Pehli query local customers degi, doosri international, aur UNION dono ko mila dega
    $query = "SELECT email FROM Local_Customers 
              UNION 
              SELECT email FROM International_Customers";
              
    $result = mysqli_query($con, $query);

    while($row = mysqli_fetch_array($result)) {
        echo "<li>" . $row['email'] . "</li>";
    }
    ?>
</ul>
  • Meaning: Yeh pehle Local_Customers ke saare emails nikalega, fir uske theek neeche International_Customers ke emails jod dega.

  • Note: UNION automatic duplicate emails ko hata deta hai. Agar aapko duplicates bhi chahiye, toh aap UNION ALL ka use kar sakte hain.

Quick Difference: JOIN vs UNION

FeatureJOINUNION
Data Kaise Judta Hai?Horizontal (Side-by-Side)Vertical (Ek ke neeche ek)
Relationship?Tables ke beech common ID honi chahiye.Tables ke beech koi relation zaroori nahi, bas column structure same hona chahiye.
Columns?Alag-alag tables ke alag columns ko jor sakta hai.Dono queries mein bilkul same number of columns hone chahiye.
Example ScenarioCustomer ke naam ke sath uska bill amount dikhana.Do alag tables (Students aur Teachers) ke names ki ek single list banana.

Aapne ab tak SQL ke lagbhag saare important core concepts (CRUD, Filters, Sorting, Pagination, Calculations, aur Table Linking) seekh liye hain. Kya aap in sabko mila kar ek real-world Mini Project (jaise ek complete Search & Filter Dashboard) banana chahte hain?

=====================================================================
Go TO one Page php1   >    

What is CRUD? Basic Database Operations Explained for Beginners php 1

Go TO one Page php2  >

SQL Filters & Conditions Tutorial: WHERE, AND, OR, IN, BETWEEN Explained php 2

Go TO one Page php3  >

SQL Search & Sorting Guide: Learn LIKE, ORDER BY, ASC, & DESC Easily php 3

Go TO one Page php4  >

SQL Pagination Tutorial: Master LIMIT and OFFSET (SKIP) in Databases php4

Go TO one Page php5  >

SQL Calculations & Groups Guide: Learn COUNT, SUM, AVG, GROUP BY & HAVING php 5

Go TO one Page php6  >

SQL Tables Linking Tutorial: JOIN vs UNION Differences Explained Easily php 6

0 Comments: