簡體   English   中英

MySQL視圖與多行聯接表

[英]MySQL view joining table with multiple rows

我有一些已聯接在一起的表,並希望聯接具有多列的表。 我當前的查詢如下:

select
    usrs.firstname, usrs.middleNames, usrs.surname,
    if (usrs.sex=0,'Male','Female') as sex,
    usrs.DOB,
   (YEAR(CURDATE())-YEAR(usrs.DOB)) - (RIGHT(CURDATE(),5)<RIGHT(usrs.DOB,5)) AS age,
    birth.townName AS 'birthTown', birth.regionName AS 'birthRegion', birth.countryName AS 'birthCountry',
    location.townName AS 'curTown', location.regionName AS 'curRegion', location.countryName AS 'curCountry',
    usrs.email, emails.email AS 'alternateEmail',
    numbers.number,
    usrs.website,
    usrs.aboutMe,
    family.mother, family.father, family.partner, marital.status, family.aboutFamily,
    children.name AS 'childsName'
from ch09.tbl_users usrs
LEFT JOIN vw_town_region_country birth ON birth.townID = usrs.birthPlace
LEFT JOIN vw_town_region_country location ON location.townID = usrs.currentLocation
LEFT JOIN tbl_alternate_emails emails ON emails.userID = usrs.id
LEFT JOIN tbl_contact_numbers numbers ON numbers.userID = usrs.id
LEFT JOIN tbl_family family ON family.userID = usrs.id
LEFT JOIN tbl_marital_status marital ON family.maritalStatusID = marital.id
LEFT JOIN tbl_children children ON family.id = children.familyID

我把我的整個查詢,這可能是一個錯誤或更干凈的方式來做到這一點。 問題在於tbl_children,因為它是“一對多”的,因此對於tbl_children表中該用戶擁有的每個孩子,單個用戶都會產生多行。

所以我的結果是:

userID:1 firstName middleNames surname ....... childsName
userID:1 firstName middleNames surname ....... childsName
userID:1 firstName middleNames surname ....... childsName

我會比較喜歡:

userID:1 firstName middleNames surname ....... childsName childsName2 childsName3

是否可以通過Join做到這一點? 顯然,在視圖上每個用戶有多個條目對我來說是不可接受的。

您可以將功能GROUP_CONCAT與GROUP BY結合使用。 GROUP_CONCAT使您可以通過串聯來匯總列中的值。 請注意,這不會為每個孩子提供一列,而是為一列包含一個包含所有名稱的字符串。

編輯; 您的查詢將變為:

select
    usrs.firstname, usrs.middleNames, usrs.surname,
    if (usrs.sex=0,'Male','Female') as sex,
    usrs.DOB,    (YEAR(CURDATE())-YEAR(usrs.DOB)) - (RIGHT(CURDATE(),5)<RIGHT(usrs.DOB,5)) AS age,
    birth.townName AS 'birthTown', birth.regionName AS 'birthRegion', birth.countryName AS 'birthCountry',
    location.townName AS 'curTown', location.regionName AS 'curRegion', location.countryName AS 'curCountry',
    usrs.email, emails.email AS 'alternateEmail',
    numbers.number,
    usrs.website,
    usrs.aboutMe,
    family.mother, family.father, family.partner, marital.status, family.aboutFamily,
    GROUP_CONCAT(children.name SEPERATOR ",") AS 'childsName' 
FROM ch09.tbl_users usrs 
LEFT JOIN vw_town_region_country birth ON birth.townID = usrs.birthPlace 
LEFT JOIN vw_town_region_country location ON location.townID = usrs.currentLocation
LEFT JOIN tbl_alternate_emails emails ON emails.userID = usrs.id 
LEFT JOIN tbl_contact_numbers numbers ON numbers.userID = usrs.id 
LEFT JOIN tbl_family family ON family.userID = usrs.id 
LEFT JOIN tbl_marital_status marital ON family.maritalStatusID = marital.id 
LEFT JOIN tbl_children children ON family.id = children.familyID 
GROUP BY userID

假設在編寫查詢時不知道子級數(即,用戶可能有0或1或5個子級),那么像這樣進行數據透視可能不是將數據導入前端應用程序的最佳途徑。

根據訪問數據的方式,最好還是按需為每個用戶返回多行,或者根據需要為每個用戶檢索子級(和電子郵件等)。 這里的關鍵是僅在需要時才檢索它們。 我相信這在面向對象的世界中被稱為延遲加載。

如果您這樣做是為了填充某種類型的列表框,因此您需要每個用戶使用它們,那么您可以考慮根據列表的顯示方式來設置對要檢索的子代數的限制。使用LEFT JOIN來獲取您要檢索的行的確切數字,而不是為每個用戶往返服務器。

換句話說,這一切都取決於。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM