How can I do this SQL in Query builder laravel - sql

How can I do this SQL in Query builder laravel
select Doctor_id from doctors where Doctor_id NOT IN
(SELECT Doctor_id from report_reviewers WHERE Report_id = 26 )

Try with this
$result = DB::table('doctors')
->whereNotIn('doctor_id', function($q){
$q->from('report_reviewers')
->select('Doctor_id')
->where('Report_id', '=', 26)
})
->select('doctor_id')
->get();
Whenver in doubt, if you know the raw SQL, you can simply do
$query = 'select Doctor_id from doctors ...';
$result = DB::select($query);
Also, all credit goes to this genius How to create a subquery using Laravel Eloquent?
Update
The missing argument comes from the closure you are using
->whereNotIn('doctor_id', function($q,$id){
$q->from('report_reviewers')
->select('Doctor_id')
->where('Report_id',$id);
})
You need to pass the $id variable like this
->whereNotIn('doctor_id', function($q) use ($id) {
$q->from('report_reviewers')
->select('Doctor_id')
->where('Report_id',$id);
})
Try again and see.

Related

exists query does not return relevant result

I have a Laravel application with this Eloquent query:
$products = Product::where('name', 'LIKE', "%{$value}%")
->whereHas('categories', function($q) {
$q->where( 'slug', 'tonery-cartridge' );
})->with('manufacturer')
->with('sm_image')
->orderBy('created_at','DESC')
->take(10)
->get();
This code generates the sql command like:
select * from `products` where `name` LIKE '%can%'
and exists (
select * from `categories` inner join `category_product`
on `categories`.`id` = `category_product`.`category_id`
where `products`.`id` = `category_product`.`product_id`
and `slug` = 'tonery-cartridge'
)
order by `created_at` desc limit 10
I am sure there are products which name contains "can" string and which belongs to the category with slug "tonery-cartridge". Why this query returns an empty result? If I try to make inner join sql manually it works well as on the screenshot below:
I think that your queries are not equivalent. The SQL output from laravel does not join tables in it's FROM clause, but in your manually constructed SQL statement you do a lot of inner joins in your FROM clause and on the resulting table you perform your operations, which is not true for the former.
Try the following
DB::table('products')
->join('category_product', 'category_product.product_id', '=', 'products.id')
->join('categories', 'category_product.category_id', '=', 'categories.id')
->whereRaw('products.name LIKE %can% AND categories.slug = "tonery-cartridge"')
->select('products.name', 'categories.slug')
->orderBy('created_at','DESC')
->take(10)
->get()
If you want to avoid using whereRaw, you can try the following.
DB::table('products')
->join('category_product', 'category_product.product_id', '=', 'products.id')
->join('categories', 'category_product.category_id', '=', 'categories.id')
->where([
['products.name, 'LIKE', '%' . $value . '%'],
['categories.slug', '=', 'tonery-cartridge']])
->select('products.name', 'categories.slug')
->orderBy('created_at','DESC')
->take(10)
->get()
HTH
This could be one of the solutions
$category = Category::where( 'slug', 'tonery-cartridge' )->first();
$products = $category->products()
->where('name', 'LIKE', "%{$value}%")
->with('manufacturer')
->with('sm_image')
->latest()
->take(10)
->get();

Yii2: how to use subquery with ActiveRecord?

I want to create a find function to use then in a GridView widget My I don't know how to use a subquery.
This is the simple version of the PostgreSQL query that I have:
SELECT color
FROM cars
LEFT JOIN (
SELECT name
FROM companies
)
Here is what I a trying and doesn't work:
$query = Cars::find()
->select([
'color' => '
SELECT name // Problem here.
FROM companies // Problem here.
',
]);
Your expected query is not what you are trying to do. You can add a custom subquery as
$query = Cars::find()
->select([
'(SELECT name FROM companies) AS xxx'
]);
But if you want to add a field from joined table to the grid, then you should have a relation defined and then you can easily add such a column to your grid. See https://www.yiiframework.com/doc/guide/2.0/en/db-active-record#relational-data
You can use instance of ActiveQuery in leftJoin() method like this:
$subQuery = Companies::find()
->select('name', 'id'); //I've added ID to show how to write on condition
$query = Cars::find()
->select('color')
->leftJoin(
['alias' => $subQuery],
'alias.id = cars.company_id'
);
See documentation for more details about leftJoin() and join() methods.
$query = Cars::find()
->select(['cars.color','companies.name'])
->leftJoin('companies', 'cars.company_id = companies.id');

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']);
}

Multiple joins using active record with YII framework

im getting stuck with active record for yii.
im using models for each table in my db.
the sql i try to get is querying and joining from 3 tables.
what i want to accomplish is the equivalent of the sql command :
SELECT location_code as stopkey, bay_no bay_no, description stop_name, route_area_code route_area_code, latitude latitude, longitude, build_code build_code, message_time message_time, ip_address ip_address, route.route_code route, make make, last_impact last_impact, impact_count impact_count, last_bootup last_bootup, bootup_count bootup_count, last_active_hour last_active_hour, last_active_day last_active_day, operator.operator_code operator_code, routes routes, bearing
FROM snapshot_stop_status route
JOIN route_visibility ON route.route_id = route_visibility.route_id
JOIN operator ON operator.operator_id = route_visibility.operator_id
WHERE usernm = 'me'
ORDER BY location_code
here is what i tried so far :
public function relations() {
return array(
'RoutesVisibility' =>array(self::MANY_MANY,'route_visibility','route_id'),
);
}
How can i accomplish this using relations function in the models ?
any kind of help would be appreciated
route_visibility is the joining table that tie operator to snapshot_stop_status. So in yii you can specify the table doing the relations:
'operators' =>array(self::MANY_MANY,'Operator','route_visibility(route_id,operator_id )'),
And in Operator:
'routes' =>array(self::MANY_MANY,'Route','route_visibility(operator_id, route_id)'),
Then you can request with something like:
$criteria = new CDbCriteria;
$criteria->with = array('operators' => array('condition' => 'usernm = "me"'));
$models => Route:model()->findAll($criteria);

How can I generate the SQL query using SQL::Abstract?

How do I generate the WHERE clause for this query using SQL::Abstract:
SELECT COUNT(*) FROM table WHERE id =
111 AND NOT FIND_IN_SET(type,
'1,2,3,4') AND status = 'pending';
What's the proper way to include conditions like WHERE FIND_IN_SET(type, '1,2,3,4')?
See the not_bool unary operator option:
use SQL::Abstract;
my $sql = SQL::Abstract->new;
my $where = {
id => 111,
status => 'pending',
-not_bool => "FIND_IN_SET(type, '1,2,3,4')",
};
my ($query, #bind) = $sql->select(
'table',
'count(*)',
$where,
);
This is how $query looks:
SELECT count(*) FROM table WHERE ( ( (NOT FIND_IN_SET(type, '1,2,3,4'))
AND id = ? AND status = ? ) )
This code generates the WHERE clause:
my $sql = SQL::Abstract->new;
my %where = (
id => 111,
-nest => \"NOT FIND_IN_SET(type, '1,2,3,4')",
status => 'pending',
);
my ($stmt, #bind) = $sql->where(\%where, \#order);
FIND_IN_SET isn't standard SQL, so SQL::Abstract doesn't have support for it. You can however put any literal SQL into an SQL::Abstract query. I expect your solution lies down that route.
Take a look at: DALMP
Database Abstraction Layer for MySQL using PHP
0% fat and extremely easy to use. Only connect to database when needed.
http://code.google.com/p/dalmp/