473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to combine two columns into one in mysql?

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?
Jun 5 '07 #1
8 29826
select concat(firstname,lastname) as 'name' from tablename
Jun 5 '07 #2
debasisdas
8,127 Expert 4TB
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.
Jun 6 '07 #3
debasisdas
8,127 Expert 4TB
Use simple concatination and a column alias to display as a single field.
Jun 6 '07 #4
keeps21
23
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>'; ?>
Jul 20 '07 #5
keeps21
23
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.  
Jul 20 '07 #6
mwasif
802 Expert 512MB
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")?>">
Jul 20 '07 #7
keeps21
23
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.  
Jul 23 '07 #8
mwasif
802 Expert 512MB
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>
Jul 23 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Philip Stoev | last post by:
Hi all, Please tell me if any of this makes sense. Any pointers to relevant projects/articles will be much appreciated. Philip Stoev http://www.stoev.org/pivot/manifest.htm ...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
2
by: tina.boroff | last post by:
I didn't know what to use as a title for this. Here is the scenario: I have a op.reqedit.php page. This page a form where you input data. In this page I have split a MySQL field. It is called...
4
by: Dave Moore | last post by:
Hi All, I'm putting a website together using PHP and a MySQL database. I've been using phpMyAdmin as it makes updating the DB nice and easy. For development, I've been using Apache, MySQL and PHP...
20
by: Steve London | last post by:
I'm sure this has been brought up many times, but I will ask anyway. Let's say I have 2 tables related: Owner: --------- o_id o_name Dog:
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
2
denny1824
by: denny1824 | last post by:
Hi everyone, Here is my problem. I have a Table Variable (I could easily turn it into a Temp Table instead) that will sometimes have rows that are identical except for one specific column. The...
1
by: bluereign | last post by:
Thank you for your assistance. I am a novice looking to JOIN and append or combine records from 2 current Tables into 2 new Tables named below. I have been able to JOIN Tables with the script...
2
by: murugavelmsc | last post by:
Hi, i develop a application with VB and mysql. my question: in excel a1 column have a 10 values. Based on the values we get data from mysql and place it in a2 columns (corresponding row) ...
3
f430
by: f430 | last post by:
i was wondering if it is possible to do the combine columns into a query to make a new table. if my original table looked like this Part Number.....Col 1.......Col 2.....Col 3..Col 4.......Col...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.