how to reproduce in query builder this
AND LENGTH( CONCAT_WS('', lsc.door_code, lsc.alarm_code, lsc.service_code, lsc.master_log) ) > 0"
I try like this
$orX = $qb->expr()->orX();
$orX
->add($qb->expr()->length('lsc.doorCode > 0'))
->add($qb->expr()->length('lsc.alarmCode > 0'))
->add($qb->expr()->length('lsc.serviceCode > 0'))
->add($qb->expr()->length('lsc.masterLog > 0'));
$qb->andWhere($orX);
have error:
[Syntax Error] line 0, col 549: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got '>'
and my query
SELECT
CONCAT(
IFNULL(location.name, ''),' (',IFNULL(location.streetAddress, ''),' ',IFNULL(location.staircase, ''),' ',IFNULL(location.flatNumber, ''),' ',IFNULL(location.postal, ''),' ',IFNULL(location.postOffice, ''),')') as address,
lsc.doorCode,
lsc.serviceCode,
lsc.alarmCode,
lsc.masterLog
FROM AppBundle:LocationServiceCompany lsc INNER JOIN lsc.location location WHERE lsc.serviceCompany = :sc AND (LENGTH(lsc.doorCode > 0) OR LENGTH(lsc.alarmCode > 0) OR LENGTH(lsc.serviceCode > 0) OR LENGTH(lsc.masterLog > 0))
how to use length ?
I came here because I had similar problem with using length in query builder.
I tried ($qb->expr()->length('lsc.doorCode > 0') as suggested above but it did not work. The SQL output of this is "LENGTH(lsc.doorCode > 0)" which throws an error.
My solution to this problem is:
$qb->expr()->gt($qb->expr()->length('lsc.doorCode'), 0)
which generates "LENGTH(lsc.doorCode) > 0" that is correct SQL syntax.
You have a typo here:
$orX = $qb->expr()->orX();
$orX
->add($qb->expr()->length('lsc.doorCode > 0')); <--
->add($qb->expr()->length('lsc.alarmCode > 0'))
->add($qb->expr()->length('lsc.serviceCode > 0'))
->add($qb->expr()->length('lsc.masterLog > 0'));
$qb->andWhere($orX);
remove the ; and get rid of the syntax error.
And for the concat length, you can try something like this:
$query->andWhere("LENGTH( CONCAT_WS('', lsc.door_code, lsc.alarm_code, lsc.service_code, lsc.master_log) ) > 0")
After the update
You can run native SQL queries with Doctrine like mentioned here:
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/native-sql.html#the-nativequery-class
For reference:
<?php
use Doctrine\ORM\Query\ResultSetMapping;
$rsm = new ResultSetMapping();
// build rsm here
$query = $entityManager->createNativeQuery('SELECT id, name, discr FROM users WHERE name = ?', $rsm);
$query->setParameter(1, 'romanb');
$users = $query->getResult();
If your updated query works, this would be a way to make it work.
Related
im trying to update one table (bigDta, fields smiles) using data from another table, but it produces an error
(bigData$smiles == '' | is.null(bigData$smiles) | is.na(bigData$smiles))
& bigData$compound_id %in% tmpCompounds$compound_id
,
`:=` (
smiles=dtChembl[dtChembl$chembl_id == compound_id ,]$canonical_smiles
, comment = paste(comment,'smiles added from chemblDB by chemblID;')
, filteringStep=12
)
]
the error i get is
Error in `[.data.table`(dtChembl, dtChembl$chembl_id == compound_id, ) :
i evaluates to a logical vector length 5832210 but there are 1088555 rows. Recycling of logical i is no longer allowed as it hides more bugs than is worth the rare convenience. Explicitly use rep(...,length=.N) if you really need to recycle.
In addition: Warning message:
In dtChembl$chembl_id == compound_id :
longer object length is not a multiple of shorter object length
i have solved the problem..in case that anyone need it, this is solution:
bigData[
dtChembl
, on=.(compound_id = chembl_id)
,
`:=` (
smiles= canonical_smiles
, comment = paste(comment,'smiles added from chemblDB by chemblID;')
, filteringStep=12
)
]
I am using Consumers Drupal 8 module. After installation, I have tried to configure it but getting below error :
> Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[42S02]: Base
> table or view not found: 1146 Table 'drupal88.consumer__roles' doesn't
> exist: SELECT t.* FROM {consumer__roles} t WHERE (entity_id IN
> (:db_condition_placeholder_0, :db_condition_placeholder_1)) AND
> (deleted = :db_condition_placeholder_2) AND (langcode IN
> (:db_condition_placeholder_3, :db_condition_placeholder_4,
> :db_condition_placeholder_5)) ORDER BY delta ASC; Array (
> [:db_condition_placeholder_0] => 1 [:db_condition_placeholder_1] => 2
> [:db_condition_placeholder_2] => 0 [:db_condition_placeholder_3] => en
> [:db_condition_placeholder_4] => und [:db_condition_placeholder_5] =>
> zxx ) in
> Drupal\Core\Entity\Sql\SqlContentEntityStorage->loadFromDedicatedTables()
> (line 1261 of
> core\lib\Drupal\Core\Entity\Sql\SqlContentEntityStorage.php).
Plugin URL: https://www.drupal.org/project/consumers
Any one help me for this?
Question
Using the mongolite package in R, how do you query a database for a given date?
Example Data
Consider a test collection with two entries
library(mongolite)
## create dummy data
df <- data.frame(id = c(1,2),
dte = as.POSIXct(c("2015-01-01","2015-01-02")))
> df
id dte
1 1 2015-01-01
2 2 2015-01-02
## insert into database
mong <- mongo(collection = "test", db = "test", url = "mongodb://localhost")
mong$insert(df)
Mongo shell query
To find the entries after a given date I would use
db.test.find({"dte" : {"$gt" : new ISODate("2015-01-01")}})
How can I reproduce this query in R using mongolite?
R attempts
So far I have tried
qry <- paste0('{"dte" : {"$gt" : new ISODate("2015-01-01")}}')
mong$find(qry)
Error: Invalid JSON object: {"dte" : {"$gt" : new ISODate("2015-01-01")}}
qry <- paste0('{"dte" : {"$gt" : "2015-01-01"}}')
mong$find(qry)
Imported 0 records. Simplifying into dataframe...
data frame with 0 columns and 0 rows
qry <- paste0('{"dte" : {"gt" : ', as.POSIXct("2015-01-01"), '}}')
mong$find(qry)
Error: Invalid JSON object: {"dte" : {"gt" : 2015-01-01}}
qry <- paste0('{"dte" : {"gt" : new ISODate("', as.POSIXct("2015-01-01"), '")}}')
mong$find(qry)
Error: Invalid JSON object: {"dte" : {"gt" : new ISODate("2015-01-01")}}
#user2754799 has the correct method, but I've made a couple of small changes so that it answers my question. If they want to edit their answer with this solution I'll accept it.
d <- as.integer(as.POSIXct(strptime("2015-01-01","%Y-%m-%d"))) * 1000
## or more concisely
## d <- as.integer(as.POSIXct("2015-01-01")) * 1000
data <- mong$find(paste0('{"dte":{"$gt": { "$date" : { "$numberLong" : "', d, '" } } } }'))
as this question keeps showing up at the top of my google results when i forget AGAIN how to query dates in mongolite and am too lazy to go find the documentation:
the above Mongodb shell query,
db.test.find({"dte" : {"$gt" : new ISODate("2015-01-01")}})
now translates to
mong$find('{"dte":{"$gt":{"$date":"2015-01-01T00:00:00Z"}}}')
optionally, you can add millis:
mong$find('{"dte":{"$gt":{"$date":"2015-01-01T00:00:00.000Z"}}}')
if you use the wrong datetime format, you get a helpful error message pointing you to the correct format: use ISO8601 format yyyy-mm-ddThh:mm plus timezone, either "Z" or like "+0500"
of course, this is also documented in the mongolite manual
try mattjmorris's answer from github
library(GetoptLong)
datemillis <- as.integer(as.POSIXct("2015-01-01")) * 1000
data <- data_collection$find(qq('{"createdAt":{"$gt": { "$date" : { "$numberLong" : "#{datemillis}" } } } }'))
reference: https://github.com/jeroenooms/mongolite/issues/5#issuecomment-160996514
Prior converting your date by multiplying it with 1000, do this: options(scipen=1000), as the lack of this workaround will affect certain dates.
This is explained here:
I am using Graticule to find people within a distance of x miles from an event. I need to send mail to everyone who is within x miles of the event.
Here is my code :-
user.rb
def self.weekly_update
#users = User.all
#users.each do |u|
#events = Event.all_with_distance([u.geo_lat, u.geo_lng]).where("start > ?", Time.zone.now).where("distance < 15")
UsersMailer.weekly_mail(u.email, #events).deliver
end
end
def self.all_with_distance(origin)
distance_sql = sql_for_distance(origin)
select("#{table_name}.*, #{distance_sql} AS distance").select("`locations`.`geo_lat`, `locations`.`geo_lng`, `locations`.`name` as location_name").joins(:location)
end
geo_search.rb
module GeoSearch
def sql_for_distance(origin)
Graticule::Distance::Spherical.to_sql(
:latitude => origin[0],
:longitude => origin[1],
:latitude_column => "`locations`.`geo_lat`",
:longitude_column => "`locations`.`geo_lng`",
:units => :kilometers
)
end
end
end
This is the error which I am getting :-
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands/runner.rb:49:in `eval': Mysql2::Error: Unknown column 'distance' in 'where clause': SELECT events.*, (ACOS( SIN(RADIANS(26.8465108)) * SIN(RADIANS(`locations`.`geo_lat`)) + COS(RADIANS(26.8465108)) * COS(RADIANS(`locations`.`geo_lat`)) * COS(RADIANS(`locations`.`geo_lng`) - RADIANS(80.9466832)) ) * 6378.135) AS distance, `locations`.`geo_lat`, `locations`.`geo_lng`, `locations`.`name` as location_name FROM `events` INNER JOIN `locations` ON `locations`.`id` = `events`.`location_id` WHERE (start > '2011-12-10 10:38:20') AND (distance < 15) (ActionView::Template::Error)
If I remove .where("distance < 15") to .order("distance") everything works fine.
In both standard SQL and the dialect supported by MySQL, column aliases can't be used in a WHERE clause; you must use the column. Here's what it would look like for your code, placing the call to where in all_with_distance:
select("#{table_name}.*, #{distance_sql} AS distance")
.select("`locations`.`geo_lat`, `locations`.`geo_lng`, `locations`.`name` as location_name")
.joins(:location)
.where("#{distance_sql} < 50")
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Slow Performance of Sql Query
Hi,
I have asked the performance of query and i tried to simplyfy it.but still it not works.I am adding my query below.Please can you simplify it more effectively
select
r.parent_itemid f_id,
parent_item.name f_name,
parent_item.typeid f_typeid,
parent_item.ownerid f_ownerid,
parent_item.created f_created,
parent_item.modifiedby f_modifiedby,
parent_item.modified f_modified,
pt.name f_tname,
child_item.id i_id,
t.name i_tname,
child_item.typeid i_typeid,
child_item.name i_name,
child_item.ownerid i_ownerid,
child_item.created i_created,
child_item.modifiedby i_modifiedby,
child_item.modified i_modified,
r.ordinal i_ordinal
from
item child_item,
type t,
relation r,
item parent_item,
type pt
where
r.child_itemid = child_item.id and
t.id=child_item.typeid and
parent_item.id = r.parent_itemid and
pt.id = parent_item.typeid
and parent_item.id in (
select
itemid
from
permission
where
itemid=parent_item.id and
(holder_itemid in (10,100) and level > 0) )
order by
r.parent_itemid,
r.relation_typeid,
r.ordinal
Thanks you
regards
jennie
You don't need the correlated subquery on the permissions. Any other problems need to be fixed by checking the indexes indexes on the join fields (like item.child_itemid ) & filter fields (like permission.holder_itemid) will help the performance of your query
select
r.parent_itemid f_id,
parent_item.name f_name,
parent_item.typeid f_typeid,
parent_item.ownerid f_ownerid,
parent_item.created f_created,
parent_item.modifiedby f_modifiedby,
parent_item.modified f_modified,
pt.name f_tname,
child_item.id i_id,
t.name i_tname,
child_item.typeid i_typeid,
child_item.name i_name,
child_item.ownerid i_ownerid,
child_item.created i_created,
child_item.modifiedby i_modifiedby,
child_item.modified i_modified,
r.ordinal i_ordinal
from
item child_item,
type t,
relation r,
item parent_item,
type pt,
permission p
where
r.child_itemid = child_item.id
and t.id=child_item.typeid
and parent_item.id = r.parent_itemid
and pt.id = parent_item.typeid
and parent_item.id = p.itemid
and p.holder_itemid in (10, 100)
and p.level > 0
order by
r.parent_itemid,
r.relation_typeid,
r.ordinal
Try removing the subquery, something like:
select
r.parent_itemid f_id,
parent_item.name f_name,
parent_item.typeid f_typeid,
parent_item.ownerid f_ownerid,
parent_item.created f_created,
parent_item.modifiedby f_modifiedby,
parent_item.modified f_modified,
pt.name f_tname,
child_item.id i_id,
t.name i_tname,
child_item.typeid i_typeid,
child_item.name i_name,
child_item.ownerid i_ownerid,
child_item.created i_created,
child_item.modifiedby i_modifiedby,
child_item.modified i_modified,
r.ordinal i_ordinal
from
item child_item,
type t,
relation r,
item parent_item,
type pt,
permission perm /* <<< added this line <<< */
where
r.child_itemid = child_item.id and
t.id=child_item.typeid and
parent_item.id = r.parent_itemid and
pt.id = parent_item.typeid
and parent_item.id = perm.itemid /* <<< modified this line <<< */
and perm.itemid = parent_item.id /* <<< copied these 2 lines from the subquery <<< */
and (perm.holder_itemid in (10,100) and perm.level > 0) ) /* <<< */
order by
r.parent_itemid,
r.relation_typeid,
r.ordinal
Give it a try, and see if it works and makes any difference.