SQL选择两个userID作为不同的别名

时间:2017-08-24 20:51:38

标签: c# sql

我无法完成以下任务:

我有两张桌子:

User:
  -uid
  -username

challenge:
   -cID
   -challengerID
   -challengedID

现在我想在insert into应用中运行c#(例如!挑战user1 user2

现在SQL是:

insert into challenge (challengerID, challengedID) 
where challengerID = id of user1 and challengedID = id of user2. 

如何抓住这两个user IDs并将它们存储在挑战表的不同字段中?

1 个答案:

答案 0 :(得分:4)

使用join

insert into challenge (challengerID, challengedID) 
    select u1.id, u2.id
    from users u1 join
         users u2
         on u1.username = 'user1' and u2.username = 'user2';

编写查询时,应使用名称参数。不要使用名称来查询查询字符串。