Aapne SQL ke sabse zaroori hisse ko touch kiya hai—Filters and Conditions. Jab database mein hazaron ya lakhon rows hoti hain, toh unme se sahi data chun kar nikalne ke liye hum inhi commands ka use karte hain.
Chaliye inko hum HTML Form, PHP code aur SQL syntax ke sath bilkul asan Hindi mein samajhte hain.
Maan lijiye hamare paas ek Products naam ki table hai, jisme columns hain: name (naam), category (shreni), price (keemat), aur stock.
1. WHERE (Specific Data Filter Karne Ke Liye)
WHERE ka kaam hai pure database mein se kisi ek specific shart (condition) ke hisab se data dhoondhna.
HTML Form & PHP Code:
<form action="filter.php" method="GET">
<input type="text" name="search_category" placeholder="Category likhein (e.g. Mobile)">
<button type="submit">Dhoondhein</button>
</form>
// filter.php
$category = $_GET['search_category'];
// Syntax: SELECT * FROM table WHERE column = 'value';
$query = "SELECT * FROM Products WHERE category = '$category'";
Meaning: Yeh query database se sirf wahi products layegi jinki category user ke likhi hui category se match karegi (jaise sirf 'Mobile').
2. AND (Dono Condition Match Karne Ke Liye)
Jab aapko do ya do se zyada shartein lagani hon, aur aap chahte hain ki sari shartein sach (true) hon, tab AND use hota hai.
HTML Form & PHP Code:
<form action="filter_and.php" method="GET">
<input type="text" name="category" placeholder="Category">
<input type="number" name="max_price" placeholder="Max Price">
<button type="submit">Filter</button>
</form>
// filter_and.php
$cat = $_GET['category'];
$price = $_GET['max_price'];
// Query jahan dono conditions sahi honi chahiye
$query = "SELECT * FROM Products WHERE category = '$cat' AND price <= '$price'";
Meaning: Yeh sirf wahi products dikhayega jo 'Electronics' ho AUR jinki keemat 500 se kam ho. Agar ek bhi shart galat hui, toh product nahi dikhega.
3. OR (Koi Ek Condition Match Karne Ke Liye)
OR ka matlab hai "ya toh yeh, ya toh wo". Agar lagayi gayi conditions mein se koi ek bhi shart sahi ho jati hai, toh data show ho jayega.
HTML Form & PHP Code:
<form action="filter_or.php" method="GET">
<input type="text" name="search_term" placeholder="Naam ya Category likhein">
<button type="submit">Search</button>
</form>
// filter_or.php
$search = $_GET['search_term'];
// Dono mein se koi ek bhi match mil gaya toh data aa jayega
$query = "SELECT * FROM Products WHERE name = '$search' OR category = '$search'";
Meaning: Agar user ne 'Laptop' likha, toh query check karegi: kya naam 'Laptop' hai? YA kya category 'Laptop' hai? Dono mein se jo bhi sahi hoga, wo product screen par aa jayega.
4. IN (Multiple Fixed Options me se Filter Karne ke Liye)
Jab aapko ek hi column ke liye kai saari fixed choices (options) check karni hon, toh bar-bar OR likhne ke bajaye hum IN ka use karte hain. Isme hum ek list de dete hain.
HTML Form & PHP Code:
<form action="filter_in.php" method="POST">
<input type="checkbox" name="brands[]" value="Apple"> Apple <br>
<input type="checkbox" name="brands[]" value="Samsung"> Samsung <br>
<input type="checkbox" name="brands[]" value="Sony"> Sony <br>
<button type="submit">Filter Brands</button>
</form>
// filter_in.php
if(isset($_POST['brands'])) {
// Array ko comma-separated string mein badalna (e.g., 'Apple', 'Sony')
$brand_list = implode("','", $_POST['brands']);
// Syntax: WHERE column IN ('val1', 'val2', 'val3')
$query = "SELECT * FROM Products WHERE brand IN ('$brand_list')";
}
Meaning: Agar user ne Apple aur Sony par tick kiya hai, toh query banegi
WHERE brand IN ('Apple', 'Sony'). Yeh sirf inhi dono brands ke items dikhayega.
5. BETWEEN (Price ya Date ki Range Filter Karne Ke Liye)
Jab aapko kisi data ko ek min (kam) aur max (zyada) limit ke beech mein se filter karna ho, tab BETWEEN sabse best hai. Yeh aksar keemat (Price) ya Tarikh (Date) ke sath use hota hai.
HTML Form & PHP Code:
<form action="filter_between.php" method="GET">
<input type="number" name="min_price" placeholder="Min Price">
<input type="number" name="max_price" placeholder="Max Price">
<button type="submit">Price Filter</button>
</form>
// filter_between.php
$min = $_GET['min_price'];
$max = $_GET['max_price'];
// Syntax: WHERE column BETWEEN value1 AND value2
$query = "SELECT * FROM Products WHERE price BETWEEN '$min' AND '$max'";
Meaning: Agar min '1000' aur max '5000' hai, toh yeh query un sabhi products ko nikalegi jinki keemat 1000 se 5000 ke beech mein hai (1000 aur 5000 ko mila kar).
Quick Revision Table (Aapki Pehchan Ke Liye)
| Operator | Simple Hindi Meaning | Example Scenario |
| WHERE | Jo is shart ko poora kare | WHERE id = 5 (Sirf ID 5 ka data) |
| AND | Yeh BHI aur Woh BHI (Dono zaroori) | WHERE status='active' AND role='admin' |
| OR | Ya toh yeh, ya toh woh (Koi ek chalega) | WHERE city='Delhi' OR city='Mumbai' |
| IN | Is poori list mein se koi bhi ho | WHERE status IN ('Pending', 'Approved') |
| BETWEEN | Yahan se lekar wahan tak (Range) | WHERE age BETWEEN 18 AND 30 |
0 Comments: