select m.messageID,m.[addeddate],m.message,count(*) as Comments
from intranet.dbo.Blog_Messages AS m inner join
intranet.dbo.Blog_Comments AS c on c.messageid = m.messageid
group by m.messageID,m.[addeddate],m.message
need help converting this to linq to sql
from m in context.Blog_Messages
select new
{
MessageId = m.MessageID,
AddedDate = m.AddedDate,
Message = m.Message,
Comments = m.Blog_Comments.Count()
}
It isn't a direct conversion, but I think that is what you are after.
from m in db.Blog_Messages
join c in db.Blog_Comments on m.MessageID equals c.MessageID
group m by new {
m.MessageID,
m.AddedDate,
m.Message
} into g
select new {
MessageID = g.Key.MessageID,
AddedDate = g.Key.AddedDate,
g.Key.Message,
Comments = g.Count()
}
Dim Msg = From m In db2.Blog_Messages _
Group Join c In db2.Blog_Comments On m.MessageID Equals c.MessageID Into Comments = Group _
Join u In db2.users On m.userID Equals u.userid _
Select New With {
m.Title, m.Message, m.MessageID,
.CommentCount = Comments.Count(),
.date1 = m.AddedDate.ToShortDateString,
.Time = m.AddedDate.ToShortTimeString(),
.fullName = u.fname & " " & u.lname
}
this is what I came up with..
thanks,
chris
Related
subquery returns more than 1 row ? sulution ?
SELECT `t_files`.*, `t_users`.`username`,
(SELECT CONCAT(b.first_name, " ", b.last_name)
FROM t_files AS a
JOIN t_users as b
ON b.id = a.user_id
) as upload_by
FROM `t_files`
LEFT JOIN `t_files_permission`
ON `t_files_permission`.`id_files` = `t_files`.`file_id`
LEFT JOIN `t_users`
ON `t_users`.`id` = `t_files_permission`.`id_users`
WHERE `t_files`.`company_id` = '1'
AND `t_files_permission`.`id_users` = '59'
AND `is_deleted` =0
Add the condition in your sub query like below
SELECT t_files.*, t_users.username,
(SELECT CONCAT(b.first_name, " ", b.last_name)
FROM t_files AS a
JOIN t_users AS b ON b.id = a.user_id
WHERE a.id_users` = '59'
) as upload_by
FROM t_files
LEFT JOIN t_files_permission ON t_files_permission.id_files = t_files.file_id
LEFT JOIN t_users ON t_users.id = t_files_permission.id_users
WHERE t_files.company_id = '1' AND t_files_permission.id_users = '59' AND is_deleted =0
try this way
SELECT `t_files`.*, `t_users`.`username`,
(select CONCAT(b.first_name, " ", b.last_name)
from t_files as a
join t_users as b on b.id = a.user_id
where t.files.id=a.id) as upload_by
FROM `t_files` as
LEFT JOIN `t_files_permission` ON `t_files_permission`.`id_files` = `t_files`.`file_id`
LEFT JOIN `t_users` ON `t_users`.`id` = `t_files_permission`.`id_users`
WHERE `t_files`.`company_id` = '1' AND `t_files_permission`.`id_users` = '59' AND `is_deleted` =0
I think you don't need sub query. You can use join to resolve stuff.
I've got this tables:
users {id = int, name = varchar, pwd = char}
company {id = int, token = char, name = varchar}
user_company {id = int, id_usr = int, id_company = int, name_usr = varchar}
I'm trying to get the pwd from users and find out if the user is in the company with the token X from user_company
When I use this query
SELECT u.pwd,h.name_usr
FROM users u, company c
LEFT JOIN users_company h ON c.id = h.id_company AND u.id = h.id_usr
WHERE u.user_name = 'user#domain.com'
AND c.token = 'f30ea71e7a9d9f0a6710bb46537c0bde'
LIMIT 1;
I keep on getting 'Unknown column u.id in on clause' although u.id exists. What am I doing wrong? Thanks
SELECT u.pwd,h.nombre_usr
FROM company c LEFT JOIN users_company h ON c.id = h.id_company
LeFt join users u on u.id = h.id_usr
WHERE u.user_name = 'user#domain.com'
AND c.token = 'f30ea71e7a9d9f0a6710bb46537c0bde'
LIMIT 1;
The Error was beacause of Join Query . The way it was written is wrong. We can not apply join two tables with single table at same time.
sI've got the solution. Thank you all for the help.
SELECT u.pwd,h.name_usr
FROM users u
LEFT JOIN company c ON c.token = 'f30ea71e7a9d9f0a6710bb46537c0bde'
LEFT JOIN users_company h ON c.id = h.id_company AND u.id = h.id_usr
WHERE u.user_name = 'user#domain.com'
LIMIT 1;
SELECT *
FROM members
WHERE memberid IN (SELECT follows.followingid
FROM follows
WHERE follows.memberid = '$memberid'
AND follows.followingid NOT IN (SELECT memberid
FROM userblock))
AND memberid NOT IN (SELECT blockmemberid
FROM userblock
WHERE memberid = '$memberid')
The query above is taking nearly 4 seconds to execute in MySQL and I want to know if anyone has any suggestions on how I might improve/optimize it to achieve a faster execution time?
Replace the in clauses with joins. I think the following captures the logic. Note that the not in turns into a left join with a condition in the where clause finding a non-match
SELECT m.*
FROM members m
follow f
on m.memberid = f.followingid and
f.memberid = $memberid left join
userblock ubf
on follows.followingid = ubf.memberid left join
userblock ub
on m.memberid = ub.blockmemberid and
ub.memberid = '$memberid'
where ub.blockmemberid is null and
ubf.memberid is null;
This looks similar but you have less nested queries.
SELECT *
FROM members m
WHERE EXIST (SELECT f.followingid
FROM follows f
WHERE f.memberid = '$memberid'
AND f.followingid = m.memberid)
AND NOT EXIST (SELECT u.blockmemberid
FROM userblock u
WHERE (m.memberid = '$memberid'
AND u.blockmemberid = m.memberid)
OR
(u.blockmemberid = m.memberid
AND u.memberid = m.memberid) )
This is the logic I reversed-engineered from your code without seeing tables.
SELECT m.*
FROM members m
INNER JOIN follows f ON f.followingid = m.memberid AND
f.memberid = '$memberid'
LEFT OUTER JOIN userblock ub1 ON f.followingid = ub1.memberid
LEFT OUTER JOIN userblock ub2 ON m.memberid = ub2.blockmemberid AND
ub2.memberid = '$memberid'
WHERE ub1.memberid IS NULL AND ub2.blockmemberid IS NULL
see the query below i want all those records where status is either using or offering and userid is not equals to 1 and coursecode is cs3333
SELECT c.`id` AS courseid,
c.`userid` AS userid,
c.`coursecode`,
m.`title`,
s.`sem_name`,
p.`professor`,
st.`status`,
a.`author_name`,
q.`quality`,
m.`comments`,
m.`price`,
m.`material`
FROM sc_courses c
JOIN sc_c_materials m
ON c.`id`=m.`courseid`
JOIN sc_semesters s
ON s.`id`=c.`semid`
JOIN sc_professors p
ON c.`profid`=p.`id`
JOIN sc_status st
ON c.`statusid`=st.`id`
LEFT JOIN sc_authors a
ON m.`authorid`=a.`id`
JOIN sc_quality q
ON m.`qualityid`=q.`id`
WHERE st.`status` = "offering" OR st.`status` = "using" AND c.`userid` != "1" AND c.`coursecode` = "CS3333";
the query is running but it ignores both the and operators the above query return all records where userid is 1 and coursecode is not equal to cs3333 but i dont want these records plz tell me what am i doing wrong????
You just need brackets
WHERE (st.`status` = "offering" OR st.`status` = "using") AND c.`userid` != "1" AND c.`coursecode` = "CS3333";
That is because priority. You should place ( .. ) to change order of comparison.
(st.`status` = "offering" OR st.`status` = "using")
AND c.`userid` != "1"
AND c.`coursecode` = "CS3333"
opss stupid mistake here it is working with this query
SELECT c.`id` AS courseid,
c.`userid` AS userid,
c.`coursecode`,
m.`title`,
s.`sem_name`,
p.`professor`,
st.`status`,
a.`author_name`,
q.`quality`,
m.`comments`,
m.`price`,
m.`material`
FROM sc_courses c
JOIN sc_c_materials m
ON c.`id`=m.`courseid`
JOIN sc_semesters s
ON s.`id`=c.`semid`
JOIN sc_professors p
ON c.`profid`=p.`id`
JOIN sc_status st
ON c.`statusid`=st.`id`
LEFT JOIN sc_authors a
ON m.`authorid`=a.`id`
JOIN sc_quality q
ON m.`qualityid`=q.`id`
WHERE c.`userid` != "1" AND c.`coursecode` = "CS3333" AND st.`status` = "offering" OR
c.`userid` != "1" AND c.`coursecode` = "CS3333" AND st.`status` = "using";
I need to convert this sql query to linq to sql and the result returns a IEnumerable:
select VisualAidName, v.VisualAidID, vs.VisualAidStatusName,
br.BrandName, v.IsEnabled, v.VisualAidCode, v.DateApproved,
br.BrandID, type, UserFirstName+ ' ' + UserLastName as name, AreaID
from VisualAids v inner join VisualAidStatus vs
on v.VisualAidStatusId = vs.VisualAidStatusId
inner join brands br
on v.BrandID = br.BrandId
inner join VisualAids_Areas_Link vareas
on v.VisualAidID = vareas.VisualAidID
left join users us
on v.Owner = us.UserID
where
AreaID IN (
select areaid
from Users inner join Users_Area_Link
on Users.UserID = Users_Area_Link.UserID
where Users.UserID= 3
)
I did this:
IEnumerable<Visual_Aid> visualAll = from v in Context.VisualAids
join vs in Context.VisualAidStatus on v.VisualAidStatusId equals vs.VisualAidStatusId
join br in Context.Brands on v.BrandID equals br.BrandId
join us in Context.Users on v.Owner equals us.UserID into vadis
from x in vadis.DefaultIfEmpty()
select new Visual_Aid()
{
VisualAid_Name = v.VisualAidName,
VisualAid_Id = v.VisualAidID,
VisualAid_StatusName = vs.VisualAidStatusName,
VisualAid_BrandsName = br.BrandName,
VisualAid_IsEnabled = bool.Parse(v.IsEnabled.ToString()),
VisualAid_Code = v.VisualAidCode,
VisualAid_DateApp = v.DateApproved.ToString() ?? "",
VisualAid_BrandId = int.Parse(v.BrandID.ToString()),
VisualAid_Type = v.Type,
VisualAid_Owner = x.UserID == null ? "" : x.UserFirstName + " " + x.UserLastName
};
but I need to do the part of the subquery, ie, I need to include this:
where AreaID IN (
select areaid from Users inner join Users_Area_Link
on Users.UserID = Users_Area_Link.UserID where Users.UserID= 3
)
Anybody know how? thank you very much in advance
You can add this as a where statement:
.......
join us in Context.Users on v.Owner equals us.UserID into vadis
from x in vadis.DefaultIfEmpty()
where (
from user in Context.Users
join userArea in Users_Area_Link
on user.UserID equals userArea.UserID
where user.UserID==3
select userArea.areaid
).Contains(????.AreaID)
select new Visual_Aid()
{
.......