SQL 连接 3 个表
我正在尝试在 1 个 SQL 查询中联接 3 个表,但我对于联接查询确实是一个“菜鸟”,这就是我到目前为止“创建”的内容:
SELECT m.Mod_ID as Modelnr, m.Mod_Naam as Modelnaam, m.Mod_Omschrijving as Omschrijving, m.Taal_ID as Taal, m.User_ID as Ontwerper
FROM Model as m
INNER JOIN Login as l ON m.User_ID = l.User_ID
INNER JOIN Taal as t ON m.Taal_ID = t.Taal_ID
WHERE m.User_ID = '" + userid + "'
数据库是在 Visual studio 内创建的(asp.net c# ),表:模型、Taal 和登录。
- 模型:Mod_ID、Mod_Naam、Mod_Omschrijving、Taal_ID、User_ID
- Taal:Taal_ID、Taal
- 登录:User_ID、U_Naam、U_Achternaam
表和表关系: http://i52.tinypic.com/2upxmbk.jpg
填充gridview的代码:
SqlCommand objCommand = new SqlCommand("SELECT m.Mod_ID as Modelnr, m.Mod_Naam as Modelnaam, m.Mod_Omschrijving as Omschrijving, m.Taal_ID as Taal, m.User_ID as Ontwerper from Model as m INNER JOIN Login as l ON m.User_ID = l.User_ID INNER JOIN Taal as t ON t.Taal_ID = t.Taal_ID WHERE m.User_ID = '" + userid + "' ", con);
dr = objCommand.ExecuteReader();
gvModel.DataSource = dr;
gvModel.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
gvModel.DataBind();
尝试在网格视图中显示模型,并使用来自 Taal (Taal.Taal) 的相应语言和来自 Login (Login.U_Naam) 的用户。
我得到的atm(数据库内模型中只有3行):http://i55.tinypic。 com/2wbrcd2.jpg
但我仍然在我的 gridview 中得到数字 ID。 谁能帮帮我吗?
谢谢 !
I'm trying to join 3 tables in 1 SQL query but I'm really a 'noob' with the join query, this is what I've 'created' so far :
SELECT m.Mod_ID as Modelnr, m.Mod_Naam as Modelnaam, m.Mod_Omschrijving as Omschrijving, m.Taal_ID as Taal, m.User_ID as Ontwerper
FROM Model as m
INNER JOIN Login as l ON m.User_ID = l.User_ID
INNER JOIN Taal as t ON m.Taal_ID = t.Taal_ID
WHERE m.User_ID = '" + userid + "'
The DB was created inside Visual studio (asp.net c#), the Tables : Model, Taal and Login.
- Model : Mod_ID, Mod_Naam, Mod_Omschrijving, Taal_ID, User_ID
- Taal : Taal_ID, Taal
- Login : User_ID, U_Naam, U_Achternaam
Tables & Relations : http://i52.tinypic.com/2upxmbk.jpg
Code behind to fill the gridview :
SqlCommand objCommand = new SqlCommand("SELECT m.Mod_ID as Modelnr, m.Mod_Naam as Modelnaam, m.Mod_Omschrijving as Omschrijving, m.Taal_ID as Taal, m.User_ID as Ontwerper from Model as m INNER JOIN Login as l ON m.User_ID = l.User_ID INNER JOIN Taal as t ON t.Taal_ID = t.Taal_ID WHERE m.User_ID = '" + userid + "' ", con);
dr = objCommand.ExecuteReader();
gvModel.DataSource = dr;
gvModel.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
gvModel.DataBind();
Trying to show Model in a gridview with the corresponding language from Taal (Taal.Taal) and user from Login (Login.U_Naam).
What I'm getting atm (only 3 rows in Model inside the DB) : http://i55.tinypic.com/2wbrcd2.jpg
But I'm still getting just number ID's in my gridview.
Anyone who can help me out ?
thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是在 SQL 工具中直接针对数据库测试此语句,还是直接在应用程序中测试它?在将其放入应用程序之前,请先针对数据库进行尝试。
您使用什么数据库管理系统?
Are you testing this statement in a SQL tool directly against the database or are you testing it directly in your application? Try it against the database first before you put it in your application.
What DBMS are you using?
在我看来,您的查询完全按照预期执行 - 问题出在查询的“选择”部分 - 一切都来自模型表。
完成“来自”和连接后,您选择的数据如下所示:
...您可以将其视为“一张大表”。因此,您还想从 User 和 Taal 表中选择内容 - 否则您就让数据库白白完成了所有连接它们的工作。我很确定你想要的是:
我还修复了你遇到的 SQL 注入问题,要在 .NET 中调用它,请执行以下操作:
It looks to me like your query is doing exactly what it's supposed to - the problem is in the "select" part of your query - everything is coming from the Model table.
After you do the "from" and the joins, the data that you're selecting looks like this:
...you can think of it kind of like "one big table". So you want to select things from the User and Taal tables too - otherwise you made the database do all the work of joining them for nothing. I'm pretty sure that what you want is:
I also fixed the SQL injection problem you have, to call this in .NET, do this:
尝试在没有内连接的情况下使用此命令
Try to use this command without inner join