选择添加附加行以进行连接提示

时间:2013-08-16 12:10:08

标签: mysql

我有以下查询:

SELECT
    a.id,
    concat( a.name, ' -', b.title, '-') AS title
FROM
    tourist_tour as a
    left join ategories as b on a.category=b.id
order by a.name asc

我想在结果的开头添加一行,其中id为0,标题为“请选择游览”。

我用

看过答案
Select 0, "select a tour"
UNION ALL
rest of select query

但它不适合我的加入。

有什么想法吗?

由于

1 个答案:

答案 0 :(得分:0)

 SELECT 0 AS id, "select a tour" AS title
 UNION ALL
 SELECT
     a.id,
     concat( a.name, ' -',  IFNULL(b.title,' '), '-') AS title
 FROM
     tourist_tour AS a
 LEFT JOIN categories AS b ON a.category=b.id
 ORDER BY CASE WHEN id = 0 THEN id ELSE title END

SQLFIDDLE:http://www.sqlfiddle.com/#!2/310cc3/1/0