i've create a Command in my controller like this :
public function actionTotal($id)
{
$query1 = new Query;
$query1 ->select('sum(patient_services.price) price, sum(receipts.price) receipts ,')
->from('patient_services ')
->leftJoin(' receipts ON 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(receipts.price) receipts FROM `patient_services` LEFT JOIN ` receipts ON` `patient_services`.`patient_id=receipts`.`patient_id` WHERE patient_services.patient_id=1
when i remove all commas from the sql code and try it in phpmyadmin .. it works fine :(
You have an invalid leftJoin replace it with this:
->leftJoin('receipts', 'patient_services.patient_id = receipts.patient_id')
also it seems you have an extra comma at the end of your select query remove that last comma the select query would look like this:
$query1 ->select('sum(patient_services.price) price, sum(receipts.price) receipts')
Hope this works.
Related
$sql = "SELECT COUNT(ID) AS total
FROM ".$datatable."
WHERE STATE=".$category ;
Im trying to figure out why this isnt working.
$sql = "SELECT COUNT(ID) AS total
FROM ".$datatable."
WHERE STATE='AL'" ;
this one hwoever works. Im unsure of what concactinate or " ' syntax issue it is.
You need to include single quotation marks for the where statement. Also, since you're using double quotes you don't actually need to escape from the string to include the PHP variables.
$sql = "SELECT COUNT(ID) AS total FROM $datatable WHERE STATE='$category'";
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 have a simple Person tree with parent_id.
I wont to build a (Yii2) query to find all children of a given Person, that are parent of someone else (a.k.a not leaves).
The output SQL should looks like this:
select * from person t
where exists (select 1 from person p2 where t.id = p2.parent_id);
But cant find the right way to build this with the query builder, there is a method ->exists(), but not much documentation/examples about it.
Not sure if i understood correctly, but do you look something like this.
$subQuery = (new \yii\db\Query)
->select([new \yii\db\Expression('1')])
->from('person p2')
->where('t.id = p2.parent_id');
$query = (new \yii\db\Query())
->select('*')
->from('person t')
->where(['exists', $subQuery]);
$command = $query->createCommand();
print_r ($command->sql);
Generates sql like:
SELECT * FROM `person` `t` WHERE EXISTS (SELECT 1 FROM `person` `p2` WHERE t.id = p2.parent_id)
You should try something like :
$tableName = Person::tableName();
$subQuery = (new Query())->select('*')->from($tableName . ' t2')->where('t1.id=t2.parent_id');
$persons = Person::find()->from($tableName . ' t1')->where(['exists', $subQuery])->all();
http://www.yiiframework.com/doc-2.0/guide-db-query-builder.html#where
I don't know the right way with only queries, but if you use PHP also, then I think this will help you.
And also try to search in Google with keywords: hierarchical menu PHP
I'm having trouble with making this query to work, I have 2 tables one with client info and the other one with product info. Trying to join them while querying the db.
mysql_query(
"SELECT client.id, client.email, client.prodid, prod.id, prod.name
FROM client, prod
WHERE client.id ="'.mysql_real_escape_string($_GET["id"]).'"
AND client.prodid =prod.id"
);
But that query doesn't return anything. What am I doing wrong? Thanks in advance.
Your quotes are wrong.
You use " to start the string in your method. In the middle you use "'.mysql_real_escape_string($_GET["id"]).'". Notice that you use "' instead of '".
This should work better (from a PHP point of view, I did not check your SQL syntax):
mysql_query(
"SELECT client.id, client.email, client.prodid, prod.id, prod.name
FROM client, prod
WHERE client.id ='".mysql_real_escape_string($_GET["id"])."'
AND client.prodid =prod.id"
);
Your quotes appear to be cancelling out the dynamic ID variable. If your client.id field is numeric, then you should remove the single quotes surrounding mysql_real_escape_string():
mysql_query(
"SELECT client.id, client.email, client.prodid, prod.id, prod.name
FROM client, prod
WHERE client.id = ".mysql_real_escape_string($_GET["id"])." AND client.prodid = prod.id"
);
If it works in PHPMyAdmin then the query's fine. Enable debugging and use var_dump():
ini_set('display_errors','on');
error_reporting(E_ALL);
$result = mysql_query($sql_query);
var_dump($result);
I have a string of IDs separated with comma
$myIDs = 22,23,45,895;
How do I write a query to return records for values that correspond to the IDs in my string?
This does not seem to be right:
SELECT *
FROM t1
WHERE itemID IN ($myIDs)
I guess I'm trying PHP array function here, hah? Is there something like this in mySQL?
Appreciate any suggestions. Thanks.
I think you're missing quotes, ie, the exact query should look like this before evaluation
SELECT *
FROM t1
WHERE itemID IN ('22','23','45','895');
Hence all you've got to do to fix this is:-
$myIDs = array(22,23,45,895);
$myIDs_string = "'".implode("','",$myIDs)."'";
then in whatever PHP/SQL library/framework you select, use PHP to execute the following php query:-
SELECT *
FROM t1
WHERE itemID IN ($myIDs_string);
Hope this helps.
$IDs = array(1,2,3,4,5);
// alternatively, you can write it like this...
// $IDs = "1,2,3,4,5";
if(is_array($IDs))
$IDs = implode(",",$IDs);
$query = "SELECT * FROM t1 WHERE itemID IN ($IDs)";
echo $query;