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:
UsersTable:id,name,emailOrdersTable: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:
<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
Orderstable keuser_idkoUserstable kiidse 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:
<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_Customerske saare emails nikalega, fir uske theek neecheInternational_Customerske emails jod dega.Note:
UNIONautomatic 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
| Feature | JOIN | UNION |
| 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 Scenario | Customer 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?
0 Comments: