Zend-Db 2.9 - SQL with RAND order - zend-db

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();

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);

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.

Count function in sql query issue

Please, i have this query here:
$query_pag_num = "SELECT COUNT(*) AS count FROM forma";
$result_pag_num = odbc_exec($connection, $query_pag_num) or die(odbc_error());
$row = odbc_fetch_array($result_pag_num);
$count = $row['id'];
The issue is i get this error here:
Undefined index: id where it's: `$count = $row['id'];`
Help please! It won't work without this piece of code here.
I'm using odbc_connect..
Thanks in advance..
your select function will only return 1 column: "count". you are trying to get to column "id"
here is your fixed code
$query_pag_num = "SELECT COUNT(*) AS count FROM forma";
$result_pag_num = odbc_exec($connection, $query_pag_num) or die(odbc_error());
$row = odbc_fetch_array($result_pag_num);
$count = $row['count'];
you do not have any field in your SELECT called 'id'. Try: $count = $row['count'];

How to get particular column in zend using Left join

I am new to zend framework,
Following is the plain mysql query which takes particular column from table,
SELECT jobs_users.id,jobs_users.first_name from jobs_users left join friends on jobs_users.id=friends.friend_id where friends.member_id=29
I tried with zend to implement the above query like below,
public function getFriendsProfileList($id){
$db = Zend_Db_Table::getDefaultAdapter();
$select = $db->select();
$select->from('jobs_users')
->joinLeft(
'friends',
'jobs_users.id=friends.friend_id',
array('jobs_users.id','jobs_users.first_name','jobs_users.last_name','jobs_users.photo')
)
->where("friends.member_id = ?", $id);
$result = $db->fetchAll($select);
return $result;
}
Here i got result with all column name , not with exact column name which i have given in query.
Kindly help me on this.
Use this instead:
$select->from('jobs_users', array('jobs_users.id','jobs_users.first_name','jobs_users.last_name','jobs_users.photo'))
->joinLeft('friends', 'jobs_users.id=friends.friend_id')
->where("friends.member_id = ?", '20');
You may also try this:
$select = $db->select();
$select->setIntegrityCheck(false);
$select->joinLeft('jobs_users','',array('jobs_users.id','jobs_users.first_name','jobs_users.last_name','jobs_users.photo'));
$select->joinLeft('friends','jobs_users.id=friends.friend_id', array());
$select->where("friends.member_id = ?", $id);
$result = $db->fetchAll($select);
return $result;

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

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 ...');