Update Codeigniter's data on Table #2 based Table #1's id - codeigniter

I have 2 tables, reservation and is customer
table #1 : reservation
------------------------
- reservation_id
- reservation_status
- customer_id
table #2 : customer
------------------------
- customer_id
- customer_name
- customer_status
I'm new in codeigniter but how I can update/change customer status (from 0 to 1 or 1 to somevalue) based on reservation id
is there using join and update or somewhat.
Thanks

Try this query:
$sql = 'UPDATE customer AS C JOIN reservation AS R ON R.customer_id=C.customer_id
SET C.customer_status = (CASE C.customer_status WHEN 1 THEN 0 ELSE 1 END)
WHERE R.reservation_id="2"';
$result = $this->db->query($sql);
if($result) {
echo "Success";
} else {
echo "fail";
}
And check your query by:
echo $this->db->last_query();

try this code
UPDATE customer
JOIN reservation
ON customer.customer_id = reservation.customer_id
SET customer.customer_status = your_value;
WHERE reservation.reservation_id="your_id"';

Related

LINQ join Table on Variable

I have this query in LINQ which I aggregate on the ID and sales:
var resultsales = (from so in _salesRepo.QueryNoTracking
group so by so.Id into TotaledOrders
select new Example
{
Id = TotaledOrders.Key.ToString(),
TotaledValue = TotaledOrders.Sum(s => s.TotalCost).ToString(),
Orders = TotaledOrders.ToString()
}).ToList();
Now I have this table (customerinfo) that I want to join:
ID Name Address
---------------
1 .... .....
2 .... .....
What's the correct way to outer join with my new table stored as a variable?

SQL | get query from 2 table with condition

I have 2 tables, I want to display all user in list in my web, and show his last login details from table tbl_login_details but get the last login details for this user
1- diplay all my user from table - tbl_user
2- display LAST login for all user so the data of tbl_login_details must be ORDER then take THE LIMT
---------tbl_user-------
id user_id user_name
1 5 mohammed
2 7 ahmed
-------------tbl_login_details----------
id user_id last_activity
1 7 2016-5-2
2 7 2017-4-2
3 7 20-17-8-4
My Work is
$this->db->select('m.*,u.*');
$this->db->from('tbl_user m');
//$this->db->where("m.user_id", $id);
$this->db->join('tbl_login_details u ', 'u.user_id = m.user_id');
$where = 'm.user_id = u.user_id OR u.user_id = m.user_id OR m.user_id=" " ORDER last_activity';
$where = $this->db->order_by("last_activity", "desc");
$this->db->where($where);
// $this->db->limit('1');
$this->db->group_by('m.user_id');// add group_by
$query = $this->db->get();
return $query->result();
use max aggregation to find the last activity:
select userid, max(last_activity)
from tbl_user a inner join tbl_login_details b on a.userid=b.userid
are you try to get the last detail login from each user, If yes you can using ORDER DESC AND LIMIT 1
create query for display tbl_user first
SELECT * FROM tbl_user ORDER BY id ASC
then you can create query for get last user login
SELECT * FROM tbl_login_details WHERE user_id = 7 ORDER BY id DESC LIMIT 1
you can using modal pop up for display detail login.
or are you try display like this ?
id user_id user_name last_activity
1 5 mohammed 20-17-8-4
2 7 ahmed 20-17-8-4
help into CI max aggregation to find the last activity:
$query = $this->db->query("select userid, max(last_activity) from tbl_user a inner join tbl_login_details b on a.userid=b.userid");
foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->body;
}
Here is the simple SQL code to fetch that. You can do it CodeIgniter's way:
Implement this SQL into CodeIgniter.
SELECT tbl_user.user_id, tbl_user.user_name, tbl_login_details.user_id, tbl_login_details.last_activity FROM tbl_user, tbl_login_details WHERE tbl_user.user_id = tbl_login_details.user_id ORDER BY tbl_login_details.last_activity DESC
May be you are searching for Following
SELECT user_id, user_name, (SELECT last_activity FROM tbl_login_details WHERE tbl_login_details.user_id = tbl_user.user_id ORDER BY last_activity DESC LIMIT 1) as last_activity FROM tbl_user
updated code. search for how to write this sub query in active record. or just use custom query like $query = $this->db->query("YOUR QUERY");
Hope this can help you
SELECT u.id,u.user_id,u.user_name,MAX(l.last_activity) AS last_activity
FROM tbl_user u
LEFT JOIN tbl_login_details l ON(u.user_id=l.user_id)
GROUP BY u.user_id
on codeigniter custom query builder
$query = $this->db->query("SELECT u.id,u.user_id,u.user_name,MAX(l.last_activity) AS last_activity FROM tbl_user u LEFT JOIN tbl_login_details l ON(u.user_id=l.user_id) GROUP BY u.user_id");
foreach ($query->result() as $row)
{
echo $row->id;
echo $row->user_id;
echo $row->user_name;
echo $row->last_activity ;
}

