Here is the code that doesn't work.
$select = $tariffsTable->select(Zend_Db_Table_Abstract::SELECT_WITHOUT_FROM_PART)
->from('tariff', null)
->where('id = ?', $this->id)
->joinLeft('characteristic_value',
'characteristic_value.tariff_id = id',
array('value_' . $locale, 'characteristic_id'))
->joinLeft('characteristic',
'id = characteristic_value.characteristic_id',
array('name_' . $locale, 'alias'));
$select->setIntegrityCheck(false);
$tariffCharacteristics = $tariffsTable->fetchAll($select)->toArray();
Thank you for help! I have solved the problem. Here is working code:
$select = $tariffsTable->select(Zend_Db_Table_Abstract::SELECT_WITHOUT_FROM_PART)
->from('tariff', null)
->joinLeft( array('characteristic_value'),
'characteristic_value.tariff_id = tariff.id',
array('value_' . $locale))
->joinLeft(array('characteristic'),
'characteristic.id = characteristic_value.characteristic_id',
array('name_' . $locale, 'alias'))
->where('tariff.id = ?', $this->id);
$select->setIntegrityCheck(false);
It is because your are using id column on WHERE and/or LEFT JOIN ON clause while an id column should be present in both table your are trying to join.
You need to specify which id should be used in both case.
Like characteristic.id or characteristic_value.id or tarriff.id.
You can use alias by using array as a parameter for from() and joinLeft() methods.
$select->from(array('t' => 'tarriff'))
->where('t.id = ?', $this->id);
Related
I need help for query in laravel
My Custom Query: (Return Correct Result)
Select * FROM events WHERE status = 0 AND (type="public" or type = "private")
how to write this query in Laravel.
Event::where('status' , 0)->where("type" , "private")->orWhere('type' , "public")->get();
But it's returning all public events that status is not 0 as well.
I am using Laravel 5.4
Pass closure into the where():
Event::where('status' , 0)
->where(function($q) {
$q->where('type', 'private')
->orWhere('type', 'public');
})
->get();
https://laravel.com/docs/5.4/queries#parameter-grouping
In your case you can just rewrite the query...
select * FROM `events` WHERE `status` = 0 AND `type` IN ("public", "private");
And with Eloquent:
$events = Event::where('status', 0)
->whereIn('type', ['public', 'private'])
->get();
When you want to have a grouped OR/AND, use a closure:
$events = Event::where('status', 0)
->where(function($query) {
$query->where('type', 'public')
->orWhere('type', 'private');
})->get();
Use this
$event = Event::where('status' , 0);
$event = $event->where("type" , "private")->orWhere('type' , "public")->get();
or this
Event::where('status' , 0)
->where(function($result) {
$result->where("type" , "private")
->orWhere('type' , "public");
})
->get();
Try this. It works for me.
$rand_word=Session::get('rand_word');
$questions =DB::table('questions')
->where('word_id',$rand_word)
->where('lesson_id',$lesson_id)
->whereIn('type', ['sel_voice', 'listening'])
->get();
$filter = [];
$filter[] = "type="public" or type = "private"";
$filter[] = "status = 0";
$event = Event::whereRaw(implode(' AND ',$filter))->get();
You can store all condition in filter array and then apply to query
How can I write query like this In Zend Framework and fetch all the rows
SELECT * FROM tbl
WHERE user_id = $part_mail
OR user_id ='$id3';
I used to try this :
$select = $tigaseModel->fetchAll($tigaseModel
->select()
->where('user_id = ?', $part_mail )
-> orwhere('user_id = ?', $id3 ));
Following ways should help you.
Solution 1:
where( "user_id = '$part_mail' OR user_id = '$id3'" );
Solution 2:
$list = array( $part_mail, $id3 );
...
where( 'user_id in ( ? )', $list );
Solution 3:
$list = array( $part_mail, $id3 );
...
where( array( 'user_id' => $list ) );
Refer to Documentation:
Example #17 Example of an array parameter in the where() method
-You can pass an array as the second parameter to the where() method when using the SQL IN operator.
orwhere available in Zend Framework 1. But there is a space in your code before orwhere(). Check the code below:
$select = $tigaseModel->fetchAll($tigaseModel
->select()
->where('user_id = ?', $part_mail )
->orwhere('user_id = ?', $id3 ));
How would i convert below query to sub query?
I don't want to use JOINS I want to do same through sub query. i-e I need three subqueries out of the three joins. How it would be possible for below query ?
protected $_name = 'sale_package_features';
public function getAllSalePackageFeatures(){
$sql = $this->select()->setIntegrityCheck(false)
->from(array('spf' => $this->_name))
->joinLeft(array('sd' => 'sale_devices'),'sd.sale_device_id = spf.sale_device_id',array('sd.sale_device_name AS deviceName'))
->joinLeft(array('sp' => 'sale_packages'),'sp.sale_package_id = spf.sale_package_id',array('sp.sale_package_name AS packageName'))
->joinLeft(array('sf' => 'sale_features'),'sf.sale_feature_id = spf.sale_feature_id',array('sf.sale_feature_name AS featureName'))
->where('sf.parent_id != ?',0)
->order('spf.sale_package_feature_id ASC');
return $sql->query()->fetchAll();
}
Edited :
SELECT
`spf`.*, `sd`.`sale_device_name` AS `deviceName`,
`sp`.`sale_package_name` AS `packageName`,
`sf`.`sale_feature_name` AS `featureName`
FROM `sale_package_features` AS `spf`
LEFT JOIN `sale_devices` AS `sd`
ON sd.sale_device_id = spf.sale_device_id
LEFT JOIN `sale_packages` AS `sp`
ON sp.sale_package_id = spf.sale_package_id
LEFT JOIN `sale_features` AS `sf`
ON sf.sale_feature_id = spf.sale_feature_id
WHERE (sf.parent_id != 0)
ORDER BY `spf`.`sale_package_feature_id` ASC
You can use
$dbAdapter = Zend_Db_Table::getDefaultAdapter();
$subSelect = $dbAdapter
->select()
->from( 'tablename',
array(
'col1_alias' => 'column1_name',
'col2_alias' => 'column2_name',
))
->where('somefield = ?', $value_to_be_quoted);
$select = $dbAdapter
->select()
->from(array('sub_alias' => $subSelect ))
->where('otherfield = ?', $other_value_to_be_quoted);
$sql = $select->assemble();
$sql will contain
SELECT
sub_alias . *
FROM
(SELECT
tablename.column1_name AS col1_alias,
tablename.column2_name AS col2_alias
FROM
tablename
WHERE
(somefield = 'quoted_value')) AS sub_alias
WHERE
(otherfield = 'quoted_other_value')
If you need to use SELECT .. WHERE x IN (subselect) than go with
$select->where( 'column IN (?)', new Zend_Db_Expr($subselect->assemble()) );
Hi I have my query in codeigniter since I I have switch to Zend Framework, I wanted do my query in zend framework:
Here is my sample query:
$sql = "SELECT Mes.fromid, Mes.is_read AS is_read, Mes.id AS mesid, Mes.message AS message,
max(Mes.date) AS date, User.username AS username, User.id AS Uid
FROM `messages` AS Mes
LEFT JOIN users AS User ON User.id = Mes.fromid
WHERE Mes.toid = ? AND Mes.fromid <> ?
GROUP BY Mes.fromid ORDER BY date DESC";
$query = $this->db->query($sql, array($fromid, $fromid));
return $query->result();
Thank you for you help.
$sql = $db ->select()
->from('messages as mes',array('fromid','is_read','id as mesid','message','max(date) as date'))
->join('users as user','user.id = mes.fromid',array('username','id as uid'))
->where('mes.toid = ? ', $toid)
->where('mes.fromid = ?', $fromid)
->group('mes.fromid')
->order('date DESC');
$select = $this->select()
->setIntegrityCheck(false)
->from(
array('Mes' => 'messages'),
array('fromid', 'is_read AS is_read', 'id AS mesid', 'message AS message',
'max(Mes.date) AS date')
)
->joinLeft(array('User' => 'users'), "User.id = Mes.fromid", array('username AS username', 'id AS Uid'))
->where('mes.toid = ? ', $toid)
->where('mes.fromid = ?', $fromid)
->group('mes.fromid')
->order('date DESC');
Hope This helps :)
Check out Zend_Db_Statement, it looks like you'll have to make very few edits to make this work.
Probably just change out the Db adapter to a Zend adapter and it'll probably work.
I have 4 tables on my DB;
merchants, pay_method, spendings ans transaction_type.
They all have an id and a title, except spend (spend have the Foreign keys of the others and other field like user_id, comment, ... ).
I try to have all the informations of all the tables for an unique user_id!
for perform it, I tried a little test that doesn't run :
public function getSpendingsForUser( $userid )
{
$mercTable = Engine_Api::_()->getDbTable('merchants', 'spendings');
$mercName = $mercTable->info('name');
$table = Engine_Api::_()->getDbTable('spendings', 'spendings');
$spendName = $table->info('name');
$select = $table->fetchAll(
$table->select()
->joinLeft($mercName, "$mercName.merchant_id = $spendName.merchant_id", NULL)
->where('user_id = ?', $userid)
->group('spending_id')
);
return $select;
}
An in the view file :
foreach($this->spendings as $s) {
var_dump($s->comment);
}
How can I make the join correctly to have all the info from the user_id ???
Thanks a lot !!!
You should define doesn't run to help us help you. But I can tell you you probably need to ->setIntegrityCheck(false);
$select = $table->fetchAll(
$table->select()
->setIntegrityCheck(false);
->joinLeft($mercName, "$mercName.merchant_id = $spendName.merchant_id", NULL)
->where('user_id = ?', $userid)
->group('spending_id')
);
Before the join.