2017-02-17 6 views
0

Gibt es mysql Funktion zu überprüfen, ob Wert nur die Nummer ist?MySQL wenn numerisch wählen Sie andere Spalte

Ich möchte CONCAT 2 Spalten, wenn tbl3.column2 Nummer ist dann concat mit anderen Tabellenspalte, wenn String dann gleiche Tabelle andere Spalte tun.

SELECT tbl1.column1 as Column1, tbl2.column2 as Column2, 
IF tbl3.column2 REGEXP '^[0-9]+$' THEN CONCAT(tbl3.column1, ' ', tbl4.column1) ELSE CONCAT(tbl3.column1, ' ', tbl3.column2) END IF as Combined 
FROM table1 tbl1 
LEFT JOIN table2 tbl2 ON tbl1.id = tbl2.id 
LEFT JOIN table3 tbl3 ON tbl3.id = tbl1.id 
LEFT JOIN table4 tbl4 ON tbl4.id = tbl1.id 
LEFT JOIN table5 tbl5 ON tbl5.id = tbl1.id 
WHERE 
tbl5.column3 = ? 
ORDER BY Column1 ASC 

Aktuelle Code gibt Fehler:

You have an error in your SQL syntax; check the manual that corresponds 
to your MySQL server version for the right syntax to use near 'tbl3.column2 REGEXP '^[0-9]+$' 
    THEN CONCAT(tbl3.column1, ' ', tbl4.column1' at line 
+0

Dieses Problem des schlechten Designs – Strawberry

+0

ja symptomatisch scheint nur die Tabellennamen sind genug, um dieses Urteil @Strawberry zu machen – e4c5

Antwort

2

versuchen dies bitte:

SELECT tbl1.column1 AS Column1, tbl2.column2 AS Column2, 
IF(tbl3.column2 REGEXP '^[0-9]+$' 
    , CONCAT(tbl3.column1, ' ', tbl4.column1) 
    , CONCAT(tbl3.column1, ' ', tbl3.column2) 
    ) AS Combined 
FROM table1 tbl1 
LEFT JOIN table2 tbl2 ON tbl1.id = tbl2.id 
LEFT JOIN table3 tbl3 ON tbl3.id = tbl1.id 
LEFT JOIN table4 tbl4 ON tbl4.id = tbl1.id 
LEFT JOIN table5 tbl5 ON tbl5.id = tbl1.id 
WHERE 
tbl5.column3 = ? 
ORDER BY Column1 ASC 
Verwandte Themen