2

Is it possible to treat two or three column treat (merge) as one column and search with LIKE keyword from that one column and ORDER result as desired?

my columns

| to_place   | from_place  | departure_place|
|:-----------|------------:|:------------: |
| Same       |    Place    |    Line     
| Rangpur    |    Dhaka    |    Chittagong   
| Badda      |    bogra    |    hello     
| Shyamoli   |    rajshahi |    mohammadpur      
| test       |    test1    |    adabor    
| another    |    test2    |    kurigram

so i want these 3 columns in one column in my SQL so that i can order them

Rejoanul Alam
  • 5,435
  • 3
  • 39
  • 68

2 Answers2

5

I've tried this and it is working.

SELECT CONCAT(`to_place`, `from_place`,`departure_place`) AS derived_place
FROM `yourtable`
HAVING derived_place LIKE '%search_value%'
ORDER BY derived_place;

I hope this helps.

Jis Jose
  • 614
  • 1
  • 6
  • 15
4
SELECT * FROM (
  SELECT to_place as Location FROM yourtable
  UNION ALL
  SELECT from_place FROM yourtable
  UNION ALL
  SELECT departure_place FROM yourtable) as DERIVEDTABLENAME
ORDER BY 1

actually you can merely use UNION as UNION ALL would return the same city name if it was in more than one column. that wouldn't really be necessary.

Niagaradad
  • 453
  • 3
  • 10