Inner Join in Codeigniter with multiple table - codeigniter

I wants to create a display function in crudodel of codeigniter to display records on Invoice no 13. I'd run the bellow Query on sql. Its working as per my requirement. Now I've to convert this Query in codeigniter using Inner Join.
SELECT b1.Invoice_No,
cust_name,
cust_address,
cust_contact,
Item_name,
Item_qty,
Item_amount
FROM tbl_bill_invoice AS b1
INNER JOIN tbl_billmenu AS b2 ON(b2.Invoice_No = b1.Invoice_No)
INNER JOIN tbl_billcustomer AS b3 ON(b3.cust_id = b1.cust_id)
WHERE b1.Invoice_no = 13

Try this
$this->db->select('b1.Invoice_No, cust_name, cust_address, cust_contact, Item_name, Item_qty, Item_amount');
$this->db->from(' tbl_bill_invoice as b1');
$this->db->join('tbl_billmenu as b2', 'b2.Invoice_No = b1.Invoice_No', 'inner');
$this->db->join('tbl_billcustomer as b3', 'b3.cust_id = b1.cust_id', 'inner');
$this->db->where('b1.Invoice_no', 13);
return $this->db->get()->row();

Take look,
$invoice_no = 13;
$this->db->select('b1.Invoice_No, cust_name, cust_address, cust_contact, Item_name, Item_qty, Item_amount');
$this->db->from('tbl_bill_invoice b1');
$this->db->join('tbl_billmenu b2', 'b2.Invoice_No = b1.Invoice_No', 'inner');
$this->db->join('tbl_billcustomer b3', 'b3.cust_id = b1.cust_id', 'inner');
$this->db->where('b1.Invoice_no', $invoice_no);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->row();
} else {
return array();
}

Simply write this
$sql = "SELECT b1.Invoice_No,
cust_name,
cust_address,
cust_contact,
Item_name,
Item_qty,
Item_amount
FROM tbl_bill_invoice AS b1
INNER JOIN tbl_billmenu AS b2 ON(b2.Invoice_No = b1.Invoice_No)
INNER JOIN tbl_billcustomer AS b3 ON(b3.cust_id = b1.cust_id)
WHERE b1.Invoice_no = 13";
$result = $this->db->query($sql)->result();

Related

how can i use paginate() function with db::select

First of all, I'm interested in how to use the paginate function with a simple query.
for example, I have this query:
$query = "SELECT
dw.id,
w.word,
d.definition,
dw.user_id,
IF(IFNULL(dw.definition_id, 0) = 0,
(SELECT COUNT(phrase_id)
FROM phrase_word wp1 WHERE wp1.user_id = dw.user_id AND wp1.word_id = dw.word_id),
(SELECT COUNT(phrase_id)
FROM phrase_word wp2 WHERE wp2.user_id = dw.user_id AND wp2.word_id = dw.word_id AND wp2.definition_id = dw.definition_id)
) phrases_count
FROM definition_word AS dw
INNER JOIN words AS w ON w.id = dw.word_id
LEFT JOIN definitions AS d ON d.id = dw.definition_id
WHERE dw.user_id = 2";
I Can't use the paginate method in this way.
$unknown_words = DB::select($query)->paginate(5);
Or if it's possible how to convert
"IF(IFNULL(dw.definition_id, 0) = 0,
(SELECT COUNT(phrase_id)
FROM phrase_word wp1 WHERE wp1.user_id = dw.user_id AND wp1.word_id = dw.word_id),
(SELECT COUNT(phrase_id)
FROM phrase_word wp2 WHERE wp2.user_id = dw.user_id AND wp2.word_id = dw.word_id AND wp2.definition_id = dw.definition_id)
) phrases_count";
this piece of the query into a query builder.
I think you have a better option you can use laravel joins select and table method and you could achieve your desire output as below.
$unknown_words = DB::table('definition_word as dw')
->join('words as w', 'w.id', '=', 'dw.word_id')
->leftJoin('definitions as d', 'd.id', '=', 'dw.definition_id')
->select(
'dw.id',
'w.word',
'd.definition',
'dw.user_id',
DB::raw('IF(IFNULL(dw.definition_id, 0) = 0,
(SELECT COUNT(phrase_id)
FROM phrase_word wp1 WHERE wp1.user_id = dw.user_id AND wp1.word_id = dw.word_id),
(SELECT COUNT(phrase_id)
FROM phrase_word wp2 WHERE wp2.user_id = dw.user_id AND wp2.word_id = dw.word_id AND wp2.definition_id = dw.definition_id)
) as phrases_count')
)->where('dw.user_id', 2)->paginate(15)

