0

我有以下查询:

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

但它不适用于我的加入。

有任何想法吗?

谢谢

4

1 回答 1

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

于 2013-08-16T15:21:12.520 回答