Created
September 12, 2017 06:06
-
-
Save Mauryashubham/e909dcf19f706ed2d0db96b0e23752ee to your computer and use it in GitHub Desktop.
SQL UNION OPERATOR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
@author : Shubham Maurya, | |
Email id : [email protected] | |
**/ | |
$DB_host = "localhost"; | |
$DB_user = "root"; | |
$DB_pass = ""; | |
$DB_name = "union"; | |
try | |
{ | |
$DBcon = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass); | |
$DBcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
// echo "Done.."; | |
} | |
catch(PDOException $e) | |
{ | |
echo "ERROR : ".$e->getMessage(); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
@author : Shubham Maurya, | |
Email id : [email protected] | |
**/ | |
require_once 'dbconfig.php'; | |
$email="[email protected]"; | |
try | |
{ | |
// UNION | |
$stmt=$DBcon->prepare("SELECT name FROM table_1 UNION SELECT name FROM table_2 ORDER BY name"); | |
$stmt->execute(); | |
if($stmt->rowcount()>0) | |
{ | |
echo "UNION<br><br>"; | |
while ($row=$stmt->FETCH(PDO::FETCH_ASSOC)) | |
{ | |
echo "Name : ".$row['name'].'<br>'; | |
} | |
} | |
// UNION ALL | |
$stmt1=$DBcon->prepare("SELECT name FROM table_1 UNION ALL SELECT name FROM table_2 ORDER BY name"); | |
$stmt1->execute(); | |
if($stmt1->rowcount()>0) | |
{ | |
echo "<br>"; | |
echo "UNION ALL<br> <br>"; | |
while ($row1=$stmt1->FETCH(PDO::FETCH_ASSOC)) | |
{ | |
echo "Name : ".$row1['name'].'<br>'; | |
} | |
} | |
// UNION USING WHERE CLAUSE | |
$stmt2=$DBcon->prepare("SELECT name,email FROM table_1 UNION SELECT name,email FROM table_2 WHERE email=:email"); | |
$stmt2->execute(array(':email' =>'[email protected]')); | |
if($stmt2->rowcount()>0) | |
{ | |
echo "<br>"; | |
echo "UNION USING WHERE CLAUSE<br> "; | |
while ($row2=$stmt2->FETCH(PDO::FETCH_ASSOC)) | |
{ | |
echo "<br>name : ".$row2['name'].'<br>'; | |
echo "email : ".$row2['email'].'<br>'; | |
} | |
} | |
// UNION ALL USING WHERE CLAUSE | |
$stmt2=$DBcon->prepare("SELECT name,email FROM table_1 UNION ALL SELECT name,email FROM table_2 WHERE email=:email"); | |
$stmt2->execute(array(':email' =>'[email protected]')); | |
if($stmt2->rowcount()>0) | |
{ | |
echo "<br>"; | |
echo "UNION USING WHERE CLAUSE<br> "; | |
while ($row2=$stmt2->FETCH(PDO::FETCH_ASSOC)) | |
{ | |
echo "<br>name : ".$row2['name'].'<br>'; | |
echo "email : ".$row2['email'].'<br>'; | |
} | |
} | |
} | |
catch (PDOException $e) | |
{ | |
echo $e->getMessage(); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UNION | |
Name : jack | |
Name : shubham | |
UNION ALL | |
Name : jack | |
Name : shubham | |
Name : shubham | |
UNION USING WHERE CLAUSE | |
name : jack | |
email : [email protected] | |
name : shubham | |
email : [email protected] | |
UNION USING WHERE CLAUSE | |
name : jack | |
email : [email protected] | |
name : shubham | |
email : [email protected] | |
name : shubham | |
email : [email protected] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- phpMyAdmin SQL Dump | |
-- version 4.6.5.2 | |
-- https://www.phpmyadmin.net/ | |
-- | |
-- Host: 127.0.0.1 | |
-- Generation Time: Sep 12, 2017 at 08:05 AM | |
-- Server version: 10.1.21-MariaDB | |
-- PHP Version: 5.6.30 | |
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; | |
SET time_zone = "+00:00"; | |
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; | |
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; | |
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; | |
/*!40101 SET NAMES utf8mb4 */; | |
-- | |
-- Database: `union` | |
-- | |
-- -------------------------------------------------------- | |
-- | |
-- Table structure for table `table_2` | |
-- | |
CREATE TABLE `table_2` ( | |
`id` int(11) NOT NULL, | |
`name` varchar(255) NOT NULL, | |
`email` varchar(255) NOT NULL | |
) ENGINE=InnoDB DEFAULT CHARSET=latin1; | |
-- | |
-- Dumping data for table `table_2` | |
-- | |
INSERT INTO `table_2` (`id`, `name`, `email`) VALUES | |
(1, 'shubham', '[email protected]'), | |
(2, 'shubham', '[email protected]'); | |
-- | |
-- Indexes for dumped tables | |
-- | |
-- | |
-- Indexes for table `table_2` | |
-- | |
ALTER TABLE `table_2` | |
ADD PRIMARY KEY (`id`); | |
-- | |
-- AUTO_INCREMENT for dumped tables | |
-- | |
-- | |
-- AUTO_INCREMENT for table `table_2` | |
-- | |
ALTER TABLE `table_2` | |
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3; | |
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; | |
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; | |
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment