嗨我有两个表用户和图片我试图从我的图片表中提取路径名(userid = 1)与pk相关,但我不确定如何从用户表中选择firstname等并从中选择picturepath图片表?
我还想知道如果我使用注释掉的行是否会正确设置img路径?
OdbcCommand cmd = new OdbcCommand("SELECT User.FirstName, User.SecondName, User.Aboutme, User.DOB, Pictures.picturepath FROM User LEFT JOIN Pictures ON User.UserID = Pictures.UserID WHERE User.UserID=1", cn);
OdbcDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Name.Text = String.Format("{0} {1}", reader.GetString(0), reader.GetString(1));
Aboutme.Text = String.Format("{0}", reader.GetString(2));
Age.Text = String.Format("{0}", reader.GetString(3));
Image1.ImageUrl = String.Format("{0}", reader.GetString(4));
}
}
}
答案 0 :(得分:2)
要从一个查询中的两个相关表中获取数据,您可以使用JOIN:
SELECT
User.FirstName,
User.SecondName,
User.Aboutme,
User.DOB,
Picture.PicturePath
FROM User
LEFT JOIN Pictures
ON User.UserID = Pictures.UserID
WHERE User.UserID=1
然后,是的,您可以取消注释最后一行。