[英]Select data with an AND clause from one table and selecting just one column from another?
I am trying to select two columns from one table with an AND clause while also just selecting a column from another table. 我试图从一个带有AND子句的表中选择两列,同时也从另一张表中选择一列。
The process I want to achieve is that the query reads one table for a username and password using the AND clause to validate whether the entered credentials are correct and another part of the process which just reads another table for one column (the column in this table is associated with the same username as the above table) 我要实现的过程是查询使用AND子句读取用户名和密码的一个表,以验证输入的凭据是否正确,而过程的另一部分仅读取一个表的另一表(此表中的列)与上表相同的用户名关联)
SQL in C# for selecting data in one table C#中的SQL用于在一个表中选择数据
SELECT TwyID, PW
FROM LoginTbl
WHERE TwyID = '"+TwyID_txt.Text+"'
AND PW = '"+Pw_txt.Text+"'"
For selecting data in the other table 用于选择其他表中的数据
SELECT Class
FROM UserInfo
WHERE TwyID = '"+TwyID_txt.Text+"'
I simply just like to merge these two SQL statements into one. 我只是想将这两个SQL语句合并为一个。
I've looked at the user of JOINS but they don't seem to work how ever I try to implement them. 我已经看过JOINS的用户,但是我尝试实现它们的方式似乎并不起作用。
I tried one solution being: 我尝试了一种解决方案是:
SELECT
TwyID, PW,
(SELECT Class
FROM UserInfo
WHERE TwyID = '"+TwyIDTextBox.Text+"')
FROM LoginTbl
WHERE TwyID = '" + TwyIDTextBox.Text + "'
AND PW = '" + PasswordTextBox.Text + "'";
Although this was to no avail. 虽然这没有用。
I'm sure I am being dim and the solution is rather simple, but trying new things never goes without its problems right? 我敢肯定,我是昏暗的,解决方案相当简单,但是尝试新事物永远不会没有问题,对吗? :-)
:-)
Hope anyone can help. 希望任何人都能提供帮助。
Thanks, Liam 谢谢,利亚姆
You should use INNER JOIN here: 您应该在此处使用INNER JOIN:
SELECT L.TwyID, L.PW
WHERE L.TwyID = '" + TwyIDTextBox.Text + "'
AND L.PW = '" + PasswordTextBox.Text + "'"
FROM LoginTbl L INNER JOIN UserInfo U ON L.TwyID = U.TywID
On a side note, you should never ever create SQL directly from user input. 附带说明,永远不要直接从用户输入创建SQL。 There is a huge security risk (SQL Injection) in doing that.
这样做存在巨大的安全风险(SQL注入)。
Why don't try Join statement? 为什么不尝试使用Join语句?
Select T1.TwyID,T1.PW,T2.Class
From LoginTbl as T1
Join UserInfo as T2 on T1.TwyID = T2.TwyID
Where T1.TwyID = '"+TwyID_txt.Text+"' AND T1.PW = '"+Pw_txt.Text+"'"
if you don't want to use join you can try this 如果您不想使用加入,可以尝试一下
select t.Id,t.Password,class
from
(select TwyID as 'Id', PW as 'Password' from LoginTable
where TwyID = '"+TwyIDTextBox.Text+"' and PW = '" + PasswordTextBox.Text + "')as t,UserInfo u
where u.Id= '"+TwyIDTextBox.Text+"'
but inner join is better. 但是内部联接更好。
Turns out a LEFT JOIN was what I needed. 原来我需要的是LEFT JOIN。
SELECT *
FROM LoginTbl
LEFT JOIN UserInfo
ON LoginTbl.TwyID = UserInfo.TwyID
WHERE LoginTbl.TwyID = '"+TwyIDTextBox.Text"'
AND LoginTbl.PW = '"+PasswordTextBox.Text+"'
This solution selects all fields in the UserInfo table where the TwyID matched the TwyID from the LoginTbl. 此解决方案从LoginTbl中选择TwyID与TwyID匹配的UserInfo表中的所有字段。 From this I was able to read the Class field from the UserInfo table into a variable and it worked!
由此,我能够将UserInfo表中的Class字段读入一个变量,并且可以正常工作!
Thank you everyone for all your answers, opened mind to play with the statement eventually getting it to work. 谢谢大家的所有回答,我们为解决该声明最终发挥作用打开了胸怀。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.