Joining Three Tables in mysql with examples

Joining Three Tables in mysql with examples

Joining Three or More Tables

Simple Although each join one query to specification joins only two tables and here example of the , FROM clauses can all the contain multiple join tables specifications. This allows many two or more tables to be joined tables for a single query.Joining Three Tables in mysql with examples

SELECT p.invoicename, v.invoicename
FROM Production.Product p
JOIN sallings.ProductVendor pv
ON p.ProductID = pv.ProductID
JOIN sallings.Vendor v
ON pv.bshopID = v.bshopID
WHERE psubID = 15
ORDER BY v.invoicename;

SQL join three tables or more tables based(INNER) on a parent-child relationship Examples

A SQL JOIN combines all the records from two tables or more tab. A JOIN locates tables related column all the values in the two or more tables. A query can contain only zero, one, or multiple level nested JOIN operations.here some example of INNER JOIN is the same as JOIN; like as a INNER Joining the keyword INNER is optional.

SELECT s.productname
, s.product_id
, s.productdesc
, h.username
FROM products s
INNER JOIN users us
on s.product_id = us.product_id
INNER JOIN halls h
on us.userid = h.userid

How to join three or more tables in SQL query – MySQL Simple Example

Here is an simple example of SQL join three or more tables with conditions.

SELECT a.ord_num,u.cust_name,a.cust_code,  
p.agent_code,u.cust_city  
FROM products p,users u,orders a  
WHERE u.cust_city=p.working_area  
AND a.cust_code=u.cust_code  
AND a.agent_code=p.agent_code  

sql joining three tables using inner joins Example

A parent-child relationship Like (uniq id) between two tables can be created only one table mapped when there is a PRIMARY KEY in tables in one table and FOREIGN KEY other tables in another table.

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Inquiry Mst~~~~~~~~~~~~~~~~~~~~~~``*/

if(isset($page) && $page == "inquirymst")

{
  if(!empty($action) && $action == "ChangeStatus")
	{
		//print_r($_REQUEST);
		$inquiryid = $_REQUEST['id'];
		$status = $_REQUEST['status'];
		$rowid = $_REQUEST['rowid'];
		$tableName = $_REQUEST['tableName'];
		$changeStatus = '';
		if($status == 0){$changeStatus = '1';}
		else if($status == 1){$changeStatus = '0';}	
		$selUpdtData = "UPDATE  $tableName SET status = '$changeStatus' 
		WHERE inquiryid = '$inquiryid'" ;
		$selDistQtyQQ=mysql_query($selUpdtData) or die(mysql_error());
		//return status and page to redirect
		if($selDistQtyQQ)
		{
			$device_token = array();
			$deviceT = "SELECT u.devicetoken FROM inquirymst i
			left outer join usermst u on u.userid = i.userid
			WHERE trim(u.devicetoken) != ' ' AND i.inquiryid = '$inquiryid'";
			$deviceTQQ = mysql_query($deviceT);
			$deviceTCount = mysql_num_rows($deviceTQQ);
			if($deviceTCount > 0)
			{
				while($deviceTRR = mysql_fetch_array($deviceTQQ))
				{
					$device_token[] = $deviceTRR['devicetoken'];
				}				
			}
			$message = "Inquiry has been closed";
			$insertNotification = "INSERT INTO notification(userid,orderid,message,type,status,inquiryid)
			VALUES('$userid','$orderid','".addslashes($message)."','2','0','$inquiryid')";
			$insertNotificationQQ = mysql_query($insertNotification);
			if($deviceTCount > 0)
			{
				$res = functionSendNotifications($device_token,$message,'2',$inquiryid,'0');
			} 
			echo "success"."|".$rowid."|".$inquiryid."|".$changeStatus."|".'inquirymst';
		}
		else
		{
			echo "Error"."|".$rowid."|".$inquiryid."|".$staus."|".'inquirymst';
		}
	}
}

Join – Example

Leave a Comment