In my first query, I am looping through all records that have a status of open, and channel_id of 23, getting the column field_id_299. The field field_id_299 is the entry_id for another channel.
The second query, then uses this field_id_299 AS entry_id, getting the column field_id_36.
Is it possible to do all of this in one query?
$query = ee()->db->select('cd.field_id_299')
->from('exp_channel_titles AS ct')
->join('exp_channel_data AS cd', 'ct.entry_id = cd.entry_id', 'left')
->where('ct.status', 'open')
->where('ct.channel_id', '23')
->get();
$query = ee()->db->select('field_id_36')
->from('exp_channel_data')
->where('entry_id', $entry_id)
->get();
Try below
$query = ee()->db->select('cd1.field_id_36')
->from('exp_channel_titles AS ct')
->join('exp_channel_data AS cd', 'ct.entry_id = cd.entry_id', 'left')
->join('exp_channel_data AS cd1', 'cd.field_id_299 = cd1.entry_id', 'left')
->where('ct.status', 'open')
->where('ct.channel_id', '23')
->get();
Related
I have MySql Query :
select * from transactions where request_shipping_date = '2020-11-25' and status = 'paid' or 'on_process' or 'finish';
and I got 44 Rows
and I Want Convert to Eloquent Model :
$request_shipping_date = Carbon::now()->addDays(1)->format('Y-m-d');
$data = Order::with('order_details')
->where('request_shipping_date', 'like', $request_shipping_date.'%')
->Where('status', 'paid')
->orWhere('status', 'on_process')
->orWhere('status', 'finish')
->get();
and i got : 24025 Rows
Is there any wrong ?
The no of rows retrieved changes because of the like operator when comparing dates
Try using the below
$request_shipping_date = Carbon::now()->addDays(1)->format('Y-m-d');
$data = Order::with('order_details')
->whereDate('request_shipping_date', $request_shipping_date)
->Where('status', 'paid')
->orWhere('status', 'on_process')
->orWhere('status', 'finish')
->get();
You could also rephrase the eloquent query as
$request_shipping_date = Carbon::now()->addDays(1)->format('Y-m-d');
$data = Order::with('order_details')
->whereDate('request_shipping_date', $request_shipping_date)
->whereIn('status', ['paid', 'on_process', 'finish'])
->get();
I want to get values from 'tournament_week' table of mysql but if one of its coulmn ''is null then get value from another table 'tournament' which have that same coulmn name. please guide me in codeigniter
MY MODEL
$this->db->select(if'w.*, t.name tournament_name');
$this->db->from('tournament_week w');
$this->db->join('tournament t', 'w.tournament_id = t.id');
$this->db->where('w.tournament_id=', $tid);
$this->db->where('w.show_week', 1);
$this->db->order_by("w.week_no", "ASC");
$query = $this->db->get()->result_array();
return $query;
Assuming the column name is name
$this->db->select('coalesce(w.name, t.name) as tournament_name, w.tournament_id,<all other tournament_week fields>');
COALESCE function should work.
Try following.
$this->db->select('COALESCE(w.name, t.name) tournament_name);
$this->db->from('tournament_week w');
$this->db->join('tournament t', 'w.tournament_id = t.id');
$this->db->where('w.tournament_id=', $tid);
$this->db->where('w.show_week', 1);
$this->db->order_by("w.week_no", "ASC");
$query = $this->db->get()->result_array();
return $query;
I have the following raw SQL query:
select a.id user_id, a.email_address, a.name_first, a.name_last, count(b.id) number_of_videos, sum(b.vimeo_duration) total_duration, sum(b.count_watched) total_playbacks
from users a,
videos b
where a.id = b.tutor_id
and a.email_address in ('candace_rennie#yahoo.com', 'tjm#hiltoncollege.com', 'matthewjameshenshall#gmail.com', 'nkululeko#syafunda.co.za', 'khulile#syafunda.co.za', 'nzakheni#syafunda.co.za')
group by a.id;
This correctly gets 6 rows from the database. I'm trying to convert this to a Laravel database query like so:
$totals = DB::table('users')
->select(DB::Raw('users.id as user_id'), 'users.email_address', 'users.name_first', 'users.name_last', DB::Raw('count(videos.id) as number_of_videos'), DB::Raw('sum(videos.vimeo_duration) as total_duration'), DB::Raw('sum(videos.count_watched) as total_playbacks'))
->join('videos', 'users.id', '=', 'videos.tutor_id')
->where('users.id', 'videos.tutor_id')
->whereIn('users.email_address', array('candace_rennie#yahoo.com', 'tjm#hiltoncollege.com', 'matthewjameshenshall#gmail.com', 'nkululeko#syafunda.co.za', 'khulile#syafunda.co.za', 'nzakheni#syafunda.co.za'))
->groupBy('users.id')
->get();
This however return 0 rows. Is there anything I'm missing?
It should be smth like below even tho groupBy user id does not help much as id is unique.
$aggregates = [
DB::raw('count(b.id) as number_of_videos'),
DB::raw('sum(b.vimeo_duration) as total_duration'),
DB::raw('sum(b.count_watched) as total_playbacks'),
];
$simpleSelects = ['users.email_address', users.id, 'users.name_first', 'users.name_last'];
$emails = ['candace_rennie#yahoo.com', 'tjm#hiltoncollege.com'....]
$users = Users::select(array_merge($simpleSelects, $aggregates))
->leftJoin('videos as b', function ($join) use ($emails) {
$join->on('b.tutor_id', 'a.id')
->whereIn('users.email_address', $emails);
})
->groupBy('users.id')
->get();
Try to remove this line:
->where('users.id', 'videos.tutor_id')
List item
after sql code convert into laravel
DB::select('posts.id','posts.title','posts.body')
->from('posts')
->where('posts.author_id', '=', 1)
->orderBy('posts.published_at', 'DESC')
->limit(10)
->get();
So I'm not sure whether I asked the question well. I'm new to laravel and right now I'm building an application where I need to fetch data for a user if certain conditions are met. This is what I'm trying to achieve:
Select from database where userid = $user and userstatus = active or inactive. or pending.
So all rows with the user id($user) and status(active, inactive and pending) will be returned.
I tried doing:
$users = DB::table('users')
->where('status', '=', 'active')
->orwhere('status', '=', 'inactive')
->orwhere('status', '=', 'pending')
->get();
This returned all the data in that particular table. What is the best way to go about this.
You have at least two options for how to do this you can use, including both so you have one for use in this case and one for general usage. Using a function in where creates a new block, so works like adding parentheses to your query
$users = DB::table('users')
->where('id', $user)
->where(function($query) {
$query->where('status', 'active')
->orWhere('status', 'inactive')
->orWhere('status', 'pending');
})->get();
or
$users = DB::table('users')->where('id', $user)->whereIn('status', ['active', 'inactive', 'pending'])->get();
You can do it as:
$users = DB::table('users')
->where('userid', $userid)
->whereIn('status',['active', 'inactive','pending'])
->get();
This is my query that i used for left join, it is working fine but something is missing when I convert it to laravel syntax.
Query to convert is
$result = DB::select("select amenities.name as
name,amenities.type_id,amenities.id as id, amenities.icon, rooms.id as status
from amenities left join rooms on find_in_set(amenities.id, rooms.amenities)
and rooms.id = $room_id and type_id !=4");
and the I am doing this
$result = DB::table('amenities')
->select('amenities.name as name', 'amenities.type_id' , 'amenities.id as id'
, 'amenities.icon', 'rooms.id as status' )
->leftJoin('rooms', function ($join) {
$join->on('FIND_IN_SET(amenities.id, rooms.amenities)')
->where('rooms.id' , '=', '$room_id')
->where('type_id','!=', 4);
})->get();
The error is
InvalidArgumentException in
F:\xampp\htdocs\arheb\Arheb\vendor\laravel\framework\src\Illuminate\Database\Query\JoinClause.php
line 79: Not enough arguments for the on clause.
Your query is wrong. I assume that amenities.id and rooms.amenities are attributes of amenities and rooms table respectively.
MySQL FIND_IN_SET() returns the position of a string if it is present (as a substring) within a list of strings.
You need to pass column names in first and second parameter of on() function.
$result = DB::table('amenities')
->select('amenities.name as name', 'amenities.type_id' , 'amenities.id as id'
, 'amenities.icon', 'rooms.id as status' )
->leftJoin('rooms', function ($join) {
$join->on('amenities.id', '=', 'rooms.amenities')
->where('rooms.id' , '=', '$room_id')
->where('type_id','!=', 4);
})->get();
I think you can try this:
$result = DB::table('amenities')
->select('amenities.name as name', 'amenities.type_id' , 'amenities.id as id'
, 'amenities.icon', 'rooms.id as status' )
->leftJoin('rooms', function ($join) {
$join->on(DB::raw("find_in_set(amenities.id, rooms.amenities)"))
->where('rooms.id' , '=', '$room_id')
->where('type_id','!=', 4);
})->get();
Hope this work for you!