MYSQL Query to Codeigniter Query - php

I have the query below that works fine when I use it in phpMyAdmin, I am just a bit unsure how to do it within the CI framework when I have the m.id etc in place.
Query:
SELECT DISTINCT m.name, m.id, c.id, c.name
FROM `default_ps_products` p
INNER JOIN `default_ps_products_manufacturers` m ON p.manufacturer_id = m.id
INNER JOIN `default_ps_product_x_cats` x ON p.id = x.product_id
INNER JOIN `default_ps_products_categories` c ON x.category_id = c.id

There are many ways.
Example 1:
$result = $this->db
->select('m.name, m.id, c.id, c.name')
->distinct()
->join('default_ps_products_manufacturers m', 'p.manufacturer_id=m.id')
->join('default_ps_product_x_cats x', 'p.id=x.product_id')
->join('default_ps_products_categories c', 'x.category_id=c.id')
->get('default_ps_products p')
->result();
echo $this->db->last_query();
Sometimes the active record can't produce the query you want. So you can write it yourself.
Example 2:
$query = "SELECT DISTINCT m.name, m.id, c.id, c.name
FROM `default_ps_products` p
INNER JOIN `default_ps_products_manufacturers` m ON p.manufacturer_id = m.id
INNER JOIN `default_ps_product_x_cats` x ON p.id = x.product_id
INNER JOIN `default_ps_products_categories` c ON x.category_id = c.id";
$result = $this->db
->query($query)
->result();
echo $this->db->last_query();
In this second example, db::query() can take an array as the second parameter that will replace any question marks (?) within $query with the respective value. For example say you needed to add some where values to your query.
Example 3:
$query = "SELECT DISTINCT m.name, m.id, c.id, c.name
FROM `default_ps_products` p
INNER JOIN `default_ps_products_manufacturers` m ON p.manufacturer_id = m.id
INNER JOIN `default_ps_product_x_cats` x ON p.id = x.product_id
INNER JOIN `default_ps_products_categories` c ON x.category_id = c.id
WHERE c.id=?";
$result = $this->db
->query($query, array(1))
->result();
echo $this->db->last_query();

Related

translate this code in phpmyadmin to laravel

i want to translate this code in phpmyadmin to laravel
SELECT AVG(valeur_5), id_client, nom_propriete, prenom_propreite, nom_entreprise, type_utilisatuer
FROM cliets c
INNER JOIN evaluations e on c.id = e.id_client
INNER JOIN users u on u.id = c.id_user
INNER JOIN propreites p on p.id_user = u.id
GROUP BY id_client
If you want to embed raw SQL into your program, just wrap it in a DB::select.
DB::select("SELECT AVG(valeur_5), id_client, nom_propriete, prenom_propreite, nom_entreprise, type_utilisatuer FROM cliets c INNER JOIN evaluations e on c.id = e.id_client INNER JOIN users u on u.id = c.id_user INNER JOIN propreites p on p.id_user = u.id GROUP BY id_client")
Alternatively, you can rewrite it with the Eloquent query builder.
try this:
$orders = DB::table('hrayfis')
->join('evaluations','evaluations.id_herfi','hrayfis.id')
->join('users','users.id','hrayfis.id_user')
->join('propreites','propreites.id_user','users.id')
->select(
DB::raw('avg(valeur_5)','propreites.nom_propriete '))
->groupBy('hrayfis.id')->get();

I'm not able to convert the Mysql query into laravel query builder

