简体   繁体   English

MySQL - Query组合具有相同id的行并保留该id的所有条目,但作为一条记录

[英]MySQL - Query combine rows with same id and keep all entry for that id but as one record

I have been working on a table in a mysql database being hold locally on a wamp server I am using the phpmyadmin area with in wamp to run the querys. 我一直在一个在一个wql服务器本地保存的mysql数据库中的表上工作我正在使用phpmyadmin区域在wamp中运行查询。 I am trying to get the data to do the following: 我试图让数据执行以下操作:

Can anyone help I have a table with a number of records for plants. 任何人都可以帮助我有一个包含许多植物记录的表格。 A plant can have a number of names the table shows this as different records. 工厂可以有许多名称,表格将其显示为不同的记录。 The table is called new_plantsname 该表名为new_plantsname

plantid name
1       tree
1       rose
2       bush
3       tree
3       bush
3       rose

this continues for over 3000 records 这持续超过3000条记录

what i want is it to combined records with same plantid and show the different names in different columns: 我想要的是将记录与相同的plantid组合在一起,并在不同的列中显示不同的名称:

plantid name1 name2 name3 ...
1       tree  rose  NULL
2       shrub NULL  NULL
3       tree  rose  bush 

etc 等等

From a glance I believe a plant has no more than 4 names. 我一眼就相信一家工厂的名字不超过4个。

Can one help me the query to do this. 可以帮我查询来做到这一点。 I also want to save results to a new table 我还想将结果保存到新表中

Someone has given me the following for the answer: 有人给了我以下答案:

select plantid,
  max(case when nameRn = 'name1' then name end) Name1,
  max(case when nameRn = 'name2' then name end) Name2,
  max(case when nameRn = 'name3' then name end) Name3,
  max(case when nameRn = 'name4' then name end) Name4
from
(
  select plantid, name,
      concat('name', @num := if(@plantid = `plantid`, @num + 1, 1)) as nameRn,
      @plantid := `plantid` as dummy
  from
  (
    select plantid, name, @rn:=@rn+1 overall_row_num
    from yourtable, (SELECT @rn:=0) r
  ) x
  order by plantid, overall_row_num
) src
group by plantid;

This seem to work while no errors but it didnt combine the records it only kept the name of the first record with the id not the rest. 这似乎工作,虽然没有错误,但它没有结合记录,它只保留第一个记录的名称与id而不是其余的。 DATA USED: 使用数据:

plantid     name
    1       tree
    1       rose
    2       tree
    3       rose
    3       bush
    3       rose

RESULTS: 结果:

结果显示除了名字之外的所有人都添加了null

Can anyone help 谁能帮忙

The problem is that MySQL does not have a good way of enumerating rows. 问题是MySQL没有很好的枚举行的方法。 The use of the constant is not guaranteed to work, alas, according to the MySQL documentation. 根据MySQL文档,使用常量并不能保证能够正常工作。 It often does work, but it can also be problematic. 它通常可行,但也可能有问题。

I would suggest that you concatenate the names together into a single field. 我建议你将这些名称连接成一个字段。 The result would look like: 结果如下:

1     tree,rose
2     tree
3     tree,bush,rose

Using the SQL: 使用SQL:

select plantid, group_concat(name separator ',')
from t
group by plantid

If you really wanted the names in separate columns, two options come to mind. 如果你真的想要在不同的列中使用这些名称,我会想到两个选项。 One is to use the results from above and then parse the result into separate strings. 一种是使用上面的结果,然后将结果解析为单独的字符串。 The other alternative is to use a self-join and aggregation to calculate a sequential number, like this: 另一种方法是使用自联接和聚合来计算序列号,如下所示:

select p.plantid, p.name, count(*) as seqnum
from t p left outer join
     t pprev
     on p.plantid = pprev.plantid and
        p.name >= pprev.name
group by p.plantid, p.name

And use this as the subquery. 并使用它作为子查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM