mysql concat function rows sql example

mysql concat function rows sql example - concate more than one rows value

Before running this example please make a database hr which can be found: click here to garb the script

mysql-concat-sql-rows-example.php


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title>mysql concat function rows sql example - concate more than one rows value</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="Author" content="Md Iqbal Hosan">
  <meta name="title" content="mysql concat function rows sql example - concate more than one rows value">
  <meta name="keywords" content="php, example, code, table, sql command, sql,mysql concat function, mysql group_concat function sql">
  <meta name="description" content="mysql concat function rows sql example - concate more than one rows value">
  <style>
   h2, h4{background:#eeD; color:#000083;}
   h2{ padding:3px; margin:3px; font-size:21px;}
   h4{ padding:2px; margin:2px; font-size:12px;}
   p{padding:2px; margin:2px; color:#0099Fd;}
   body{ background:#FFFFFE;}
  </style>
 </head>
 <body>
  <h2>mysql concat function rows sql example - concate more than one rows value</h2>
  <h2>actually it is group_concat() function - which concates more than one rows result to a single string</h2>
  <pre>
   <?php
    /*
     ---------------------------------------------------------------------------------------
     we have a table named EMPLOYEES in the HR database and we would like to concat
     all employees of IT department(DepartmentID=60) by concatenating FirstName columns 
     to a single string, in short we want to concat multiple rows value to a single column
     by using mysql group_concat() function
     ---------------------------------------------------------------------------------------
    */
    $sql = "
     SELECT  GROUP_CONCAT(FirstName)
     FROM  employees
     WHERE  DepartmentID=60
    ";
    /*
     ----------------------------------------------------------------------------------------------
     Here DepartmentID=60 means IT department.

     Note : GROUP_CONCAT() function also returns NULL if all of the given argument(s) is NULL.
     
     ALSO NOTE(my experience) ABOUT NULL:
     
     1. NULL IS A VALUE THAT IS UNAVAILABLE
     2. NULL IS A VALUE THAT IS UNASSIGNED
     3. NULL IS A VALUE THAT IS UNKNOWN
     4. NULL IS A VALUE THAT IS INAPPLICABLE
     5. NULL IS A VALUE THAT IS NOT EVEN EQUAL TO NULL
     6. NULL IS A VALUE THAT IS NOT EQUAL TO ZERO : ZERO IS A NUMBER
     7. NULL IS A VALUE THAT IS NOT EQUAL TO BLANK SPACE OR EMPTY STRING : SPACE IS A CHARACTER
     AND 8. COLUMNS OF ANY DATATYPE CAN CONTAIN NULL VALUE
     -----------------------------------------------------------------------------------------------
    */
    /*
     -------------------------------------------------------
     USING PHP YOU CAN DO THIS. 
     GET CONNECTION TO DATABASE BY USING : mysql_connect()
     SELECT DATABASE BY USING : mysql_select_db()
     THEN RUN THE QUERY
     -------------------------------------------------------
    */
    

    mysql_connect('localhost', 'root', 'root');
    mysql_select_db('hr');
    mysql_query($sql);
    
    /*
     ------------------------------------------------------------------------------
     TO RUN THE PHP CODE PROVIDE REAL DATA TO mysql_connect AND mysql_select_db()
     ------------------------------------------------------------------------------
    */
   ?>
  </pre>
 </body>
</html>

mysql concat function rows sql example - concate more than one rows value - output in the client

No comments:

Post a Comment

leave your comments here..