Is there any modified query for this query: - database

I am using laravel.In my controller I used a query(given below).Is there any modified query for this?? Or is it okk???
$sub = Studentacademicinfo::where('class','=',$cat)->where('section','=',$section)->get();

You could do this if you wanted to tidy up the code:
$sub = Studentacademicinfo::whereClass($cat)->whereSection($section)->get();
But the resulting SQL will be the same

Related

How can I get data from laravel using stored procedure?

I want to get the data and put it on dd just try check and got nothing
Here is my controller
controller.php
public function editView($crewprogrammemoid){
$transactionmemo = DB::connection('sqlsrv2')
->select("exec sp_Crewprogrammemo_Select 'crewprogrammemo_id = ''?'' ' ", [$crewprogrammemoid]);
dd(collect($transactionmemo)->all()));
}
and I got nothing
How do I solve that? Because if I can, I try to get some specific column.
Here is the data when I execute on my SQL Server:
For example I want to try getting value on column like this
dd($transractionmemo['show_name']);
How can I do that?

Laravel 5.5 can't get record from DB

I try to learn Laravel 5.5, but I have problem to use DB;
DB Table have records.
Following code return some records.
$result = DB::select(DB::raw("select * from todays where FROM_UNIXTIME(login, '%Y/%m/%d') = '2019/03/04'"));
But following code doesn't return any records. What is wrong this ?
$result = DB::table('todays')->where(DB::raw("FROM_UNIXTIME(login, '%Y/%m/%d')", DB::raw('2019/03/04')))->get();
Please help me.
Thanks
Your query seems to be wrong.
$result = DB::table('todays')->where(DB::raw("FROM_UNIXTIME(login, '%Y/%m/%d')"), DB::raw('2019/03/04'))->get();
or
$result = DB::table('todays')->where(DB::raw("FROM_UNIXTIME(login, '%Y/%m/%d')"), '2019/03/04')->get();
First one is more of an error-prone while the second one is used as pdo statement.

From T-Sql to Linq-to-SQL

I need a very simple query in my aplication but I can't figure out how to translate it into Linq-to-SQL because I need it in an asp.net application.
It is neccesery that it is a lambda expression because of internal procedures.
In T-SQL it looks like this:
select top 1 [datum]
from Test
where Datum <> (select max (Datum) from Test)
Sorry for the stupid question in advance.
You can use the Max function inline with the query
var result = Test
.Where(t => t.Datum != Test.Max(t1 => t1.Datum))
.Select(t => t.Datum)
.FirstOrDefault();
To apply such query using lambda expression first you need to fetch all records from table test and the list that you get can be used to get the output you want.
Sample query would be as follows after you fetch the list from the database.
var test= Tests
.OrderByDescending(e => e.datum)
.Skip(1)
.First();

how to get approximate searching results in sql server 2014

How to write sql procedure to get matching results while searching for string values.It should search by the similarity of the characters entered by user. For instance Ahmmed, Ahmad, Ahmmad or Mohammad, Mohammed, Mohamad etc should display all similar names while executing search.
Any help would be highly appreciated.
Thanks in advance.
Well I guess you need AI for that but, you can use the like keyword to do wildcards.
e.g.
$search_string = 'Ahm'; //or Moha
$query = SELECT column FROM tableName WHERE column LIKE '$search_string%'; //starts with the $search_string
or
$query = SELECT column FROM tableName WHERE column LIKE '%$search_string'; //ends with the $search_string
or
$query = SELECT column FROM tableName WHERE column LIKE '%$search_string%'; //starts and/or ends with the $search_string
take note of the %

How to create db_select query from simple sql query

I have a sql query as:
SELECT `entity_type` FROM `field_data_field_project_team_member` WHERE field_project_team_member_uid=30
and i want it to write in a db_select().
How to write it in db_select ??
I go through various tutorials and found drupal formate to write a query for it as:
$sub_qry = db_select('field_data_field_project_team_member', 't');
$sub_qry->fields('t', array('entity_id'));
$sub_qry->where("t.field_project_team_member_uid = 30");
Hope its useful for someone else.

Resources