mysql 按设置 id 进行分组

发布于 2024-11-03 10:32:58 字数 1069 浏览 0 评论 0原文

MYSQL 数据库:

我有一个数据表,需要将其放入两个表中。一个表包含人和动物。每一张记录都是一个人和一只动物。我将其更改为多表数据库,并希望按人员姓名进行分组,然后为该组提供一个 id(如客户 id),然后在我的另一个表中将客户 id 传递给 idcustomer 以连接两个表。为了简化这一点,我不介意这些新创建的 id 是否位于具有新列名称的单个表中。我可以事后将它们导出并导入。

真正的问题是,如何按客户名称创建一个组,提供该客户和 ID,然后在列中使用相同的 ID 来进行连接。

描述一下这个计划:

我接手了一个项目。目前的数据库是一张表。在这张表中:

persons name, address, childsname, description of child

至少希望它开始的是:

id_person, person name, childsname, childparent, description of child.

人和孩子的 id 应该相同。当我分解表,并将子信息放入子表中时,孩子将有一个父ID。

我还是打算有两张桌子。但我可以导出并创建表,我的问题是将父 ID 分配给具有当前数据的子列。

几行的一个例子是:

person_name, address, childsname, description

mark twain, 23 st., Buckweat, short smart kid

mark twain, 23 st., Daniel, short kinda smart

相反,我想要两张表,一张为父母,另一张为他们的孩子。

该数据库的设置方式是,如果一个人有多个孩子,则会有另一行列出所有相同的信息。

我的计划是拥有多个带有连接的表。

原始数据库没有索引或唯一标识符。

我想要做的是循环记录,因为没有唯一的 id,如果客户名称相同,意味着它们被列出两次,那么它们必须有多个子项。

在这种情况下,我希望能够浏览数据库并为父母分配一个 id。并且还有另一个名为parentid 的列,它将成为子表。

MYSQL Database:

I have a table of data that I need to put into two tables.The one table contains persons and an animal. Every record is a person and an animal. I am chaning this to a multi table database and want to group by the persons name, and then give the group an id (like a customer id) and then in my other table pass the customer ID to the idcustomer to join the two tables. To simplfy this i dont mind if these newly created ids are in the single table with new column names. I can after the fact export them out and import them.

The question really is, how can I create a group by customer name, give that customer and ID and then use that same id in a column to do the join.

To describe the scheme:

I have taken over a project. The database as of now is one table. In this one table is:

persons name, address, childsname, description of child

What would like it to be at least to start with is:

id_person, person name, childsname, childparent, description of child.

The id of the person and the childsparent should be the same. When I break the table down, and put the child information in the child table, the child will have a parent id.

I still plan on having two tables. But I can export and create the tables, my problem is assiging the parent id to the child column with the current data.

An example of a couple rows would be:

person_name, address, childsname, description

mark twain, 23 st., Buckweat, short smart kid

mark twain, 23 st., Daniel, short kinda smart

Instead i would like to have 2 tables, one for the parents and the other table is their children.

The way this database was setup, if one person has more than one child, there is another row that lists all of the same information.

What I have planned is to have multiple tables with a join.

The original database has no index, or unique identifier.

What I want to do is loop through the records, since there is no unique id, if the customer name is identical, meaning they are listed twice, then they must have more than one child.

In that case, i want to be able to go through the database and assign a id for the parents. and also ad another colum called parentid, which will be the child table.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开浅夏 2024-11-10 10:32:58

要创建您需要的表,您可以使用临时表 - 您将在其中插入所有父级名称并为其提供 ID。然后您可以更新现有表:

CREATE TABLE name_to_id (
`id` INT(11) AUTO_INCREMENT,
`name` VARCHAR(256),
PRIMARY KEY (`id`));

INSERT INTO name_to_id (name)
SELECT DISTINCT name FROM my_table;

ALTER TABLE my_table
ADD COLUMN id INT(11) FIRST,
ADD COLUMN parent_id INT(11) AFTER childsname;

UPDATE my_table t
JOIN name_to_id n ON t.name = n.name
SET t.id = n.id, t.parent_id = n.id;

要创建父表和子表,您可以使用:

CREATE TABLE parents AS
SELECT id, name, address FROM my_table
GROUP BY id;

CREATE TABLE children AS
SELECT childsname, parent_id, description
FROM my_table;

您可能希望稍后更改这些表以添加主键和其他所需的索引。

To create the table you need you can use a temporary table - to which you will insert all parent names and give them IDs. Then you can update the existing table:

CREATE TABLE name_to_id (
`id` INT(11) AUTO_INCREMENT,
`name` VARCHAR(256),
PRIMARY KEY (`id`));

INSERT INTO name_to_id (name)
SELECT DISTINCT name FROM my_table;

ALTER TABLE my_table
ADD COLUMN id INT(11) FIRST,
ADD COLUMN parent_id INT(11) AFTER childsname;

UPDATE my_table t
JOIN name_to_id n ON t.name = n.name
SET t.id = n.id, t.parent_id = n.id;

To create the parents and children separate tables you can use:

CREATE TABLE parents AS
SELECT id, name, address FROM my_table
GROUP BY id;

CREATE TABLE children AS
SELECT childsname, parent_id, description
FROM my_table;

You would probably want to ALTER those tables later to add a primary keys and other needed indexes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文