gpt4 book ai didi

php - mysql 中的数据崩溃

转载 作者:行者123 更新时间:2023-11-29 12:24:05 24 4
gpt4 key购买 nike

我有两张 table 。

  • biblio_contributor_data(cid,姓氏,名字)
  • biblio_contributor(nid,cid)

第二个表中的每个 cid 都有多个 nid。我想做的是将查询中的数据折叠到临时列或表中,如示例所示。

biblio_contributor_data

             +-----+----------+-----------+| cid | lastname | firstname |+-----+----------+-----------+| 1   | john     | grand     || 2   | James    | cook      || 3   | marco    | palo      |+-----+----------+-----------+

biblio_contributor

+-----+------+| nid |  cid |+-----+------+| 4   |   1  || 4   |   2  || 4   |   3  | | 5   |   2  |+-----+------+

如果可能的话,我想要一个具有以下内容的查询结果:如果可能的话,使用 SQL 语句:

+-----+------------------------------------+| nid | temporary column                   |+-----+------------------------------------+| 4   | john grand, James cook, marco palo || 5   | James cook                         |+-----+------------------------------------+

最佳答案

使用 CONCAT 组合 lastnamefirstname 并使用 GROUP_CONCAT 组合不同的行。

查询

SELECT t1.nid,
GROUP_CONCAT(CONCAT(t2.lastname,' ',t2.firstname)) AS `temporary column`
FROM biblio_contributor t1
JOIN biblio_contributor_data t2
ON t1.cid=t2.cid
GROUP BY t1.nid;

Fiddle demo

关于php - mysql 中的数据崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28606127/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com