Codeigniter count_all_results() and result() with group_by, join, where vs

I want to do query both result and count with group_by.
$id= 2;
$limit = 10;
$offset = ($id - 1) * $limit;
$this->db->group_by('BE.etiket_id');
$this->db->from('bulten_icerigi_etiketler as BE');
$this->db->join('etiketler as E', 'BE.etiket_id=E.id', 'left');
$this->db->join('bulten_icerigi as BI', 'BE.bulten_id=BI.id', 'left');
$count = $this->db->count_all_results('', false);
$this->db->limit($limit, $offset);
$this->db->order_by('E.adi');
$results = $this->db->get()->result();
However, I have error
Error Number: 1054
Unknown column 'tbl_`E.adi' in 'field list'
Can I use count_all_results() and result() together ?
Thank you.
You can't group by columns which you didn't selected in select.
Try to use E.adi and E.id instead of BE.etiket_id in your group by.
Your query should looks like:
SELECT
COUNT(*) as count,
tbl_E.adi,
tbl_E.id
FROM tbl_bulten_icerigi_etiketler as BE
LEFT JOIN tbl_etiketler as E ON BE.etiket_id=E.id
LEFT JOIN tbl_bulten_icerigi as BI ON BE.bulten_id=BI.id
GROUP BY tbl_E.adi, tbl_E.id // here you need to change
ORDER BY tbl_E.adi
$this->db->group_by(array("E.adi", "E.id"));

Doctrine subquery syntax

I need to reproduce with doctrine a AND statement from an sql query
AND ((SELECT e2.candidature_id_ca FROM evaluateur_candidature e2 WHERE
e.candidature_id_ca = e2.candidature_id_ca AND e2.role_evaluateur =
'evaluateur' ) IS NULL )
It's so easy to reproduce this with doctrine i tried :
$query->andWhere('(SELECT e2.candidature_id_ca FROM evaluateur_candidature e2 WHERE
e.candidature_id_ca = e2.candidature_id_ca AND e2.role_evaluateur = ?)' , 'evaluateur' , 'IS NULL ');
But i get
Couldn't find class e2
Someone know how i could do it ? thanks in advance
UPDATE here the full doctrine query :
$query = Doctrine_Query::create()
->from('Candidature c')
->innerJoin('c.RoleActeur ra_cand ON c.id_ca = ra_cand.id_ca AND ra_cand.id_role = 4')
->innerJoin('ra_cand.Acteur ac_cand')
->innerJoin('c.CandidatureActeur ca ON c.id_ca = ca.id_ca')
->where('c.id_stca = 5');
$query->leftJoin('c.EvaluateurCandidature ec');
//here i would like to get the "Candidature collection" who not have records in the table EvaluateurCandidature when the column role_evaluateur is set to "evaluation".
//$query->andWhere('ec.role_evaluateur = ? ) ? )', 'evaluateur' ,'IS NULL ');
$query->andWhere('(SELECT e2.candidature_id_ca FROM evaluateur_candidature e2 WHERE
e.candidature_id_ca = e2.candidature_id_ca AND e2.role_evaluateur = ?)' , 'evaluateur' , 'IS NULL ');
As per your description you could include another condition on join EvaluateurCandidature to join only records where role_evaluateur is set to "evaluation" after that select only records where id of EvaluateurCandidature is null
$query = Doctrine_Query::create()
->from('Candidature c')
->innerJoin('c.RoleActeur ra_cand ON c.id_ca = ra_cand.id_ca AND ra_cand.id_role = 4')
->innerJoin('ra_cand.Acteur ac_cand')
->innerJoin('c.CandidatureActeur ca ON c.id_ca = ca.id_ca')
->leftJoin("c.EvaluateurCandidature ec WITH ec.role_evaluateur = 'evaluation'")
->where('c.id_stca = 5')
->andWhere('ec.id IS NULL');
WITH keyword

