Laravel query builder whereRaw with convert to datatype returning different results - sql

Here I have two similar queries that return the same result if I paste them directly on SMSS:
But when using Laravel's query builder I get different results:
//select count(*) from [tbl_rfaccount] where CONVERT(varchar, id) = 'test'
$count = DB::connection('sqlsrv_rf_user')->table('tbl_rfaccount')
->whereRaw("CONVERT(varchar, id) = ?", $request->input('username'))
->count();
//$count = 1
//select count(*) from [tbl_rfaccount] where id = CONVERT(binary(13), 'test')
$count = DB::connection('sqlsrv_rf_user')->table('tbl_rfaccount')
->whereRaw("id = CONVERT(binary(13), ?)", $request->input('username'))
->count();
//$count = 0
Why is that? I've using Laravel Telescope and the queries look exactly like those I ran on SSMS.
This is how the table looks like:

Related

PostgreSQL stored function with multiple queries

I'm working on a PostgreSQL function, but having a real struggle with it. It's not my main area so that's probably why, but I wanted to see if this is doable.
I'm trying to create a function to generate records based on year.
In php I'd do something along the lines of:
function recordsByYear($year=''){
$years = array();
if(empty($year)){
$sql = "SELECT year FROM myTable GROUP BY year ORDER BY year;";
$res = $conn->query($sql);
if($res !== false){
$data = $res->fetchAll(PDO::FETCH_COLUMN);
foreach($data as $oneYear){
$years[] = $oneYear;
}
}
}
else{
$years[] = $year;
}
foreach($years as $thisYear){
//do some queries based on $thisYear
}
}
But I'd like to create a function inside of PostgreSQL, and struggling because I'm not familiar enough with how it all works. I'd like to return a table. I can do some basic stuff, but haven't been able to get something like this working where I have one query then loop through those results running an additional query for each year, then combine the results of that second query and spit out the results as a table.
Is hard to guess without more information but you probably need a JOIN
SELECT *
FROM (
SELECT DISTINCT year
FROM myTable
) y
JOIN ( SELECT year, ... <some query>
....
) t
ON y.year = t.year
PostgreSQL function:
CREATE FUNCTION record_years() RETURNS SETOF int AS $$
SELECT DISTINCT year FROM myTable ORDER BY 1
$$ LANGUAGE SQL;
Your PHP code:
$sql = "SELECT record_years()";
or
$sql = "SELECT year FROM record_years() AS year";
or alternatively all values in a single row, as a JSON array:
$sql = "SELECT array_to_json(ARRAY(SELECT record_years())) AS arr";
$res = $conn->query($sql);
if($res !== false){
$row = $res->fetch();
$years[] = json_decode($row['arr']);
}

Zend-framework Query TOP

Hello i try generate a query in Zend 2 just like this
select top 10 * from mensaje where idUsuario = 11 order by fechaAltaMensaje DESC
i try use this
$select = $sql->select();
$select->from('mensaje');
$select->where('idUsuario = '.$idUser.' order by fechaAltaMensaje DESC');
$select->limit(5);
but don't work
You are missing some details in your code in order for it to work,
please see below.
$adapter = $this->tableGateway->getAdapter();//use Zend\Db\TableGateway\TableGateway;
$sql = new Sql($adapter);//use Zend\Db\Sql\Sql;
$select = $sql->select();
$select->from('mensaje');
$select->where('idUsuario = '.$idUser.'');
$select->order('fechaAltaMensaje DESC');
$select->limit(5);
$selectString = $sql->getSqlStringForSqlObject($select);//print_r($selectString);die; //gives you the query in string
$results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
$resultSet = new ResultSet();//use Zend\Db\ResultSet\ResultSet;
$resultSet->initialize($results);
return $resultSet->toArray();//the result to array
Please read the tutorials below and you will get the full picture
Examples
Examples 2
The limit function only applies to platforms that support it. To achieve what you're after in SQL you need to use the quantifier function.
Also - where accepts an array of column => value pairs.
And there is an order function that accepts a column name and direction:
$select = $sql->select();
$select->from('mensaje')
->where(['idUsuario' => $idUser])
->order('fechaAltaMensaje DESC')
->quantifier('TOP(5)')
I am not pleased with Zends implementation of the sql abstraction layer, when you need to use two different functions to write SQL that is not cross platform to do simple things like limit or top. That's just my two pence.

select from db sql query

I have this query :
$query = mysql_query ("SELECT *
FROM saledb.application
WHERE app_id = (
SELECT app_id
FROM saledb.applicationdetails
WHERE is_hot = '1'
) LIMIT $Kvet,$Zet
");
And I have the following error:
Unable to save result set in
/home/lemondo/lemondosales.itnovations.ge/content/tpl/gtpl/main.php on
line 68
When I changing select item with MAX(app_id) it works but i need show all results. i know where is problem mysql cant choose in one query meny ID but i need alternativ query.
Use the IN predicate instead of = like os:
SELECT *
FROM saledb.application
WHERE app_id IN
(SELECT app_id
FROM saledb.applicationdetails
WHERE is_hot = '1');
$query = mysql_query ("SELECT * FROM saledb.application WHERE app_id IN (SELECT app_id FROM saledb.applicationdetails WHERE is_hot = '1') LIMIT $Kvet,$Zet");
That should do the trick. If you leave '=' and the subquery returns more than one row, you wil get that error. To match all the lines in saledb.application that have the app_id in the result set you need to use "IN" :)

