How to fetch all post and post comments posted by any user and his friends - asp.net

I am working on an asp.net application "social networking site".in this i am working on the following tables:-
Friends(ID,User_ID,Friend_ID)
Users(User_ID,User_Name,Full_Name,DOB,Gender)
User_Detail(ID,User_ID,Image,Father_Name,Mother_Name,Location,JobProfile)
Wall(Wall_ID,Wall_Content,TimeAgo,User_ID)
Wall_Comments(Comment_ID,Wall_ID,Comments,Comment_Date,User_ID)
I want to select all records of Tables- Wall and Wall_Comments where User_ID=5 and all friends of User_ID=5
I have done following Sql Query:
SELECT DISTINCT TOP (30) wall.wall_content,
wall.wall_id,
wall.user_id,
wall.timeago,
users.user_name,
users.full_name,
users.user_id AS UID,
wall_comments.comments,
wall_comments.comment_id,
CONVERT(NVARCHAR(1000), user_detail.image, 0) AS Image
FROM friends
INNER JOIN users
ON friends.user_id = users.user_id
INNER JOIN wall
ON users.user_id = wall.user_id
INNER JOIN user_detail
ON users.user_id = user_detail.user_id
left outer join wall_comments
ON wall.wall_id = wall_comments.wall_id
WHERE ( users.user_id IN (SELECT friend_id
FROM friends AS Friends_1
WHERE ( user_id = 5 )) )
UNION
SELECT wall.wall_content,
wall.wall_id,
wall.user_id,
wall.timeago,
users.user_name,
users.full_name,
users.user_id AS UID,
wall_comments.comments,
wall_comments.comment_id,
CONVERT(NVARCHAR(1000), user_detail.image, 0) AS Image
FROM wall
INNER JOIN users
ON wall.user_id = users.user_id
INNER JOIN user_detail
ON users. user_id = user_detail.user_id
left outer join wall_comments
ON wall.wall_id = wall_comments.wall_id
WHERE ( users.user_id = 5 )
ORDER BY wall.wall_id DESC
But it is showing only one record of table wall and Wall_Comments of user_ID-5 it is not showing his friends Wall and their post.and If their is not any comment on any wall then it is not showing.please Help me someone.

Something like this?
select *
from Friends
inner join Wall_Comments On (Wall_Comments.User_ID = Friends.User_ID or Wall_Comments.User_ID = Friends.Friend_ID)
inner join Users On Users.User_ID = Wall_Comments.User_ID
where Friends.User_ID = 5

Related

Nopcommerce export user roles

I'm trying to get a list of ALL user roles for each user through the nopcommerce database. i just need the Customer.Id, Customer.Username, Customer.Email and Customer.Role (some have multiple roles)
This is all jacked up.
SELECT Customer.Id, Customer.Username, Customer.Email, CustomerRole.Name
FROM ((Customer
INNER JOIN CustomerRole ON CustomerRole.Id = Customer.Id)
INNER JOIN Customer_CustomerRole_Mapping ON Customer_CustomerRole_Mapping.CustomerRole_Id = CustomerRole.Id)
Any help is greatly appreciated!
I think you need a LEFT OTUER JOIN because in your DB can be user without role; in this case you can use this:
SELECT DISTINCT CUST.Id, CUST.Username, CUST.Email, CR.Id, CR.Name,
CCRM.CustomerRole_Id ,
FROM CUSTOMER CUST
LEFT OUTER JOIN Customer_CustomerRole_Mapping CCRM
ON CUST.ID = CCRM.Customer_Id
LEFT OUTER JOIN CustomerRole CR
ON CCRM.CustomerRole_Id = CR.Id
Also, you can have only one row per user with a column with his roles, like this:
SELECT DISTINCT CUST.Id, CUST.Username, CUST.Email,
Roles = STUFF((
SELECT ',' + CAST(CR1.Name as varchar(4000))
FROM CustomerRole CR1
LEFT OUTER JOIN Customer_CustomerRole_Mapping CCRM1
ON CCRM1.CustomerRole_Id = CR1.Id
LEFT OUTER JOIN CUSTOMER CUST1
ON CUST1.ID = CCRM1.Customer_Id
WHERE CUST1.Id = CUST.Id
FOR XML PATH('')
), 1, 1, '')
FROM CUSTOMER CUST
LEFT OUTER JOIN Customer_CustomerRole_Mapping CCRM
ON CUST.ID = CCRM.Customer_Id
LEFT OUTER JOIN CustomerRole CR
ON CCRM.CustomerRole_Id = CR.Id
Depending of your using, last query can be a little bit slow if you have a lot of customers.

SQFlite - delete row through join throws: near LEFT syntax error