How can i manage to loop the group_concat value into datatable and checkboxes

i am storing id_supplier in product table. I able to display it in table by just one row for each item code with it's supplier id. I also need the name of supplier as well, but i face problem on separating id and name.
//query
select code, group_concat(s.supplier_id,s.supplier_name) AS supplier_id_name
from catalog c
left join product p on p.product_id= c.product_id
left join supplier s on s.supplier_id = p.product_id
group by code;
//query will show following output
id supplier - 1, name - abc
id supplier - 2, name - def
Code | Supplier
A001 | 1abc,2def // id and name of supplier
What i am expected
Code | Supplier
A001 | checkboxes value = 1 and then display supplier name beside....... and continue in the same row if more supplier
There will be a checkbox for every supplier display for the code and checkbox will contain the supplier id for storing purpose.
How can i do this?
There are 2 solutions.
Solution 1: Compose HTML code just by MySQL query itself, by using following sql:
// Query
SELECT CODE,
GROUP_CONCAT(CONCAT('<input type="checkbox" value="', s.supplier_id, '"> ', s.supplier_name) SEPARATOR ',') AS supplier_id_names
FROM catalog c
LEFT JOIN product p ON p.product_id= c.product_id
LEFT JOIN supplier s ON s.supplier_id = p.product_id
GROUP BY CODE;
Solution 2: First grab supplier id and name separated by specific char, and then in backend code, filter each id and name by splitting it, to produce the full HTML code: (Following is PHP code)
$query= "SELECT CODE,
GROUP_CONCAT(CONCAT(s.supplier_id, ':', s.supplier_name) SEPARATOR '|||') AS supplier_id_names
FROM catalog c
LEFT JOIN product p ON p.product_id= c.product_id
LEFT JOIN supplier s ON s.supplier_id = p.product_id
GROUP BY CODE";
$result = mysqli_query($dbc,$query);
while($row = mysqli_fetch_array($result)){
// get html for each row
$checkboxHtml = '';
$idSups = explode('|||', $row['supplier_id_names']);
foreach($idSups AS $s) {
list($i,$s) = explode(':', $s);
$checkboxHtml .= '<input type="checkbox" value="' . $i . '"> ' . $s;
}
// do something else...
}

Linq - Inner join with group with sum

I have 2 tables, first table if transactions
transaction table:
Id LoanId
1 100
The second table I have is a TransactionLeg table
transactionleg table:
ID TransactionId GLAmount
1 1 200
2 1 200
I would like to join the two on the TransactionId column, group by the loanID and sum the GLAmount
So it should produce the following:
LoanId TotalGlAmount
100 400
var investmentsWritten = from transaction in ctx.Transactions
join transactionleg in ctx.TransLegs on transaction.Id equals transactionleg.TransactionId
where
transaction.Class == Transaction.TransactionClasses.WriteOff &&
transaction.Created >= StartDate.Date && transaction.Created <= EndDate.Date
group transaction by transaction.LoanId
into g
select new { Id = g.Key, Value = _____ };
I was wondering what goes where the underline is, the value is transactionleg.GLAmount. I tried g.Sum(x => x.GLAmount) but GLAmount is not recognized, it is highlighted in red stating Cannot resolve symbol GLAmount
It is because GLAmount is in transactionleg not in transaction,
Modify your group by like this:
group new
{
transaction,
transactionleg
}
by new
{
transaction.LoanId
}
and now in Select:
select new
{
Id = g.Key,
Value = g.Sum(x=>x.transactionleg.GLAmount)
}

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