CodeIgniter - Grouping where clause

I have this following query for CodeIgniter:
$q = $this->db->where('(message_from="'.$user_id.'" AND message_to="'.$this->auth_model->userdata['user_id'].'")')
->or_where('(message_from="'.$this->auth_model->userdata['user_id'].'" AND message_to="'.$user_id.'")')
->get('messages');
I want to write this query with completely active record.
I have tried something like this:
$from_where = array('message_from'=>$user_id, 'message_to'=>$this->auth_model->userdata['user_id']);
$to_where = array('message_from'=>$this->auth_model->userdata['user_id'],'message_to'=>$user_id);
$q = $this->db->where($from_where)
->or_where($to_where)
->get('messages');
die($this->db->last_query());
The above code produces this query:
SELECT * FROM (`messages`) WHERE `message_from` = '2' AND `message_to` = '1' OR `message_from` = '1' OR `message_to` = '2'
But this is what I want to produce:
SELECT * FROM (`messages`) WHERE (message_from="2" AND message_to="1") OR (message_from="1" AND message_to="2")
There are similar questions here and here, but thosedid not provide a real solution for me.
How's this possible, If not via core libraries, is there an extension which allows writing such queries?
Thanks,
You can use sub query way of codeigniter to do this for this purpose you will have to hack codeigniter. like this
Go to system/database/DB_active_rec.php Remove public or protected keyword from these functions
public function _compile_select($select_override = FALSE)
public function _reset_select()
Now subquery writing in available And now here is your query with active record
$this->db->where('message_from','2');
$this->db->where('message_to','1');
$subQuery1 = $this->db->_compile_select();
$this->db->_reset_select();
$this->db->where('message_from','1');
$this->db->where('message_to','2');
$subQuery2 = $this->db->_compile_select();
$this->db->_reset_select();
$this->db->select('*');
$this->db->where("$subQuery1");
$this->db->or_where("$subQuery2");
$this->db->get('messages');
Look at this answer of mine. This shows how to use sub queries. This will help
Using Mysql WHERE IN clause in codeigniter
EDITES
Yes i have done it
Rewrite the query this way exactly you want
$this->db->where('message_from','2');
$this->db->where('message_to','1');
$subQuery1 = $this->db->_compile_select(TRUE);
$this->db->_reset_select();
$this->db->where('message_from','1');
$this->db->where('message_to','2');
$subQuery2 = $this->db->_compile_select(TRUE);
$this->db->_reset_select();
$this->db->select('*');
$this->db->where("($subQuery1)");
$this->db->or_where("($subQuery2)");
$this->db->get('messages');
Compile select is with true parameter. Will not produce select clause. This will produce
SELECT * FROM (`messages`) WHERE (`message_from` = '2' AND `message_to` = '1') OR (`message_from` = '1' AND `message_to` = '2')

SELECT MAX query returns only 1 variable + codeigniter

I use codeigniter and have an issue about SELECT MAX ... I couldnot find any solution at google search...
it looks like it returns only id :/ it's giving error for other columns of table :/
Appreciate helps, thanks!
Model:
function get_default()
{
$this->db->select_max('id');
$query = $this->db->getwhere('gallery', array('cat' => "1"));
if($query->num_rows() > 0) {
return $query->row_array(); //return the row as an associative array
}
}
Controller:
$default_img = $this->blabla_model->get_default();
$data['default_id'] = $default_img['id']; // it returns this
$data['default_name'] = $default_img['gname']; // it gives error for gname although it is at table
To achieve your goal, your desire SQL can look something like:
SELECT *
FROM gallery
WHERE cat = '1'
ORDER BY id
LIMIT 1
And to utilise CodeIgniter database class:
$this->db->select('*');
$this->db->where('cat', '1');
$this->db->order_by('id', 'DESC');
$this->db->limit(1);
$query = $this->db->get('gallery');
That is correct: select_max returns only the value, and no other column. From the specs:
$this->db->select_max('age');
$query = $this->db->get('members');
// Produces: SELECT MAX(age) as age FROM members
You may want to read the value first, and run another query.
For an id, you can also use $id = $this->db->insert_id();
See also: http://www.hostfree.com/user_guide/database/active_record.html#select
CodeIgniter will select * if nothing else is selected. By setting select_max() you are populating the select property and therefore saying you ONLY want that value.
To solve this, just combine select_max() and select():
$this->db->select('somefield, another_field');
$this->db->select_max('age');
or even:
$this->db->select('sometable.*', FALSE);
$this->db->select_max('age');
Should do the trick.
It should be noted that you may of course also utilize your own "custom" sql statements in CodeIgniter, you're not limited to the active record sql functions you've outlined thus far. Another active record function that CodeIgniter provides is $this->db->query(); Which allows you to submit your own SQL queries (including variables) like so:
function foo_bar()
{
$cat = 1;
$limit = 1;
$sql = "
SELECT *
FROM gallery
WHERE cat = $cat
ORDER BY id
LIMIT $limit
";
$data['query'] = $this->db->query($sql);
return $data['query'];
}
Recently I have been utilizing this quite a bit as I've been doing some queries that are difficult (if not annoying or impossible) to pull off with CI's explicit active record functions.
I realize you may know this already, just thought it would help to include for posterity.
2 helpful links are:
http://codeigniter.com/user_guide/database/results.html
http://codeigniter.com/user_guide/database/examples.html