I have the following query:
SELECT id, subject, date
FROM mail
WHERE date > some_date
I need to cross reference the id returned by the above query with another table to get the user's username. How can I do it in one query?
Right now I am doing this:
foreach (id in result)
{
query = String.Format("SELECT username from USERS where id = {0}", id);
}
Expected column output:
id subject date username
--------------------------------
SELECT m.id, u.username, m.subject, m.date
FROM mail m
inner join USERS u on m.id = u.id
WHERE m.date > @some_date
Of course, if you want it in C#/LINQ:
var msgs = from m in db.Mail
where m.date > some_date
select new
{
m.id,
m.subject,
m.date,
m.Users.Username
};
Use:
SELECT m.id,
m.subject,
m.date,
u.username
FROM MAIL m
JOIN USERS u ON u.id = m.id
AND u.id = {0}
WHERE m.date > some_date
SELECT username FROM users WHERE id IN (
SELECT id FROM mail WHERE date > some_date
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.