Trouble creating MySQL query in Symfony containing JOIN and RAND() - sql

How do I do this:
SELECT t.id
FROM table t
JOIN (SELECT(FLOOR(max(id) * rand())) AS maxid FROM table)
AS tt
ON t.id >= tt.maxid
LIMIT 1
in Symfony? (I know how to do basic stuff, but this is too much.

$connection = Doctrine_Manager::getConnection()->getDbh();
won't work... Try this:
$connection = Doctrine_Manager::getInstance()->getCurrentConnection()->getDbh();
Then:
$stmt = $connection->query('SELECT * FROM some_table');
$stmt->execute();
$result = $stmt->fetchAll();

$connection = Doctrine_Manager::getConnection()->getDbh();
$result = $connection->query('SELECT ...');

Related

How to implement oracle query to select from different tables in laravel

I'm trying to select distinct from different tables in laravel. In oracle I have implemented successfully the query and works. Now I'm trying to "translate" it into laravel. How can I do that?
In oraclesql the query is the following. TEMP table exists as well as all other tables used in this query. Is it possible to do this with DB::raw? Could you give me your advice please?
INSERT INTO TEMP (OBJECT_TYPE, OBLECT_ID)
SELECT DISTINCT HRP1001_CG.OBJECT_TYPE, HRP1001_CG.OBJECT_ID FROM HRP1001_SC, HRP1001_CG, CONFIG
WHERE
(HRP1001_SC.OBJECT_TYPE = 'CG')
AND
(HRP1001_SC.REL_OBJ_TYPE = 'SC')
AND
(HRP1001_SC.REL_OBJ_ID = CONFIG.SC)
AND
((HRP1001_SC.ST_DATE < CONFIG.DES_DATE) AND (HRP1001_SC.END_DATE > CONFIG.DES_DATE))
AND
(HRP1001_CG.REL_OBJ_ID = HRP1001_SC.OBJECT_ID)
AND
((HRP1001_CG.OBJECT_TYPE ='CG') OR (HRP1001_CG.OBJECT_TYPE ='SM'))
ORDER BY HRP1001_CG.OBJECT_ID;
update:
I also tried this code, but no result was received too :(.
$data = DB::table('hrp1001_sc')
->join('config', 'config.sc', '=', 'hrp1001_sc.rel_obj_id')
->join('config', 'config.des_date', '>', 'hrp1001_sc.st_date')
->join('config', 'config.des_date', '<', 'hrp1001_sc.end_date')
->join('hrp1001_cg', 'hrp1001_cg.rel_obj_id', '=',
'hrp1001_sc.object_id')
->where('hrp1001_sc.object_type', '=', 'cg')
->where('hrp1001_sc.rel_obj_type', '=', 'sc')
->select('hrp1001_sc.object_id')
->distinct()
->get();
Solution was found and this is it:
$q = "insert into temp(object_type, object_id)";
$q = $q."select distinct hrp1001_cg.object_type, hrp1001_cg.object_id from
hrp1001_cg, hrp1001_sc, config where";
$q =$q." hrp1001_sc.object_type = 'CG'";
$q = $q." and hrp1001_sc.rel_obj_type = 'SC'";
$q = $q." and hrp1001_sc.rel_obj_id = config.sc";
$q = $q." and hrp1001_sc.st_date < config.des_date";
$q = $q." and hrp1001_sc.end_date > config.des_date";
$q = $q." and hrp1001_cg.rel_obj_id = hrp1001_sc.object_id";
$q = $q." and";
$q = $q." ((hrp1001_cg.object_type = 'CG') or (hrp1001_cg.object_type = 'SM'))";
$xx = DB::insert($q);

Zend-Db 2.9 - SQL with RAND order

guys, I need help, I am using zend-db 2.9 - and I have an sql that need the rand() order, but the result of this query came wrong
my code:
$sql = new Sql($this->dbAdapter);
$select = $sql->select(
['a'=>$this->table]
);
$select->order("RAND() ASC");
$select->limit(1);
$stt = $sql->prepareStatementForSqlObject($select);
$res = $stt->execute();
result is :
SELECT `a`.* FROM `mytable` AS `a` ORDER BY `RAND``(``)` ASC LIMIT 1
how to fix it?
I found the answer fot this: Just Add new Expression("RAND()")
$sql = new Sql($this->dbAdapter);
$select = $sql->select(
['a'=>$this->table]
);
$select->order([new Expression("RAND() ASC")]);
$select->limit(1);
$stt = $sql->prepareStatementForSqlObject($select);
$res = $stt->execute();

Yii2 subquery in Active Record

How can I convert this sql into active record query
SELECT * FROM `base_twitter` WHERE id NOT IN (SELECT base_id from base_followers)
Assuming that your models are named BaseTwitter and BaseFollower accordingly, this should work:
$subQuery = BaseFollower::find()->select('id');
$query = BaseTwitter::find()->where(['not in', 'id', $subQuery]);
$models = $query->all();
// SELECT * FROM (SELECT * FROM `user` WHERE `active` = 1) `activeusers`;
$subquery = (new \yii\db\Query)->from('user')->where(['active' => true])
$query = (new \yii\db\Query)->from(['activeusers' => $subquery]);
// subquery can also be a string with plain SQL wrapped in parenthesis
// SELECT * FROM (SELECT * FROM `user` WHERE `active` = 1) `activeusers`;
$subquery = "(SELECT * FROM `user` WHERE `active` = 1)";
$query = (new \yii\db\Query)->from(['activeusers' => $subquery]);
Try This Query
$models = BaseTwitter::find()->where('id NOT IN (SELECT base_id from base_followers)')->all();

Yii db command and CDbCriteria

I have two tables Products(id, name) and Views(id,count,time), and those two tables are not related to each other. This is my code:
$dbCommand = Yii::app()->db->createCommand("
SELECT P.`id`, P.`name`, V.`time`
FROM `products` P, `views` V
WHERE P.`type` = 2
ORDER BY V.`time` DESC
");
$data = $dbCommand->queryAll();
It is working, but I want to convert this query to CDbCriteria syntax.
$cdb = new CDbCriteria();
$cdb->select = //???
$cdb->where = //???
$cdb->order = //???
How can I do this? Can somebody help me?
You cannot use CDbCriteria, Try using query builder.
Yii::app()->db->createCommand()
->select('P.id, P.name, V.time')
->from('products P, views V')
->where('P.type = :type')
->order('V.time DESC')
->queryAll(array(
':type' => 2
));

how can i nest two AND queries in Doctrine2 using createQueryBuilder?

following is my query :
$em = \Zend_Registry::get('em');
$qb_1 = $em->createQueryBuilder();
$q_1 = $qb_1->select('link_req')
->from('\Entities\link_requests','link_req')
->where( 'link_req.is_confirmed = 1' )
->andWhere('link_req.$link_requestsSenderUser='.$user1_id .'or'.' link_req.$link_requestsSenderUser='.$user2_id)
->andWhere('link_req.$link_requestsReceiverUser='.$user1_id .'or'.' link_req.$link_requestsReceiverUser='.$user2_id);
$result= $q_1->getDql();
echo $result;
and i want following query:
SELECT * FROM [fb_local].[dbo].[link_requests]
WHERE is_confirmed = 1 AND (request_user_id = 12 or request_user_id=19) AND (accept_user_id = 12 or accept_user_id = 19)
Your SQL is just 1 query, so not sure what you're trying to nest. But try the following:
$em = \Zend_Registry::get('em');
$qb_1 = $em->createQueryBuilder();
$q_1 = $qb_1->select('link_req')
->from('\Entities\link_requests','link_req')
->where( 'link_req.is_confirmed = 1' )
->andWhere('link_req.link_requestsSenderUser=:user_id or link_req.link_requestsSenderUser=:user_id')
->andWhere('link_req.link_requestsReceiverUser=:user_id or link_req.link_requestsReceiverUser=:user_id');
$q_1->setParameter('user_id', $user1_id);
$query = $q_1->getQuery();
echo $query->getSql();
Notice how I removed the $ in front of link_requestsReceiverUser. I also made a parameter from $user1_id, this prevents SQL injections.