Connecting Tech Pros Worldwide Forums | Help | Site Map

How to combine two columns into one in mysql?

Newbie
 
Join Date: Jun 2007
Posts: 1
#1: Jun 5 '07
For examples if we take first name,last name as the attribute values and if i want to print the data as a single column then how do we do it?

Newbie
 
Join Date: Jun 2007
Posts: 2
#2: Jun 5 '07

re: How to combine two columns into one in mysql?


select concat(firstname,lastname) as 'name' from tablename
debasisdas's Avatar
Moderator
 
Join Date: Dec 2006
Location: Bangalore ,India
Posts: 7,511
#3: Jun 6 '07

re: How to combine two columns into one in mysql?


Hi
nelaturuk
Welcome to TSDN.

You have reached the right place for knowledge shairing.

Here you will find a vast resource of related topics and code.

Feel free to post more doubts/questions in the forum.

But before that give a try from your side and if possible try to post what/how you have approached to solve the problem.
It will help Experts in the forum in solving/underestanding your problem in a better way.
debasisdas's Avatar
Moderator
 
Join Date: Dec 2006
Location: Bangalore ,India
Posts: 7,511
#4: Jun 6 '07

re: How to combine two columns into one in mysql?


Use simple concatination and a column alias to display as a single field.
Newbie
 
Join Date: Jul 2007
Location: Durham, UK
Posts: 23
#5: Jul 20 '07

re: How to combine two columns into one in mysql?


This works fine for me when only outputting one column.

I have several fields:

no, firstname, surname, position, shoots, height, weight

I need to join firstname and surname and output as a single field along with the other fields;

so the output fields would be.

no, (firstname + lastname), position, shoots, height, weight


Thanks for any advice, here is my current code, it outputs everything I want, however the name field has a value of 'Array' in each row

<?php
/**
* @version $Id: mod_roster.php 2007-07-20
* @copyright Copyright (C) 2007 Stephen Hoult - SMH WebDesign. All rights reserved.
*/

// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

// Get data records from table.

$result=mysql_query("SELECT * FROM war_roster ORDER BY position DESC, surname ASC");

mysql_query("SELECT CONCAT(firstname, surname) FROM war_roster = $name");

?>

<table width="500" class="roster_border" cellpadding="20" cellspacing="1" >
<tr bgcolor="#78171e" class="table_header">
<td valign="top"><strong>#</strong></td>
<td valign="top"><strong>Name</strong></td>
<td valign="top"><strong>Position</strong></td>
<td valign="top"><strong>Birthdate</strong></td>
<td valign="top"><strong>Height</strong></td>
<td valign="top"><strong>Weight</strong></td>
<td valign="top"><strong>Shoots</strong></td>
</tr>

<?
// Make a variable "$num" with a value "0".
$num=1;

// Do while loop for out put records.
while($row=mysql_fetch_assoc($result)){

// Plus 1 at $num.
$num++;

// Use modulus by 2 in $num value and set the value of "$bg" if result equal 0 or not.
if(($num%2)!=0){
$bg="#E6D78E";
}else{
$bg="#dcc55d";
}
?>
<tr class="table_content" bgcolor="<? echo $bg; ?>">
<td><? echo $row['no']; ?></td>
<td><? echo $row .$name; ?></td>
<td><? echo $row['position']; ?></td>
<td><? echo $row['birthdate']; ?></td>
<td><? echo $row['height']; ?></td>
<td><? echo $row['weight']; ?></td>
<td><? echo $row['shoots']; ?></td>
</tr>

<?
// End while loop.
}

// Close database connection.
mysql_close();
?>
<? echo '</table>'; ?>
Newbie
 
Join Date: Jul 2007
Location: Durham, UK
Posts: 23
#6: Jul 20 '07

re: How to combine two columns into one in mysql?


Fixed the problem,

Here is the fixed code if anyone is interested, also any advice on simplifying it would be appreciated.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /**
  3. * @version $Id: mod_roster.php 2007-07-20 
  4. * @copyright Copyright (C) 2007 Stephen Hoult - SMH WebDesign. All rights reserved.
  5. */
  6.  
  7. // no direct access
  8. defined( '_VALID_MOS' ) or die( 'Restricted access' );
  9.  
  10. // Get data records from table. 
  11.  
  12. //$result=mysql_query("SELECT * FROM war_roster ORDER BY position DESC, surname ASC");
  13.  
  14. //mysql_query("SELECT CONCAT(firstname, surname) FROM war_roster = $name");
  15.  
  16. $result=mysql_query("SELECT no, position, birthdate, height, weight, shoots, CONCAT(firstname,' ',surname) AS 'name' FROM war_roster ORDER BY position DESC, surname ASC");
  17.  
  18. ?>
  19.  
  20. <table width="500" class="roster_border" cellpadding="0" cellspacing="1" >
  21. <tr bgcolor="#78171e" class="table_header">
  22. <td valign="top"><strong>#</strong></td>
  23. <td valign="top"><strong>Name</strong></td>
  24. <td valign="top"><strong>Position</strong></td>
  25. <td valign="top"><strong>Birthdate</strong></td>
  26. <td valign="top"><strong>Height</strong></td>
  27. <td valign="top"><strong>Weight</strong></td>
  28. <td valign="top"><strong>Shoots</strong></td>
  29. </tr>
  30.  
  31. <?
  32. // Make a variable "$num" with a value "0". 
  33. $num=1;
  34.  
  35. // Do while loop for out put records. 
  36. while($row=mysql_fetch_assoc($result)){
  37.  
  38. // Plus 1 at $num.
  39. $num++;
  40.  
  41. // Use modulus by 2 in $num value and set the value of "$bg" if result equal 0 or not. 
  42. if(($num%2)!=0){
  43. $bg="#E6D78E";
  44. }else{
  45. $bg="#dcc55d";
  46. }
  47. ?>
  48. <tr class="table_content" bgcolor="<? echo $bg; ?>">
  49. <td><? echo $row['no']; ?></td>
  50. <td class="padding" align="left"><? echo $row['name']; ?></td>
  51. <td><? echo $row['position']; ?></td>
  52. <td><? echo $row['birthdate']; ?></td>
  53. <td><? echo $row['height']; ?></td>
  54. <td><? echo $row['weight']; ?></td>
  55. <td><? echo $row['shoots']; ?></td>
  56. </tr>
  57.  
  58. <? 
  59. // End while loop. 
  60.  
  61. // Close database connection. 
  62. mysql_close();
  63. ?>
  64. <? echo '</table>'; ?>
  65.  
mwasif's Avatar
Moderator
 
Join Date: Jul 2006
Location: Pakistan
Posts: 719
#7: Jul 20 '07

re: How to combine two columns into one in mysql?


Everything is fine. To keep the script clean remove unwanted code:).

