2014-01-25 39 views
0

我創建MySQL中的方法,用以下代碼內:MySQL的:如何包括「 G」的過程

delimiter # 
drop procedure if exists a03_strings # 
create procedure a03_strings() 
begin 
    select concat(cl_id, ' ',cl_name_last, ', ', cl_name_first) as Client 
    , group_concat(coalesce(concat(an_name, ' (', an_type, ')'), 'no animals')) as Animals 
    from vt_clients 
    left join vt_animals using (cl_id) 
    group by cl_id\G; 
end; 
# 

我在第9行,以便得到下面的輸出添加\G

enter image description here 所以輸出不是一個表,而是行。

當我測試這只是作爲選擇語句,它與\G合作。但是,當我在程序中包含\G並且它給我錯誤時說我不能在另一個程序中包含一個程序。

是否有替代方案來實現我需要的輸出?

謝謝!

回答

0

簡短的回答是否定的:「\ G」是由MySQL客戶端不是DBMS(如存儲過程運行處理,但你可以簡單:

mysql> create procedure a03_strings() 
    -> begin 
    -> select concat(cl_id, ' ',cl_name_last, ', ', cl_name_first) as Client 
    -> , group_concat(coalesce(concat(an_name, ' (', an_type, ')'), 'no animals')) as Animals 
    -> from vt_clients 
    -> left join vt_animals using (cl_id) 
    -> group by cl_id\G; 
    -> end; 
    -> # 
... 
mysql-> call a03strings() \G# 

或者用它作爲你的SELECT ...

SELECT IF (rowselect='A', 
    CONCAT('Client: ', ilv.Client), 
    CONCAT('Animals', ilv.Animals) 
FROM (
    select concat(cl_id, ' ',cl_name_last, ', ', cl_name_first) as Client 
    , group_concat(coalesce(concat(an_name, ' (', an_type, ')'), 'no animals')) as Animals 
    from vt_clients 
    left join vt_animals using (cl_id) 
    group by cl_id 
) ilv, 
(SELECT 'A' AS rowselect UNION SELECT 'B') AS rowmultiplier 
+0

,謝謝,我結束了使用'選擇CONCAT( '客戶:',CONCAT(cl_id, '',cl_name_last, '',cl_name_first), '\ n動物:',GROUP_CONCAT(合併(CONCAT( an_name,'(',an_type,')'),'no animals')))' – PMa