2013-08-16 55 views
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 

看到答案,但它不與我一起工作。

任何想法?

感謝

+0

這應該很好地工作。你能發佈完整的查詢哪些不起作用和你得到的錯誤信息? – Vatev

+0

使用數據庫而不是接收程序的奇怪做法。 – mishik

+0

看看我的答案。你的'ORDER'子句是我認爲的主要問題,而第二個'SELECT'中缺少名稱列。我還添加了一個IFNULL函數,因爲您正在使用'LEFT JOIN' –

回答

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