You can use ternary operator to show iterrative row color as follows
No need for this whole code
[PHP]// Plus 1 at $num.
$num++;

// Use modulus by 2 in $num value and set the value of "$bg" if result equal 0 or not.
if(($num%2)!=0){
$bg="#E6D78E";
}else{
$bg="#dcc55d";
}[/PHP]

use this one

Expand|Select|Wrap|Line Numbers
  1. <tr class="table_content" bgcolor="<?=(++$num%2==0 ? "#E6D78E" : "#dcc55d")?>">
Newbie
 
Join Date: Jul 2007
Location: Durham, UK
Posts: 23
#8: Jul 23 '07

re: How to combine two columns into one in mysql?


One more quick (i hope) question is;

The code I've been trying to write outputs a hockey team roster, name, number, position, etc. Here is the code in action: http://www.whitleywarriors.net/index.php?option=com_content&task=view&id=12&Itemi d=26

Eventually each player name will need to act as a hyperlink to that players player profile/bio, as seen here: http://www.newyorkrangers.com/team/roster.asp

As a newbie to myql/php how should I go about doing that?

Here is my updated code,

Thanks for your help guys.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /**
  3. * @version $Id: mod_roster.php 2007-07-20 
  4. * @copyright Copyright (C) 2007 Stephen Hoult - SMH WebDesign. All rights reserved.
  5. */
  6.  
  7. // no direct access
  8. defined( '_VALID_MOS' ) or die( 'Restricted access' );
  9.  
  10. // Get data records from table. 
  11.  
  12. $result=mysql_query("SELECT no, position, birthdate, height, weight, shoots, CONCAT(firstname,' ',surname) AS 'name' FROM war_roster ORDER BY position DESC, surname ASC");
  13.  
  14. ?>
  15.  
  16. <table width="600" class="roster_border" cellpadding="0" cellspacing="1" >
  17. <tr bgcolor="#78171e" class="table_header">
  18. <td valign="top"><strong>#</strong></td>
  19. <td valign="top"><strong>Name</strong></td>
  20. <td valign="top"><strong>Position</strong></td>
  21. <td valign="top"><strong>Birthdate</strong></td>
  22. <td valign="top"><strong>Height</strong></td>
  23. <td valign="top"><strong>Weight</strong></td>
  24. <td valign="top"><strong>Shoots</strong></td>
  25. </tr>
  26.  
  27. <?
  28. // Make a variable "$num" with a value "0". 
  29. $num=0;
  30.  
  31. // Do while loop for out put records. 
  32. while($row=mysql_fetch_assoc($result)){
  33.  
  34.  
  35. ?>
  36. <tr class="table_content" bgcolor="<?=(++$num%2==0 ? "#E6D78E" : "#dcc55d")?>">
  37. <td><? echo $row['no']; ?></td>
  38. <td align="left"><? echo $row['name']; ?></td>
  39. <td align="left"><? echo $row['position']; ?></td>
  40. <td><? echo $row['birthdate']; ?></td>
  41. <td><? echo $row['height']; ?></td>
  42. <td><? echo $row['weight']; ?></td>
  43. <td><? echo $row['shoots']; ?></td>
  44. </tr>
  45.  
  46. <? 
  47. // End while loop. 
  48.  
  49. // Close database connection. 
  50. mysql_close();
  51. ?>
  52. <? echo '</table>'; ?>
  53.  
mwasif's Avatar
Moderator
 
Join Date: Jul 2006
Location: Pakistan
Posts: 719
#9: Jul 23 '07

re: How to combine two columns into one in mysql?


Use <a> tag around the name and provide a link to profile page with id. This id will be used to fetch the palyer information on the view profile page.

Expand|Select|Wrap|Line Numbers
  1. <td align="left"><a href="playerprofile.php?id=<?=$row['no']?>"><? echo $row['name']; ?></a></td>
Reply