I am currently working on a shopping list, where the user can have multiple lists and is able to add different items on the different tables. It is also possible to check those items. Everything works fine, but when I want to delete some of the checked items it throws me an error:
D/Sqflite (10847): [Thread[Sqflite,5,main]] DELETE FROM shoppingTitles
LEFT JOIN shopping ON shoppingTitles.idShopping = shopping.id LEFT
JOIN listTitles ON shoppingTitles.idTitles = listTitles.id WHERE
shopping.checked = 1 AND listTitles.titleName = ? [Einkaufsliste]
E/SQLiteLog(10847): (1) near "LEFT": syntax error
This is my sql query:
DELETE
FROM shoppingTitles
LEFT JOIN shopping ON shoppingTitles.idShopping = shopping.id
LEFT JOIN listTitles ON shoppingTitles.idTitles = listTitles.id
WHERE shopping.checked = 1
AND listTitles.titleName = "Liste"
I hope somebody is able to help me. Thanks in advance XD
FOUND MY SOLUTION BASED ON THE ANSWER provided by Akn
DELETE FROM shoppingTitles
WHERE idShopping IN (
SELECT shoppingTitles.idShopping
FROM shoppingTitles, shopping, listTitles
WHERE shopping.id = shoppingTitles.idShopping
AND shoppingTitles.idTitles = listTitles.id
AND shopping.checked = 1
AND listTitles.titleName = "Liste"
)
Try it like this:
DELETE S
FROM
shoppingTitles S
LEFT JOIN shopping ON S.idShopping = shopping.id
LEFT JOIN listTitles ON S.idTitles = listTitles.id
WHERE
shopping.checked = 1
AND listTitles.titleName = "Liste"
Update:
Guess, there is no support for JOINs within DELETE statements so you better find a way to use SELECT statement.
Below SELECT statement will return an id. If it matches with your myIdShopping, it'll be deleted. I couldn't check/run the code. Hope it works.
DELETE FROM
shoppingTitles
WHERE
myIdShopping IN(
SELECT
shoppingTitles.idShopping
FROM
shoppingTitles
LEFT JOIN shopping ON shoppingTitles.idShopping = shopping.id
LEFT JOIN listTitles ON shoppingTitles.idTitles = listTitles.id
WHERE
shopping.checked = 1
AND listTitles.titleName = "Liste"
)

QueryBuilder - Use Select in an innerJoin

can someone help me.
I need to change my sql query to a querybuilder. I don't know how to use select inside a innerJoin.
Here my sql query:
SELECT
user.id,
analytic_youtube_demographic.age_group,
analytic_youtube_demographic.percentage
FROM
user
INNER JOIN user_youtube ON user_youtube.id = user.id
INNER JOIN analytic ON analytic.id = user_youtube.analytic_id
INNER JOIN analytic_youtube_demographic ON analytic_youtube_demographic.analytic_id = analytic.id
INNER JOIN
(
SELECT analytic_youtube_demographic.id as id, MAX(analytic_youtube_demographic.percentage) max
from user
INNER JOIN user_youtube ON user_youtube.id = user.id
INNER JOIN analytic ON analytic.id = user_youtube.analytic_id
INNER JOIN analytic_youtube_demographic ON analytic_youtube_demographic.analytic_id = analytic.id
GROUP BY user.id
) AS T ON analytic_youtube_demographic.percentage = T.max
WHERE analytic_youtube_demographic.age_group IN ('18-24')
Thanks a lot for your help.

how to search user bookmark only?

