I have a problem with a drupal db_select.
Here is my code :
$query = db_select('node', 'n');
$query->addField('n', 'nid', 'nid');
$query->addField('cfs', 'entity_id', 'feature_support_id');
$query->addField('fpffs', 'entity_id', 'parent_feature_support_id');
$query->addField('cfsfc', 'feature_support_compared_target_id', 'feature_support_compared');
$query->addField('fpffsfc', 'feature_support_compared_target_id', 'parent_feature_support_compared');
//Get feature_support of the feature
$query->join('field_data_feature_support_feature', 'cfs', 'n.nid = cfs.feature_support_feature_target_id');
$query->join('field_data_feature_support_compared', 'cfsfc', 'cfs.entity_id = cfsfc.entity_id');
//Get parent feature_support through feature
$query->join('field_data_feature_parent_feature', 'fp', 'n.nid = fp.entity_id');
$query->join('field_data_feature_support_feature', 'fpffs', 'fp.feature_parent_feature_target_id = fpffs.feature_support_feature_target_id');
$query->join('field_data_feature_support_compared', 'fpffsfc', 'fpffs.entity_id = fpffsfc.entity_id');
$query->condition('n.nid', $node_revision->nid, '=');
$query->condition('cfsfc.feature_support_compared_target_id', 'fpffsfc.feature_support_compared_target_id', '=');
$result = $query->execute();
In DB my request should be
SELECT n.nid AS nid, cfs.entity_id AS feature_support_id, fpffs.entity_id AS parent_feature_support_id, cfsfc.feature_support_compared_target_id AS feature_support_compared, fpffsfc.feature_support_compared_target_id AS parent_feature_support_compared
FROM node n
INNER JOIN field_data_feature_support_feature cfs ON n.nid = cfs.feature_support_feature_target_id
INNER JOIN field_data_feature_support_compared cfsfc ON cfs.entity_id = cfsfc.entity_id
INNER JOIN field_data_feature_parent_feature fp ON n.nid = fp.entity_id
INNER JOIN field_data_feature_support_feature fpffs ON fp.feature_parent_feature_target_id = fpffs.feature_support_feature_target_id
INNER JOIN field_data_feature_support_compared fpffsfc ON fpffs.entity_id = fpffsfc.entity_id
WHERE (n.nid = '9') AND (cfsfc.feature_support_compared_target_id = fpffsfc.feature_support_compared_target_id)
This request work when I try it in phpmyadmin, but instead in mysql log I have
SELECT n.nid AS nid, cfs.entity_id AS feature_support_id, fpffs.entity_id AS parent_feature_support_id, cfsfc.feature_support_compared_target_id AS feature_support_compared, fpffsfc.feature_support_compared_target_id AS parent_feature_support_compared
FROM node n
INNER JOIN field_data_feature_support_feature cfs ON n.nid = cfs.feature_support_feature_target_id
INNER JOIN field_data_feature_support_compared cfsfc ON cfs.entity_id = cfsfc.entity_id
INNER JOIN field_data_feature_parent_feature fp ON n.nid = fp.entity_id
INNER JOIN field_data_feature_support_feature fpffs ON fp.feature_parent_feature_target_id = fpffs.feature_support_feature_target_id
INNER JOIN field_data_feature_support_compared fpffsfc ON fpffs.entity_id = fpffsfc.entity_id
WHERE (n.nid = '9') AND (cfsfc.feature_support_compared_target_id = 'fpffsfc.feature_support_compared_target_id')
See at the end, in the WHERE, there is single quote around 'fpffsfc.feature_support_compared_target_id' which should not be there.
It's obviously because the second argument of ->condition seems only accept variable. Anyone know how I can make a condition with two db fields with db_select?
Thanks for any help you can bring me.
Use $query->where($snippet, $args = array());
$query->where('cfsfc.feature_support_compared_target_id = fpffsfc.feature_support_compared_target_id');
Related
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.
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.
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 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
I'm trying to query the model from my Symfony2 project and I am not able to replace wildcards into my DQL query. Look;
$q2 =
'SELECT
p.codigo,
p.descripcion,
SUM(l.cantidad) as cantidad,
SUM(l.cantidad*l.pvp) as euros
FROM
MGFAppBundle:LineaVenta l
JOIN
MGFAppBundle:Producto p
WITH
l.producto = p.id
JOIN
l.venta v
WITH
l.venta = v.id
WHERE
l.producto IN (:array)
AND
v.farmacia = :farmacia
GROUP BY
p.codigo';
$query2 = $this->em->createQuery($q2)
->setParameter('farmacia', $farmacia)->setParameter('array', $array);
$porFarmacia = $query2->getResult();
// This does not return a single value, when it should return 2 lines.
echo $query2->getSQL();
// Returns:
// SELECT p0_.codigo AS codigo0, p0_.descripcion AS descripcion1, SUM(l1_.cantidad) AS sclr2, SUM(l1_.cantidad * l1_.pvp) AS sclr3 FROM LineaVenta l1_ INNER JOIN Producto p0_ ON (l1_.lineaventas_id = p0_.id) INNER JOIN Venta v2_ ON l1_.venta_id = v2_.id AND (l1_.venta_id = v2_.id) WHERE l1_.lineaventas_id IN (?) AND v2_.farmacia_id = ? GROUP BY p0_.codigo
So, question marks where parameters should be. Any hint? Thanks in advance.