dongpu2476 2018-07-26 08:23
浏览 126
已采纳

GROUP_CONCAT很奇怪

I use three tables with an inner join on 'user.ID'='email.ID' and 'user.ID'='telephone.ID'.

Every user has a name and an ID. Each email has a username, the email address itself and a corresponding userID. Each telephone number has a username, the number itself and a corresponding userID. It is possible for a user to have any amount of email addresses and telephone numbers.

I managed to output a users email addresses by selecting them like this:

GROUP_CONCAT(email.Address SEPARATOR ',') AS Address,
GROUP_CONCAT(email.Username SEPARATOR ',') AS eUsername

I then converted it into an array:

$Address = explode(",", $row["Address"]);
$eUsername = explode(",", $row["eUsername"]);

And printed it like this:

  for($i = 0;$i<count($Address);$i++) {
    echo $eUsername[$i] .': '. $Address[$i].'<br>';
  }

This is the printed table: enter image description here

I then just did the same thing with the telephone numbers

Selecting:

GROUP_CONCAT(telephone.Number SEPARATOR ',') AS Number,
GROUP_CONCAT(telephone.Username SEPARATOR ',') AS tUsername

Converting:

$Number = explode(",", $row["Number"]);
$tUsername = explode(",", $row["tUsername"]);

Printing:

for($i = 0;$i<count($Number);$i++) {
    echo $tUsername[$i] .': '. $Number[$i].'<br>';
  }

But this messed up the whole table: enter image description here

I don´t get why it prints the email addresses three times and just doesn´t print anything in the 'Telephone' coloumn although the second for loop is echoed into the most right coloumn.

Sorry for the long post and thanks for any help.

EDIT:

Here is my full SQL query:

SELECT user.Name AS Name, user.ID AS ID,
GROUP_CONCAT(email.Address SEPARATOR ',') AS Address,
GROUP_CONCAT(email.Username SEPARATOR ',') AS eUsername,
GROUP_CONCAT(telephone.Number SEPARATOR ',') AS Number,
GROUP_CONCAT(telephone.Username SEPARATOR ',') AS tUsername
FROM user
INNER JOIN email ON user.ID=email.ID
INNER JOIN telephone ON user.ID=telephone.ID
WHERE Name REGEXP '$searchterm'
GROUP BY ID

The WHERE statement is not final. This is just for testing.

  • 写回答

1条回答 默认 最新

  • dongyushen9063 2018-07-26 10:24
    关注

    The simplest solution is to use distinct:

    SELECT u.Name AS Name, u.ID AS ID,
           GROUP_CONCAT(DISTINCT e.Address SEPARATOR ',') AS Address,
           GROUP_CONCAT(DISTINCT e.Username SEPARATOR ',') AS eUsername,
           GROUP_CONCAT(DISTINCT t.Number SEPARATOR ',') AS Number,
           GROUP_CONCAT(DISTINCT t.Username SEPARATOR ',') AS tUsername
    FROM mitglied u INNER JOIN
         email e
         ON u.ID = e.ID INNER JOIN
         telephone t
         ON u.ID = t.ID
    WHERE u.Name REGEXP '$searchterm'
    GROUP BY u.ID, u.name;
    

    Notes:

    • I assume that mitglied is users.
    • Table aliases make the query easier to write and to read.
    • Qualified column names are recommended whenever you have multiple table references.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能