I want to detect id that are not equal to 1,2,3 or NULL . here is my query:
$qb = $this->_em->createQueryBuilder()
->select('u.cityId')
->from('.....\Entities\Cities', 'u')
->where("u.cityId is null OR u.cityId NOT IN (:parentType) ")
->setParameter('parentType' , "2,3,10");
$qb = $qb->getQuery();
return $qb->getResult();
Although it shows me id which are NULL or not equal to 2 and another values. It's not restrict 3,10. Any suggestion?
You need to pass an array. Try this:
->setParameter('parentType' , array(2,3,10) );
Instead of:
->setParameter('parentType' , "2,3,10");
Hope this help
Related
How to make IN condition always return true just like WHERE 1, I tried null but didn't work:
WHERE X IN (NULL)
Is there a way to alway make IN returns true and accept all rows?
return true from something like the following:
where X in ("Any value here to alway return true")
The best you could do is to include the column being compared:
where x in (x)
However, this does not include NULL values. In fact, there is no way you can make this return true:
where NULL in ( . . . )
You could revise this to:
where coalesce(x, '') in (coalesce(x, '')
One way is to use LEFT JOIN :
select t.*
from table t left join
( . . . ) tt
on tt.x = t.x;
For example I have a variable $num = 123; and another one called `$name=joe;' , and there is a Database that contains a table called "data" and inside this table there are two columns (num [type=varchar(255)] - name[type=varchar(255)]) .
For example these query exists in the DB :
num = 123456 , name = joe
How to make a check that the first "3" numbers equals the $num variable and the name equals variable $name ?
I tried this but it didn't work:
SELECT * FROM data Where SUBSTRING(num , 0 , 3) = '123' AND name = 'joe'
In MySQL, substring indexing starts at 1:
WHERE SUBSTRING(num , 1 , 3) = '123' AND name = 'joe'
But LEFT() or LIKE would more commonly be used:
WHERE LEFT(num , 3) = '123' AND name = 'joe'
WHERE num = '123%' AND name = 'joe'
The advantage of LIKE is that it can make use of an index . . . even one on (name, num).
The MySQL substring() function is 1-based, not 0-based, so this should work for you:
SELECT * FROM data Where SUBSTRING(num , 1 , 3) = '123' AND name = 'joe'
i've create command and it works fine but when I add IFNull exception it add a comma and i searched a lot there is no answer my code :
public function actionTotal($id)
{
$query1 = new Query;
$query1 ->select(' sum(patient_services.price) price ,
sum( IFNULL(receipts.price,0)) receipts')
->from('patient_services')
->leftJoin('receipts', 'patient_services.patient_id = receipts.patient_id')
->where('patient_services.patient_id=:id', array(':id'=>$id));
$command1 = $query1->createCommand();
$price = $command1->queryAll();
echo Json::encode($price);
}
when i try it ... the select code have a comma and idon't know how to remove it
SELECT sum(patient_services.price) price, sum( IFNULL(receipts.price, `0))` AS `receipts` FROM `patient_services` LEFT JOIN `receipts` ON patient_services.patient_id = receipts.patient_id WHERE patient_services.patient_id=2
In you code
$query1 ->select('sum(patient_services.price) price
,sum( IFNULL(receipts.price,)) receipts')
^^ here is missing the value for ifnull
eg: ifnull(your_column, 0);
->from('patient_services')
->leftJoin('receipts', 'patient_services.patient_id = receipts.patient_id')
->where('patient_services.patient_id=:id', array(':id'=>$id));
then try
$query1 ->select(' sum(patient_services.price) price ,
sum( IFNULL(receipts.price,0)) receipts')
->from('patient_services')
->leftJoin('receipts', 'patient_services.patient_id = receipts.patient_id')
->where('patient_services.patient_id=:id', array(':id'=>$id));
looking at the strange result in your img
try using this this notation and (remove also the two space between query1 and ->
$query1->select(["sum(patient_services.price) AS price",
"sum( IFNULL(receipts.price,0)) AS receipts"] )
And eventually try to clear runtime directory ..and flush the db cache
Add 0 as a second parameter for IFNULL function, so that if the value is null it will print 0, here is the example.
$query1->select(['sum(patient_services.price) price,
sum( IFNULL(receipts.price,0)) receipts'])
I need help with my SELECT.
I got a field that can be NULL and in it there is stored a foreign-key.
SELECT * FROM beerImage WHERE beerBRewID = brewID AND beerBrandID = beerID <--- this can be NULL
So if it's NULL nothing happens.
How can I check if beerID is NOT NULL so I can use "beerBrandID = beerID"?
You probably need something like this:
First example:
SELECT * FROM beerImage WHERE beerBRewID = brewID AND (beerID IS NULL OR beerBrandID = beerID)
Second Example:
SELECT * FROM beerImage WHERE beerBRewID = brewID AND beerID IS NOT NULL AND beerBrandID = beerID
The first example will allow you to show records which have beerID null along with the beers which beerBrandID is equal to beerID (both).
The second one will return exactly beers which correspond to beerBrandID (excluding NULL beerIDs).
How about using with CASE statement in where clause like
WHERE
CASE WHEN beer.id IS NOT NULL
THEN beer.brand_id = 12345
ELSE TRUE
END
If you want to include records where there's no match, you need an outer join
SELECT beer.id AS beerID,
beer.barrelID AS barrelID,
beer.imageID AS imageID,
beer.title AS title,
beer.map AS map,
beer.longitude AS longitude,
beer.latitude AS latitude,
brand.name AS brandName,
brew.type AS brewType,
image.name AS imageName,
variation.name AS variationName
FROM brand, brew, image, beer
LEFT OUTER JOIN variation ON variation.ID = beer.VariationID
WHERE beer.id = %s
AND md5(beer.memberID) = %s
AND beer.review = 1
AND brand.ID = beer.brandID
AND brew.ID = beer.brewID
AND image.ID = beer.imageID
To check for null / not null, use IS NULL / IS NOT NULL
AND beerID IS NOT NULL
You can use "IS NULL" or "IS NOT NULL" MySQL comparison functions.
Read more about this here:
http://dev.mysql.com/doc/refman/5.0/en/working-with-null.html
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_is-null
I want to do a simple SQL query in Drupal, but I'm not sure how. I would like to achieve this:
SELECT COUNT(nid) AS i_count FROM node WHERE `created` != `changed`;
I have the following code, which doesn't work:
$query = db_select('node', 'n');
$query->addExpression("COUNT(nid)","i_count");
$query->condition("created","changed","!=");
$i_number_published = $query->execute()->fetchCol();
The reason why it doesn't work is that it compares the column created with the string value "changed". Is there any way I can tell it to compare columns instead of column-string?
Use the where() member function to add conditions based on other fields in the table:
$query = db_select('node', 'n');
$query->addExpression("COUNT(nid)","i_count");
$query->where("created != changed");
$i_number_published = $query->execute()->fetchCol();
$result_handler = db_query("select count(nid) from {node} where `created` != `changed`") ;
$result_arr = db_fetch_array($result_handler) ;
$number_of_nodes = $result_arr['count(nid)'] ;