$this->db->where how to use it? - database

In this code, I want to select the records from database only where the employee_id_open and the employee_id_close is the same and is same to a variable $employee_id
$employee_id = 1;
$between = 'between ' . $this->db->escape($this->params['start_date'] . ' 00:00:00').' and ' . $this->db->escape($this->params['end_date'] . ' 23:59:59');
$this->db->select("registers.name as register_name, open_person.first_name as open_first_name, open_person.last_name as open_last_name,open_person.hourly_salary as hourly_salary, close_person.first_name as close_first_name, close_person.last_name as close_last_name, register_log.*, (register_log.close_amount - register_log.open_amount - register_log.cash_sales_amount) as difference");
$this->db->from('register_log as register_log');
$this->db->join('people as open_person', 'register_log.employee_id_open=open_person.person_id');
$this->db->join('people as close_person', 'register_log.employee_id_close=close_person.person_id', 'left');
$this->db->join('registers', 'registers.register_id = register_log.register_id');
//------My modification - but this is not working. no records are selected with this query
$this->db->where('open_person.person_id ', 'close_person.person_id');
// I also tried this>>> $this->db->where('open_person.person_id = ', 'close_person.person_id');
//------/ My modifcation
$this->db->where('register_log.shift_start ' . $between);
$this->db->where('register_log.deleted ', 0);
$this->db->where('registers.location_id', $location_id);
Can you please tell me what I am doing wrong and how can I fix this?
Any help would be much appreciated.
Many Thanks

you can dump the last query sql statement using $this->db->last_query().

Related

Laravel concat query return 0 items

Here is my controller code:
if ($request->exists('tipo')) {
$valor = $request->value;
$candidates = Candidate::buscarpor($tipo, $valor)
->orderBy('id', 'desc')
->Paginate(5)
->withQueryString();
dd($candidates);
}
And this is the scope "buscarpor" inside my "Candidate" model:
public function scopeBuscarpor($query, $tipo, $valor)
{
if(($tipo) && ($valor)) {
if($tipo == 'names') {
// return $query->orWhereRaw("concat(name, ' ', last_name) like '%".$valor."%' ");
return $query->where(DB::raw("CONCAT('name', ' ', 'last_name')"), 'like', '%'.$valor.'%')
->orWhere(DB::raw("CONCAT('last_name', ' ', 'name')"), 'like', '%'.$valor.'%');
}
return $query->where($tipo, 'like', "%$valor%");
}
}
When the search is of type "names" I should query in the DB to search a candidate/person by using his first name or last name, I only have one input type text, I just writting all his names is this input type text.
The variable $valor inside of this scope has data and no problem with it.. I tested adding a name that exists in my database but it returns 0 items.
This my dd($candidates) output.
I don't know what I'm doing wrong, please guys if you have some idea about how fix this problem, I will appreciate it.. Thanks so much.
Maybe the where expressions are not correctly formed. Try writing it like this
$query->where(DB::raw("CONCAT('name', ' ', 'last_name') like '%?%'", [$valor]))
->orWhere(DB::raw("CONCAT('last_name', ' ', 'name') like '%?%'", [$valor]));
You can also use the whereRaw() syntax instead
$query->whereRaw("CONCAT('name', ' ', 'last_name') like '%?%'", [$valor])
->orWhereRaw("CONCAT('last_name', ' ', 'name') like '%?%'", [$valor]);
Or maybe the names are written with capital letters in the database? If you are using postgreSQL, you can use ilike instead of like for a case insensitive search.
If you're using MySQL, you might have to use lower() in your raw methods.
$query->whereRaw("LOWER(CONCAT('name', ' ', 'last_name')) like '%?%'", [$valor])
->orWhereRaw("LOWER(CONCAT('last_name', ' ', 'name')) like '%?%'", [$valor]);

Problem with IIF function in Microsoft Access

Good morning all,
I need your help !
I am working in Microsoft Access with ODBC database.
I want to fill in the blank in many fields, I have 6 columns and sometimes it happened to me that I have no items in SSCDT (sub-conditioning) , I want when I have no items in SSCDT, replace them with items in CDT (conditioning)
I created this code and since I modified the part (IIF) of SSCDT I have this error:
SELECT DISTINCT SOC1_PRO.MOTCLE AS CODPRO, SOC1_PRO.CODUNI AS UV, Min(Round((SOC1_TSC.PRXTAR*0.85),2)) AS PV1, IIF(MAX(IIF(SOC1_PRU.CODUNI='SAC', SOC1_PRU.COEFUV,' '))=' ', MAX(IIF(SOC1_PRU.CODUNI='CAR', SOC1_PRU.COEFUV,' ')) ,MAX(IIF(SOC1_PRU.CODUNI='SAC', SOC1_PRU.COEFUV,' '))) AS SSCDT, Min(SOC1_TSC.PRXTAR) AS PV2, MAX(IIF(SOC1_PRU.CODUNI='CAR', SOC1_PRU.COEFUV,' ')) AS CDT, Min(SOC1_PRO.NOMPRO) AS Désignation
FROM (SOC1_PRO AS SOC1_PRO INNER JOIN SOC1_TSC ON SOC1_PRO.CODPRO = SOC1_TSC.CODPRO) INNER JOIN SOC1_PRU ON SOC1_TSC.CODPRO = SOC1_PRU.CODPRO
WHERE (((SOC1_TSC.DATFIN)=' ') AND ((SOC1_TSC.CODSOC)='1') AND ((SOC1_TSC.DATDEB)>='20210000') AND ((SOC1_PRO.CODZN6)<>'N') AND ((SOC1_TSC.CODDEV)='EUR')) AND (SOC1_PRU.CODUNI = 'SAC' OR SOC1_PRU.CODUNI='CAR')
GROUP BY SOC1_PRO.MOTCLE, SOC1_PRO.CODUNI;
For exemple :
I hope i be clear with my explanation
Thank you ! <3

