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
Related
I have an issue with the IN instruction for a field...maybe you can see better than me what's happening.
The issue is on the waves parameter.
I got this in my database it's a configuration field...and this field contain a json
{
"typeId":"10,57",
"codeVague":"ITM1702A,ITMTEST"
}
This request inside sql return results:
SELECT v.id,v.`vague_code`, v.`date_fin_ultime`,m.*
FROM Vague v
INNER JOIN Enquete e ON e.`vague_id` = v.`id`
INNER JOIN Mission m ON m.id = e.`mission_id`
INNER JOIN Contrat c ON c.id = m.`contrat_id`
INNER JOIN `User` u ON u.`enqueteur_id` = e.`enqueteur_id`
INNER JOIN `PointDeVente` p ON p.id = e.`pdv_id`
WHERE v.`vague_code` IN ('ITM1702A','ITMTEST')
AND type_id IN (10,57)
But when I try to do the same from my symfony controller...I got an empty result
$sql="SELECT v.id,v.codeVague, v.date_fin_ultime,c.distance,p.adresse1,p.code_postal,p.ville,m
FROM McInvestigatorBundle:Vague v
INNER JOIN McInvestigatorBundle:Enquete e WITH e.vague_id = v.id
INNER JOIN McInvestigatorBundle:Mission m WITH m.id = e.mission_id
INNER JOIN McInvestigatorBundle:Contrat c WITH c.id = m.contrat
INNER JOIN McInvestigatorBundle:User u WITH u.enqueteur_id = e.enqueteur_id
INNER JOIN McInvestigatorBundle:PointDeVente p WITH p.id = e.pdv_id
WHERE v.codeVague IN (:waves)
AND e.type_id IN (:type_id)
AND m.enqueteur_id=:enq_id
ORDER BY m.date_rea_prev ASC";
$results= $em->createQuery($sql)->setParameters([
'waves' => $waves,
'type_id' => $type_id,
'enq_id' => $enq_id,
])->getResult();
The strange thing is that I got no issue with type_id, it works fine.
But, the waves field it's not the same story....
In fact, it works only if I have only one value in the json
"codeVague":"ITM1702A" is working
"codeVague":"ITM1702A,ITMTEST" is not working.
I already tried those strings in $waves.
"ITM1702A,ITMTEST" and "'ITM1702A','ITMTEST'" and "'ITM1702A,ITMTEST'"
Thanks you for your help !
EDIT: Problem solved, thanks Veve.
How to query in MySQL to show each menu's submenu from wordpress database.
Actually I need sql query not php code.
As like I have menu and it is like this
This may help.
select * from (select d.*,e.name,e.slug from(SELECT p.id,txr.term_taxonomy_id, p.post_title, p.post_name, p.menu_order, n.post_name as n_name, n.post_title as n_title, pp.meta_value as menu_parent,pt.meta_value as type
FROM wp_term_relationships as txr
INNER JOIN wp_posts as p ON txr.object_id = p.ID
LEFT JOIN wp_postmeta as m ON p.ID = m.post_id
LEFT JOIN wp_postmeta as pl ON p.ID = pl.post_id AND pl.meta_key = '_menu_item_object_id'
LEFT JOIN wp_postmeta as pp ON p.ID = pp.post_id AND pp.meta_key = '_menu_item_menu_item_parent'
LEFT JOIN wp_postmeta as pt ON p.ID = pt.post_id AND pt.meta_key = '_menu_item_object'
LEFT JOIN wp_posts as n ON pl.meta_value = n.ID
WHERE p.post_status='publish'
AND p.post_type = 'nav_menu_item' AND m.meta_key = '_menu_item_url'
ORDER BY pp.meta_value) d
LEFT JOIN wp_terms as e on d.term_taxonomy_id=e.term_id) i where i.term_taxonomy_id = MENU ID
I have the this SQL query in a typed dataset in asp.net 2005 and SQL Server 2008 R2.
i.e I have used in Typed Dataset -- "Add Query" -- "Use Sql Statements" and used the below script, then I returned a Datatable. ("Fill a Datatable" checkbox was unchecked")
SELECT DISTINCT
ISNULL(Projects.ProjectID,'0') as
ProjectID,Messages.MessageID, Projects.ProjectName, Customers.CustomerName as
CustomerName, Regions.RegionName,
(select U.firstName + ' ' + U.LastName
from Users U inner join Projects P on P.ProjectManagerId = U.UserID inner join
Messages M on M.ProjectId = P.ProjectID
where M.MessageID = Messages.MessageID) as ProjectManagerName,
dbo.phases.TagName as Phase,
(select U.firstName + ' ' + U.LastName
from Users U inner join Messages M on M.CreatedBy = U.UserID Where M.MessageID =
Messages.MessageID) as CreatedBy,
Messages.DateCreated as EmailCreatedDate,
Messages.MessageSubject as MessageSubject,
Users.Email as [From],
**(stuff((select ','+U.EMAIL
FROM USERS U INNER JOIN Recipients R ON U.USERID = R.USERID INNER JOIN MESSAGES M
ON R.MESSAGEID = M.MESSAGEID
WHERE m.MessageID=Messages.MessageID AND R.RecipientTypeID=1
for xml path(''), type).value('.', 'nvarchar(max)'), 1, 1, '')) as [TO],**
**(stuff((select ','+U.EMAIL
FROM USERS U INNER JOIN Recipients R ON U.USERID = R.USERID INNER JOIN
MESSAGES M ON R.MESSAGEID = M.MESSAGEID
WHERE m.MessageID=Messages.MessageID AND R.RecipientTypeID=2
for xml path(''), type).value('.', 'nvarchar(max)'), 1, 1, '')) as CC**
FROM Messages INNER JOIN Users
ON Messages.Sender = Users.UserID
INNER JOIN Recipients
ON Messages.MessageID = Recipients.MessageID
LEFT OUTER JOIN dbo.Projects
ON dbo.Messages.ProjectID = dbo.Projects.ProjectID
Left OUTER JOIN dbo.Customers on dbo.Projects.CustomerId =
dbo.Customers.CustomerID
Left outer join dbo.regions on dbo.Customers.RegionID =
dbo.Regions.RegionID
left outer join dbo.Phases on messages.phaseid = dbo.phases.phaseid
where (Messages.MessageTypeID=1)
and Projects.ProjectID <> '0'
order by Projects.ProjectName
But on clicking next , it is causing an error like this:
Error in WHERE clause near '('.
Unable to parse query text.
However If I run the same above query in SQL Server 2008 R2 Management Studio, it runs fine and returns the results.
Please suggest what am I doing wrong.
Thanks
Hi, I have used one way out for this..ie used this Query(after slight modification) as inline query in .cs code behind file.. but now another problem is arising....ie.
Now it is taking too much of time to return results.. is it because of the functions I have used in the query ? please suggest some workaround for this..
SELECT DISTINCT
ISNULL(Projects.ProjectID,'0') as
ProjectID,Messages.MessageID, Projects.ProjectName, Customers.CustomerName as CustomerName, Regions.RegionName,
(select U.firstName + ' ' + U.LastName
from Users U inner join Projects P on P.ProjectManagerId = U.UserID inner join Messages M on M.ProjectId = P.ProjectID
where M.MessageID = Messages.MessageID) as ProjectManagerName,
dbo.phases.TagName as Phase,
(select U.firstName + ' ' + U.LastName
from Users U inner join Messages M on M.CreatedBy = U.UserID Where M.MessageID = Messages.MessageID) as CreatedBy,
Messages.DateCreated as EmailCreatedDate,
Messages.MessageSubject as MessageSubject,
Users.Email as [From],
(select [dbo].[fn_ForEmailReport](Messages.MessageID,1)) as [TO],
(select [dbo].[fn_ForEmailReport](Messages.MessageID,2)) as [CC]
FROM Messages INNER JOIN Users
ON Messages.Sender = Users.UserID
INNER JOIN Recipients
ON Messages.MessageID = Recipients.MessageID
LEFT OUTER JOIN dbo.Projects
ON dbo.Messages.ProjectID = dbo.Projects.ProjectID
Left OUTER JOIN dbo.Customers on dbo.Projects.CustomerId = dbo.Customers.CustomerID
Left outer join dbo.regions on dbo.Customers.RegionID = dbo.Regions.RegionID
left outer join dbo.Phases on messages.phaseid = dbo.phases.phaseid
where (Messages.MessageTypeID=1)
and Projects.ProjectID <> '0'
order by Projects.ProjectName
-- FUNCTION "fn_ForEmailReport" used in above query.
ALTER FUNCTION [dbo].[fn_ForEmailReport]
(
#MessageID int,
#RecipientTypeID int
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
DECLARE #List VARCHAR(8000)
set #List = (stuff((select ','+U.EMAIL
FROM USERS U inner JOIN Recipients R ON R.USERID = U.USERID inner JOIN
MESSAGES M ON R.MESSAGEID = M.MESSAGEID
WHERE m.MessageID=#MessageID AND R.RecipientTypeID=#RecipientTypeID
for xml path(''), type).value('.', 'nvarchar(max)'), 1, 1, ''))
RETURN #List;
END
There are too many joins I believe. For example, the query below
(select U.firstName + ' ' + U.LastName
from Users U inner join Projects P
on P.ProjectManagerId = U.UserID
inner join Messages M on M.ProjectId = P.ProjectID
where M.MessageID = Messages.MessageID) as ProjectManagerName,
gets only a firstname and lastname, a cell, it doesn't need to be a join query. Plus, you have already joined Projects table at the end. You can give an alias to outer query and make inner queries simpler:
SELECT DISTINCT
ISNULL(Projects.ProjectID,'0') as
ProjectID,Messages.MessageID, Projects.ProjectName, Customers.CustomerName as CustomerName, Regions.RegionName,
(select U.firstName + ' ' + U.LastName
from Users WHERE P.ProjectManagerId = UserID) as ProjectManagerName,
dbo.phases.TagName as Phase,
(select U.firstName + ' ' + U.LastName
from Users WHERE M.CreatedBy = UserID) as CreatedBy,
Messages.DateCreated as EmailCreatedDate,
Messages.MessageSubject as MessageSubject,
Users.Email as [From],
(select [dbo].[fn_ForEmailReport](Messages.MessageID,1)) as [TO],
(select [dbo].[fn_ForEmailReport](Messages.MessageID,2)) as [CC]
FROM Messages M INNER JOIN Users
ON Messages.Sender = Users.UserID
INNER JOIN Recipients
ON Messages.MessageID = Recipients.MessageID
LEFT OUTER JOIN dbo.Projects P
ON dbo.Messages.ProjectID = dbo.Projects.ProjectID
Left OUTER JOIN dbo.Customers on dbo.Projects.CustomerId = dbo.Customers.CustomerID
Left outer join dbo.regions on dbo.Customers.RegionID = dbo.Regions.RegionID
left outer join dbo.Phases on messages.phaseid = dbo.phases.phaseid
where (Messages.MessageTypeID=1)
and Projects.ProjectID <> '0'
order by Projects.ProjectName
Finally, if you don't retrieve any information from Customers table, you can omit the following line:
Left OUTER JOIN dbo.Customers on dbo.Projects.CustomerId = dbo.Customers.CustomerID
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
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"])+"