Problem joining select strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe

    #1

    Problem joining select strings

    I've got 3 columns in a mysql database that I am trying to join. I'm using
    CONCAT(), but it's giving me NULL results. This has worked for me before ...
    What am I doing wrong?

    Here's a straight select:

    mysql> select peopleID, firstName, middleInit, lastname
    -> from people;
    +----------+-----------+------------+------------+
    | peopleID | firstName | middleInit | lastname |
    +----------+-----------+------------+------------+
    | 1 | Joe | NULL | Shockey |
    | 3 | John | NULL | Doe |


    Here my select with the concat. It's not concatenating the string for some
    reason:

    mysql> select peopleID, CONCAT(firstNam e, ' ', middleInit, ' ', lastname) AS
    person
    -> from people;
    +----------+--------+
    | peopleID | person |
    +----------+--------+
    | 1 | NULL |


    Can anyone tell me what I'm doing wrong? Should I be using a different
    method for joining these srings?

    BTW - here's my table structure:

    mysql> show columns from people;
    +------------+-------------+------+-----+---------+----------------+
    | Field | Type | Null | Key | Default | Extra |
    +------------+-------------+------+-----+---------+----------------+
    | peopleID | int(10) | | PRI | NULL | auto_increment |
    | firstName | varchar(50) | YES | | NULL | |
    | middleInit | varchar(50) | YES | | NULL | |
    | lastName | varchar(50) | YES | | NULL | |
    | phone | varchar(50) | YES | | NULL | |
    | email | varchar(50) | YES | | NULL | |
    +------------+-------------+------+-----+---------+----------------+

  • Sugapablo

    #2
    Re: Problem joining select strings

    Joe wrote:[color=blue]
    > I've got 3 columns in a mysql database that I am trying to join. I'm using
    > CONCAT(), but it's giving me NULL results. This has worked for me before ...
    > What am I doing wrong?[/color]

    CONCAT(str1,str 2,...)
    Returns the string that results from concatenating the arguments.
    Returns NULL if any argument is NULL. May have more than 2 arguments. A
    numeric argument is converted to the equivalent string form:

    mysql> SELECT CONCAT('My', 'S', 'QL');
    -> 'MySQL'
    mysql> SELECT CONCAT('My', NULL, 'QL');
    -> NULL
    mysql> SELECT CONCAT(14.3);
    -> '14.3'

    CONCAT_WS(separ ator, str1, str2,...)
    CONCAT_WS() stands for CONCAT With Separator and is a special form
    of CONCAT(). The first argument is the separator for the rest of the
    arguments. The separator can be a string as well as the rest of the
    arguments. If the separator is NULL, the result will be NULL. The
    function will skip any NULL values after the separator argument. The
    separator will be added between the strings to be concatenated:

    mysql> SELECT CONCAT_WS(","," First name","Second name","Last Name");
    -> 'First name,Second name,Last Name'
    mysql> SELECT CONCAT_WS(","," First name",NULL,"Las t Name");
    -> 'First name,Last Name'



    --
    Sugapablo
    ------------------------------------
    http://www.sugapablo.com <--music
    http://www.sugapablo.net <--personal

    Comment

    Working...