I'm struggling to convert this below MySQL query into laravel query builder. It has a lot of joins and subqueries inside. Can someone please help me?
MYSQL query:
SELECT a.id , b.*, a.agent_id, td.contacts, td.addresses
FROM teams a
left join team_details as td on td.team_id = a.id
LEFT OUTER JOIN
(
SELECT m.id, m.due_date, t.team_id, m.department, m.assigned_to , cm.title, u.name
FROM (
SELECT team_id, MAX(due_date) AS due_date
FROM campaign_activities where task_status=false and due_date is not null
GROUP BY team_id
) t JOIN campaign_activities m ON m.team_id = t.team_id AND t.due_date = m.due_date
left join campaign_activity_masters cm on cm.id = m.camp_activity_id
left join users u on u.id = m.assigned_to
) b ON a.id = b.team_id
order by a.id
I'm trying something like below but got stuck with LEFT OUTER JOIN as it has sub query inside :).
DB::table('teams as a')
->leftjoin('team_details as td', 'td.team_id','=','a.id')
->select('a.id', 'b.*', 'a.agent_id','td.contacts','td.addresses')
->leftouterjoin(DB::select(DB::raw('SELECT m.id, m.due_date, t.team_id, m.department, m.assigned_to , cm.title, u.name
FROM (
SELECT team_id, MAX(due_date) AS due_date
FROM campaign_activities where task_status=false and due_date is not null
GROUP BY team_id
) t JOIN campaign_activities m ON m.team_id = t.team_id AND t.due_date = m.due_date
left join campaign_activity_masters cm on cm.id = m.camp_activity_id
left join users u on u.id = m.assigned_to')))
->get();
Any suggestions, please? I want to apply paginate function paginate() instead of get() for this query builder.
I think you should try.
$cards = DB::select("SELECT a.id , b.*, a.agent_id, td.contacts, td.addresses
FROM teams a
left join team_details as td on td.team_id = a.id
LEFT OUTER JOIN
(
SELECT m.id, m.due_date, t.team_id, m.department, m.assigned_to , cm.title, u.name
FROM (
SELECT team_id, MAX(due_date) AS due_date
FROM campaign_activities where task_status=false and due_date is not null
GROUP BY team_id
) t JOIN campaign_activities m ON m.team_id = t.team_id AND t.due_date = m.due_date
left join campaign_activity_masters cm on cm.id = m.camp_activity_id
left join users u on u.id = m.assigned_to
) b ON a.id = b.team_id
order by a.id");
Hope it help you.

If statement in sql query with inner join

I am sorry, I know there is a lot of answer on the forum concerning that question but I didn't succeed to adapt an answer to my problem.
Here is my function :
public function getPupil($db, $pupil_id){
$pupil = $db->query('
SELECT
a.gender as genderPupil,
pupil_name,
pupil_lastname,
a.id as pupilId,
b.id as idOne,
c.id as idTwo,
b.lastname as lastnameOne,
b.name as nameOne,
c.name as nameTwo,
c.lastname as lastnameTwo,
birthdate,
school,
level,
allergies,
special_diet,
tap,
d.name as tapNameFirstTerm,
e.name as tapNameSecondTerm,
f.name as tapNameThirdTerm
FROM pupils a
INNER JOIN responsibles b ON a.id_responsibles = b.id
INNER JOIN responsibles c ON a.id_responsibles_two = c.id
INNER JOIN tap_activities d ON a.tap_first_term = d.id
INNER JOIN tap_activities e ON a.tap_second_term = e.id
INNER JOIN tap_activities f ON a.tap_third_term = f.id
WHERE a.id = ?
', [$pupil_id])->fetch();
return $pupil;
}
I would like to add conditions to do not query the INNER JOIN when a.tap_first_term or a.tap_second_term or a.tap_third_term are equal to 0.
Thank you very much for your answers.
I found my solution.
For the people that could be interested, I just changed the three INNER JOIN for the tap_activities by LEFT JOIN. And it works as I want.

MYSQL customizable join and select

i want to use JOIN categories c ON c.id = i.category if post_type is 1 and i want to have c.title AS category_name, in SELECT otherwise JOIN categories c ON c.id = i.category and c.title AS category_name, must be not worked in query , i'm using case for join but my sql command is not correct. please help me. in all description my quastion means is how to change this below command to if post_type is 1 join must be not work
SELECT SQL_CALC_FOUND_ROWS
i. * ,
c.title AS category_name,
s.title AS status_title,
i.thumb_image,
CONCAT( u.name, ' ', u.family ) AS author
FROM contents i
LEFT JOIN categories c
ON i.category = CASE post_type WHEN 1 then c.id END
JOIN users u ON u.id = i.posted_by
JOIN status_topics s ON s.id = i.t_status
WHERE i.id = 2
I would always do the LEFT JOIN and put a condition on the column itself:
SELECT SQL_CALC_FOUND_ROWS
i. * ,
IF(i.post_type = 1, c.title, NULL) AS category_name,
s.title AS status_title,
i.thumb_image,
CONCAT( u.name, ' ', u.family ) AS author
FROM contents i
LEFT JOIN categories c ON i.category = c.id
JOIN users u ON u.id = i.posted_by
JOIN status_topics s ON s.id = i.t_status
WHERE i.id = 2

insert php code in sql query

function search_num_rows($param){
$company_name=$param['company_name'];
$loan_no=$param['loan_no'];
$q = $this->db->query("select Count(0) as num_rows
from contact_new
inner join companies c on contact_new.company_id = c.id
inner join history on contact_new.id = history.receiver_email
inner join escalation_level on contact_new.escalation_level_id = escalation_level.id
inner join departments on contact_new.departmend_id = departments.id
WHERE loan_no= '$loan_no' if($company_name){ AND company_name= '$company_name'} ")->result();
return $q[0]->num_rows;
}
can i insert the php code as i done in where clause.Is there any other way to do this without using active records.
It's actually very easy:
function search_num_rows($param){
$company_name = (isset($param['company_name']) && !empty($param['company_name']) ? " AND company_name = '$param[company_name]'" : '');
$loan_no=$param['loan_no'];
$q = $this->db->query("select Count(0) as num_rows
from contact_new
inner join companies c on contact_new.company_id = c.id
inner join history on contact_new.id = history.receiver_email
inner join escalation_level on contact_new.escalation_level_id = escalation_level.id
inner join departments on contact_new.departmend_id = departments.id
WHERE loan_no= '$loan_no' $company_name")->result();
return $q[0]->num_rows;
}

Categories