MySQL View 连接多行表

标签 mysql sql join

我有几个表,我已经连接在一起,想连接一个有多个列的表。我当前的查询如下:

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 表中拥有的每个 child 的单个用户的多行。

所以我的结果是:

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 以某种方式做到这一点?显然,我不能接受每个用户在 View 中有多个条目。

最佳答案

您可以使用函数 GROUP_CONCAT为此结合 GROUP BY。 GROUP_CONCAT 让您通过连接列来聚合列中的值。请注意,这不会为每个 child 提供一列,而是为您提供一个包含所有名称的字符串的列。

编辑;你的查询会变成这样:

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

关于MySQL View 连接多行表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7148935/

相关文章:

java - jdbc与mysql连接的步骤是什么

sql - 在存储过程中重用 SELECT 查询的结果

php - mysql价格比较,显示所有价格

c# - 仅在其他机器上的 MySQL 语法错误

php - 如何使我的搜索栏链接到页面

sql - 如何根据出现的列序列提取行

php - php 函数中的 MySQL-Link 错误

mysql - 获取加入结果的最新记录

MySQL根据场景适配JOIN

mysql - 全新 mysql-server 安装不要求输入密码