I am trying to write sql query to show search throw resource that user's bookmarked. my problem is when user type keyword the search will search all resource on the website and including his bookmark I want to be able to search user bookmark
SELECT
a.Id ,
a.summary ,
a.pageId ,
a.name ,
a.createdOn ,
a.userID ,
Total
FROM Resources AS a
LEFT JOIN Topics_Resources AS b ON a.Id = b.ResourceID
LEFT JOIN Skills_Resources AS c ON a.Id = c.ResourceID
INNER JOIN Modules AS m ON a.ModuleId = m.ModuleID
INNER JOIN ContentItems AS ci ON m.ModuleID = ci.ModuleID
INNER JOIN Tabs AS t ON t.TabID = ci.TabID
INNER JOIN TabModules AS tb ON t.TabID = tb.TabID
INNER JOIN Bookmarks ON a.Id = Bookmarks.resourceID
INNER JOIN Bookmarks AS Bookmarks_1 ON a.Id = Bookmarks_1.resourceID
INNER JOIN
(
SELECT
r.Id , COUNT(l.resourceId) Total
FROM resources r
LEFT JOIN likes l ON r.Id = l.resourceid
GROUP BY r.Id
)
l ON a.Id = l.Id
LEFT OUTER JOIN HtmlText AS h ON tb.ModuleID = h.ModuleID
WHERE (h.content LIKE '%winter%')
OR (t.description LIKE '%winter%')
OR (t.keywords LIKE '%winter%')
OR (a.summary LIKE '%winter%')
AND Bookmarks.userID = '695A8626-A28B-4308-AD9B-D5AC576B4E5A'
GROUP BY a.Id ,
a.summary ,
a.pageId ,
a.name ,
a.createdOn ,
a.userID ,
Total
ORDER BY a.createdOn DESC
try enclosing every OR in brackets
SELECT a.Id, a.summary, a.pageId, a.name, a.createdOn,a.userID,Total
FROM Resources AS a
LEFT JOIN Topics_Resources AS b ON a.Id = b.ResourceID
LEFT JOIN Skills_Resources AS c ON a.Id = c.ResourceID
INNER JOIN Modules AS m ON a.ModuleId = m.ModuleID
INNER JOIN ContentItems AS ci ON m.ModuleID = ci.ModuleID
INNER JOIN Tabs AS t ON t.TabID = ci.TabID
INNER JOIN TabModules AS tb ON t.TabID = tb.TabID
INNER JOIN Bookmarks ON a.Id = Bookmarks.resourceID
INNER JOIN Bookmarks AS Bookmarks_1 ON a.Id = Bookmarks_1.resourceID
INNER JOIN (select r.Id, count(l.resourceId) Total
from resources r
left join likes l on r.Id = l.resourceid
group by r.Id) l
on a.Id = l.Id
LEFT OUTER JOIN HtmlText AS h ON tb.ModuleID = h.ModuleID
where ((h.content like '%winter%') or ( t.description like '%winter%') or ( t.keywords like '%winter%') or ( a.summary like '%winter%'))
and Bookmarks.userID='695A8626-A28B-4308-AD9B-D5AC576B4E5A'
GROUP BY a.Id, a.summary, a.pageId, a.name, a.createdOn,a.userID,Total
ORDER BY a.createdOn DESC
Note: if you expect big quantity of data, this kind of query will last hours. you should add FULLTEXT

Modify the sql query in asp.net?

I am using sql server 2005. In this query i want the log-in user detail should also display but it is not displaying .
So please modify the query so that log-in user detail should also display with the help of session[userId].tostring();
Query written by me is:
SELECT DISTINCT MUDMEMBER.PK_ID, MUDMEMBER.EMPLOYEE_ID, LKB.BANK_NAME, MUHD.SALARY_ACCOUNT_NO, MUHD.PF_NO,
MUHD.PAN_NO, MUHD.GENDER, LKD.DESIGNATION_NAME FROM M_LEADERLED MLL INNER JOIN M_USER_DETAILS MUDMEMBER ON
MLL.LED_ID = MUDMEMBER.PK_ID AND MLL.START_DATE <= Getdate() AND MLL.END_DATE > Getdate() AND MLL.LEADER_ID = '1' LEFT OUTER JOIN
M_USER_HR_DETAILS MUHD ON MUHD.FK_USER_ID = MUDMEMBER.PK_ID AND MUHD.IS_ACTIVE =1 LEFT OUTER JOIN
LK_BANKS LKB ON LKB.PK_ID = MUHD.FK_BANK_ID LEFT OUTER JOIN LK_DESIGNATION LKD ON
LKD.DESIGNATION_VALUE = MUHD.FK_DESIGNATION_VALUE AND LKD.FK_ORGANIZATION_ID = 1 AND LKD.IS_ACTIVE = 1 WHERE MUDMEMBER.ACTIVE = 1
ASP.Net Page you can fetch the loggedin user's detail as follows:
SELECT DISTINCT MUDMEMBER.PK_ID, MUDMEMBER.EMPLOYEE_ID, LKB.BANK_NAME, MUHD.SALARY_ACCOUNT_NO, MUHD.PF_NO,
MUHD.PAN_NO, MUHD.GENDER, LKD.DESIGNATION_NAME FROM M_LEADERLED MLL INNER JOIN M_USER_DETAILS MUDMEMBER ON
MLL.LED_ID = MUDMEMBER.PK_ID AND MLL.START_DATE <= Getdate() AND MLL.END_DATE > Getdate()
AND MLL.LEADER_ID = '1' LEFT OUTER JOIN
M_USER_HR_DETAILS MUHD ON MUHD.FK_USER_ID = MUDMEMBER.PK_ID AND MUHD.IS_ACTIVE =1
LEFT OUTER JOIN
LK_BANKS LKB ON LKB.PK_ID = MUHD.FK_BANK_ID LEFT OUTER JOIN LK_DESIGNATION LKD ON
LKD.DESIGNATION_VALUE = MUHD.FK_DESIGNATION_VALUE AND LKD.FK_ORGANIZATION_ID = 1
AND LKD.IS_ACTIVE = 1
WHERE MUDMEMBER.ACTIVE = 1 AND MUDMEMBER.PK_ID ="+Convert.ToInt32(Session["UserId"])+"

Resources