Search code examples
mysqlconcat-ws

Mysql Inner join with concat_ws


I have 2 table.

first table

 | idgroup | namegroup

second table

 | idrequest | col1 | col2 | N1 | N2 | date_extract  | 

I want to join the tables with concat_ws

    SELECT
    tb1.*,tb2.*,
    CONCAT_WS("_",tb2.N1, tb2.N2) AS GR,
    FROM
    table2 tb2
    INNER JOIN table1 tb1 ON tb1.namegroup= tb2.GR
    WHERE
    tb2.date_extract = "2015-02-13"

Is it possible? then how?


Solution

  • I am giving a blind try. Nothing changed much except a date() conversion to tb2.date_extract-taking the date value if date_extract is of type TIMESTAMP or DATETIME.

    SELECT
        tb1.*,tb2.*,
        CONCAT_WS("_",tb2.`N1`, tb2.`N2`) AS `GR`,
        FROM
        table2 tb2
        INNER JOIN table1 tb1 ON tb1.`namegroup`= tb2.GR
        WHERE
        date(tb2.`date_extract`) = "2015-02-13"
    

    If this is not your problem, then please post the error message with the question.

    UPDATE: You can simply use GR instead of tbl2.GR need to use the namespace for the result

    SELECT
        tb1.*, tb2.*, CONCAT_WS("_",tb2.`N1`, tb2.`N2`) AS `GR`,
        FROM table2 tb2
        INNER JOIN table1 tb1 ON tb1.`namegroup`= `GR`
    WHERE
        date(tb2.`date_extract`) = "2015-02-13"
    

    -now let me know how it goes?