Laravel use result of a table as a join in another select

I would like to get in laravel 5.2 the same result as this from mysql:
select a3.user_id,sum(a3.task_hour)
from activities a3
where id not in
( select a1.id from activities a1 inner join
(SELECT * FROM `activities` where validated = 1) a2
on a1.user_id = a2.user_id and a1.project_id = a2.project_id where a1.validated = 0)
group by a3.user_id
So in order to get clear view on the code, I try to get the following first:
(SELECT * FROM `activities` where validated = 1) a2
with this
$activity_onlyFromOTL = DB::table('activities AS a2');
$activity_onlyFromOTL->select('a2.id','a2.year','a2.month','a2.user_id','a2.project_id','a2.task_hour')->where('a2.from_otl','=','1');
For the second part, I have this:
( select a1.id from activities a1 inner join
(SELECT * FROM `activities` where validated = 1) a2
on a1.user_id = a2.user_id and a1.project_id = a2.project_id where a1.validated = 0)
For clarity, I tried to use the first variable into this second statement so I can read it better:
$activity_whereOTLandNonOTL = DB::table('activities AS a1');
$activity_whereOTLandNonOTL->select('a1.id');
$activity_whereOTLandNonOTL->join($activity_onlyFromOTL, function ($join) {
$join->on('a1.user_id', '=', 'a2.user_id')->on('a1.project_id', '=', 'a2.project_id');
});
$activity_whereOTLandNonOTL->where('a1.validated', '=' , '0');
But when I try to get the toSql for that one, I get the error:
ErrorException in Grammar.php line 39:
Object of class Illuminate\Database\Query\Builder could not be converted to string
How can I work this way so that it is a much more clear code?

left join with ActiveRecord (yii2)

I tried to send SQL request with LEFT JOIN but it doesn't display data from table2 table.
public static function top($limit)
{
return self::findBySql("
SELECT * FROM table 1 g1
LEFT JOIN table2 s1
ON (g1.id = s1.g_id AND s1.id = (
SELECT MAX(id)
FROM table2 s2 WHERE s2.g_id = g1.id
))
LIMIT :limit",
[':limit' => $limit]
)->all();
}
It seems you are adding this function to the model and self represents the model itself.
Yii will not return results from another table and will be limited to the model only if you are calling the find on a model, instead you need to use a db query as below:
$query = new \yii\db\Query;
$query->select('*')
->from('table 1 g1')
->leftJoin('table2 s1', 's1.g_id AND s1.id = (SELECT MAX(id) FROM table2 s2 WHERE s2.g_id = g1.id')
->limit($Limit);
$command = $query->createCommand();
$resp = $command->queryAll();
The correct SQL query is
SELECT * FROM table 1 g1
LEFT JOIN table2 s1
ON g1.some_field = s1.some_field
where g1.some_field = s1.some_field are the fields that define the join.
I have working code something like...:)
with user and user_friend_list
$query = new Query;
$query ->select(['user.id AS user_id', 'user_friend_list.id'])
->from('user')
->innerJoin('user_friend_list', 'user.email = user_friend_list.email');
$command = $query->createCommand();
$data = $command->queryAll();
foreach($data as $datakey)
{
//echo $datakey['id'];
$friendfind = UserFriendList::findOne($datakey['id']);
$friendfind->is_app_using_user_id=$datakey['user_id'];
$friendfind->save();
}

Resources