[英]SQL syntax using C#
I'm trying to insert values into some table with this code using C# and SQL: 我正在尝试使用C#和SQL使用此代码将值插入某些表中:
string query1 = "select PlayerID from Table1";
string query2 = "select GameID from Table2";
string q = "insert into Table3 (PlayerID, GameID) values(" + query1 + "," + query2 + ")";
Is it possible? 可能吗?
Edit: 编辑:
I have 3 tables: 我有3张桌子:
playersTbl
- with auto increment id (primary key) playersTbl
具有自动增量ID(主键) gameTbl
- with auto increment id (primary key) gameTbl
具有自动增量ID(主键) JoinTbl
When the game is starting, the user can register some players to the same game, so in the same game lets say there are 4 players. 游戏开始时,用户可以向同一游戏注册一些玩家,因此在同一游戏中,假设有4位玩家。
When I'm adding those 4 players the playersTbl
will look like: 当我添加这4个玩家时,
playersTbl
将如下所示:
ID | first name|
----------------
1 | p1 |
2 | p2 |
3 | p3 |
4 | p4 |
and the gameTbl
will look like: 并且
gameTbl
将如下所示:
ID | Date | Hour|
-----------------
1 | | |
So what I'm trying to do is the Table3
will look like: 所以我想做的是
Table3
看起来像:
PlayerID | GameID|
------------------
1 | 1 |
2 | 1 |
3 | 1 |
4 | 1 |
Now when I'm starting a new game with one player 现在,当我与一个玩家开始新游戏时
I'm adding the player to the playersTbl
, and the game to the gameTbl
, but now the third table will look like 我加入玩家的
playersTbl
,游戏到gameTbl
,但现在的第三个表的样子
PlayerID | GameID|
------------------
1 | 1 |
2 | 1 |
3 | 1 |
4 | 1 |
5 | 2 |
to be more clear, I don't want that players which are registered in the second game will be in the first game.. 更清楚地说,我不希望在第二场比赛中注册的玩家会在第一场比赛中。
use INSERT INTO..SELECT
statement, 使用
INSERT INTO..SELECT
语句,
INSERT INTO Table3(PlayerID, GameID)
SELECT a.PlayerID, b.GameID
FROM Table1 a CROSS JOIN Table2 b
what the query does is it produces cartesian product from both tables. 查询的作用是从两个表中产生笛卡尔积。
Consider The following example, 考虑以下示例,
Table1 表格1
PlayerID
========
1
2
3
Table2 表2
GameID
=======
10
11
12
when the query below is executed, 当执行以下查询时,
SELECT a.PlayerID, b.GameID
FROM Table1 a CROSS JOIN Table2 b
it produces the following result, 它产生以下结果,
PlayerID GameID
========= ======
1 10
1 11
1 12
2 10
2 11
2 12
3 10
3 11
3 12
您应该能够利用INSERT INTO SELECT方法,如下所示:
INSERT INTO Table3 (PlayerID, GameID) SELECT Table1.ID, Table2.ID FROM Table1, Table2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.