-5

嘿,所以我是 sql 新手,正在构建一个库存管理系统。因此,对于数据库,我陷入了困境,此时我需要将各种用户 ID 插入公司中的不同团队,因此当我尝试将多个 int 值分配给特定团队时出现问题。数据库是在一种需要 TeamId 和相应的 UserId 的方式。

4

2 回答 2

1

可能这对您有帮助-

DECLARE @temp TABLE (ID INT)

-- For 2008 and higher

INSERT INTO @temp (ID)
VALUES (1), (2), (3)

-- For 2005 and higher

INSERT INTO @temp (ID)
SELECT ID
FROM (
    SELECT ID = 4
    UNION ALL
    SELECT 5
    UNION ALL
    SELECT 6
) t

SELECT * 
FROM @temp

更新(评论@Sivakumar:“1,19 不是整数。它是 varchar。”):

DECLARE @temp TABLE (txt varchar(500))

INSERT INTO @temp (txt)
VALUES ('1,19'), ('2,18')

SELECT t.c.value('.', 'INT')
FROM (
    SELECT txml = CAST('<t>' + REPLACE(txt, ',', '</t><t>') + '</t>' AS XML)
    FROM @temp
) a
CROSS APPLY txml.nodes('/t') AS t(c)
于 2013-05-24T09:58:40.513 回答
0

这听起来像是一对多的关系:一个团队可以有零个或多个用户。

因此,在 User 中创建一个引用 Team 主键的外键列。

像这样的东西(检查我的语法):

create table team (
    id int not null auto_increment,
    primary key(id);
);

create table user (
    id int not null auto_increment,
    tid int,
    primary key(id),
    foreign key tid references(team);
);

select * 
from team t
join user u
on t.id = u.tid;
于 2013-05-24T09:57:52.553 回答