2017-03-13 72 views
1

我有兩個表。一個是TblPeople(ID(PK),名,姓,Embo ..)和其他是TBLembolization(EmbolizationID(PK),PeopleID(FK),EmbolizationDate,在單行顯示重複的行mysql

這是我的查詢,使用名稱...

 SELECT tblpeople.FirstName,details.peopleID,details.embolizationDate FROM tblpeople 
     LEFT JOIN tblembolizations AS details ON tblpeople.ID=details.peopleID 
     WHERE tblpeople.Embo = 1 and concat(FirstName,' ',LastName) like '%alina%' 
     UNION 
     SELECT tblpeople.FirstName,details.peopleID,details.embolizationDate FROM tblpeople 
     LEFT JOIN tblembolizations AS details ON tblpeople.ID=details.peopleID 
     WHERE tblpeople.Embo != 1 and concat(' ',FirstName,' ',LastName,' ') like '%alina%' 

結果是:

............Result ....................... 
    FirstName |peopleID|embolizationDate 
    Alina  |16,459 |2017-27-03 
    Alina  | 16,459 |2017-24-03 
    Alina D. | 14,499 |2017-27-09 
    ====================================== 

可以看到,阿麗娜擁有像她那樣的栓塞上兩個日期的多個紀錄,我想它在單一的記錄,如果有人做了栓塞超過1次,所以我想要它這樣的一些事情

............Result ....................... 
    FirstName |peopleID|embolizationDate|2ndembolization 
    Alina  |16,459 |2017-27-03  |2017-24-03 
    Alina D. | 14,499 |2017-27-09 
    ====================================== 

如果有人不止一次地完成了栓塞並且只有一次栓塞的時間比記錄以正確的方式進行了一次,那麼是否有可能在單一記錄中得到答案。 謝謝

回答

2

我不明白你爲什麼使用UNION。這看起來像一個簡單的聚合。

將值放在單獨的列是棘手的。將它們放在分隔字符串中更容易。也許這是你想要的:

SELECT p.FirstName, p.ID, GROUP_CONCAT(d.embolizationDate) as dates 
FROM tblpeople p LEFT JOIN 
    tblembolizations details 
    ON p.ID = details.peopleID 
WHERE pconcat(FirstName,' ',LastName) like '%alina%' 
GROUP BY p.FirstName, p.ID; 
+0

謝謝你的工作:) –