2017-05-05 86 views
0

我有表名= schedule如何選擇只有一個字段和所有其他字段值重複的記錄?

-------------------------------------------- 
| course_id | room | day |  time  | 
-------------------------------------------- 
|  2  | 401 |saturday| 9:00 - 13:00 | 
-------------------------------------------- 
|  3  | 401 | sunday | 9:00 - 13:00 | 
-------------------------------------------- 
|  2  | 402 | monday | 9:00 - 13:00 | 
-------------------------------------------- 
|  3  | 403 | tuesday| 14:00 - 17:00| 
-------------------------------------------- 
|  4  | 401 | tuesday| 9:00 - 13:00 | 
-------------------------------------------- 
|  2  | 402 |wednesday|14:00 - 17:00| 
-------------------------------------------- 

和另一個表名=當然

---------------------------------- 
| id | course_code | course_name | 
---------------------------------- 
| 2 | cse  | cse   | 
------------------------ 
| 3 | eee  | eee   | 
---------------------- 
| 4 | ct   | ct   | 
----------------------- 

現在如何讓輸出這樣的..

---------------------------------- 
| course code | course name | info | 
---------------------------------- 
|cse   | cse  |401,satueday-9:00-13:00; 402,monday-9:00-13:00; 402,wednesday-14:00-17:00| 
------------------------ 
| eee  | eee   | 401,sunday-9:00-13:00;403,tuesday-14:00-17:00 | 
------------------------ 
| ct  | ct   | 401,tuesday-9:00-13:00| 
----------------------- 
+0

我不知道如何使表堆棧溢出?由於這個原因,我的表結構不好.. –

+0

php Myadmin數據庫。 –

+0

查詢[** HERE **](https://webapps.stackexchange.com/questions/6700/is-there-a-webapp-to-create-ascii-art-tables)表格格式 –

回答

1

您可以使用GROUP_CONCAT

select c.course_code, 
    c.course_name, 
    group_concat(concat(s.room, ',', s.day, '-', s.time) separator ';') 
from course c 
join schedule s on s.course_id = c.id 
group by c.course_code, 
    c.course_name; 
+0

我可以使用WHERE這個查詢中的條件?像這樣select c.course_code, c.course_name, group_concat(concat(s.room,',',s.day,' - ',s.time)separator';') from course c join schedule on s.course_id = c.id group by c.course_code, c.course_name WHERE example = 8; –

+0

@KuntalGupta你的桌子不是有例子嗎?但是,你可以在那裏包括 –

+0

哦對不起,它只是輸入錯誤。 –

相關問題