是否可以将两个或三个列处理(合并)视为一列,并使用该列中的LIKE
关键字进行搜索,并根据需要搜索ORDER结果?
我的专栏
| to_place | from_place | departure_place|
|:-----------|------------:|:------------: |
| Same | Place | Line
| Rangpur | Dhaka | Chittagong
| Badda | bogra | hello
| Shyamoli | rajshahi | mohammadpur
| test | test1 | adabor
| another | test2 | kurigram
所以我希望我的SQL
中的一列中有这3列,以便我可以订购它们
答案 0 :(得分:5)
我已经尝试了这个并且它正在运作。
SELECT CONCAT(`to_place`, `from_place`,`departure_place`) AS derived_place
FROM `yourtable`
HAVING derived_place LIKE '%search_value%'
ORDER BY derived_place;
我希望这会有所帮助。
答案 1 :(得分: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
实际上你只能使用UNION,因为UNION ALL会返回相同的城市名称,如果它在多个列中。这不是必要的。