how to get rid of "ADOQuery Field name not found" error in Delphi? (Using MS Access, and ADOQuery)

im trying to display a (For example): UserCode and Password that is already set in my MS Access Database for Delphi
if there is any fix for this i would appreciate it.
this is part of my code that im using to obtain the infos and display them into a TLabels and in which it returns the error that i'm having..
with DataModule5 do
begin
// using the following code should display credentials from ms-access database to label text (TESTING)
ADOQuery1.SQL.Text := 'SELECT * FROM Credentials ' +
'WHERE (UserCode = :UserCode) ' +
'AND (Password = :Password) ' +
'AND (FirstName = :FirstName) ' +
'AND (LastName = :LastName) ' +
'AND (Age = :Age) ' +
'AND (Adminstrator = :Adminstrator) ';
MyAccountPage.UsernameDetail.Caption := ADOQuery1.FieldByName('UserCode').asString;
MyAccountPage.PasswordDetail.Caption := THashMD5.GetHashString(ADOQuery1.FieldByName('Password').AsString);
ADOQuery1.Open;
MyAccountPage.Show;
end;
and this is what i get when i try to access to "My Account" form:
PS: Not just "UserCode" that is not found, but even the remaining details (Password, FirstName.. etc)
Thanks for help in advance!
You are reading values from your ADOQuery before you fetched them
Change
MyAccountPage.UsernameDetail.Caption := ADOQuery1.FieldByName('UserCode').asString;
MyAccountPage.PasswordDetail.Caption := THashMD5.GetHashString(ADOQuery1.FieldByName('Password').AsString);
ADOQuery1.Open;
to this
ADOQuery1.Open;
MyAccountPage.UsernameDetail.Caption := ADOQuery1.FieldByName('UserCode').asString;
MyAccountPage.PasswordDetail.Caption := THashMD5.GetHashString(ADOQuery1.FieldByName('Password').AsString);
EDIT
But that is not the only problem with your code.
As suggested by whosrdaddy in the comments, your parameters are not set. Please read about how to use parameters here
Also, never store passwords in clear text in your database. Better store the hash of the password, you can concatinate it with the user code as suggested by fpiette in the comments

In SQL Server, REPLACE function is not working properly when I use to replace with 1 space

select
replace(WBSElem, appProj, '') AS WBSElement,
replace(WBSElem + '#' + Descrip, '', '') as Descrip,
*
from
SAP_sFTP_DB.dbo.MMHE_PS_WBS
where
appProj = '1.13T0117'
order by
RecID asc
Here second replace is working fine, but the 1st replace is not working properly where I have above 100 records to change. I am getting output like nothing happened for 1st replace function. Can anybody help me?
select replace(WBSElem, rtrim(ltrim(appProj)), '') AS WBSElement,
replace(WBSElem+'#'+Descrip,'','')as Descrip, * from SAP_sFTP_DB.dbo.MMHE_PS_WBS where appProj='1.13T0117'
Due to White spaces the function did'nt work.Now its fine

Raiseerror and Concat for the Message

I'd like to do something like this
raiserror(concat('Error in case #isFishy =', #isFishy, ' #isSmarmy=', #isSmarmy, ' #isTasty = ', #isTasty), 10, 1)
--or
raiserror('Error in case #isFishy =' + #isFishy + ' #isSmarmy=' + #isSmarmy + ' #isTasty = ' + #isTasty, 10, 1)
But it just isn't working. How do I accomplish this? I'm in SQL Server 2005.
The error message in RAISERROR has actually similar syntax to printf function in C, so assuming your arguments are of the type of integer you would need to use:
raiserror(N'Error in case #isFishy = %d #isSmarmy = %d #isTasty = %d',10,1,#isFishy,#isSmarmy,#isTasty)
check out BOL for details and other options
I use raiserror a lot. We have some stored procedures that are called from a .Net app each night for batch processing, and the .Net app wants to log the procedure output this way. I don't know why, but I generally have to build the string before calling raiserror.

Resources