At two different tables,
The new value of [chargeINFO.u_chargewait]
I want to add and save the existing [userinfo.u_charged] value.
After the query operation, I want the value of [u_charged] to be saved as 150000.
I can't find it even if I search for the query statement.
Please help me. Thank you.
$query = "UPDATE userinfo
INNER JOIN chargeINFO ON (userinfo.u_id = chargeINFO.u_id)
SET userinfo.u_charged = chargeINFO.u_chargewait";
In order to get the result 150000 from the original u_charged = 50000 and u_chargewait = 100000, you need to add the two columns together, rather than just assigning one to the other. Then you can assign the result back to the column to update it.
UPDATE userinfo
INNER JOIN chargeINFO ON (userinfo.u_id = chargeINFO.u_id)
SET userinfo.u_charged = userinfo.u_charged + chargeINFO.u_chargewait
Related
Here I want to access two table field but I cant get success. Here is my little code. please check that. I want to access Analysis.Right and tabl3.right.
I am printing its with foreach loop. like $res['Right'] of Analysis and $res['right'] of tabl3. when I try to print this it's show me error
Undefind index Right.
any one can help me
$qry = "select Analysis.Q_Id, tabl3.Q_Id, Analysis.Right, tabl3.right from tabl3 INNER JOIN Analysis ON Analysis.Q_Id = tabl3.Q_Id where Analysis.Q_Id = 3";
please help..
you have tow column with right name related to different table so there is not a column name right but 'Analysis.Right ' or 'tabl3.right'
or you can assign an alias for set the column name equalt to Right where you need .. eg:
$qry = "select
Analysis.Q_Id
, tabl3.Q_Id
, Analysis.Right as Right
, tabl3.right as Right_t3
from tabl3
INNER JOIN Analysis ON Analysis.Q_Id = tabl3.Q_Id where Analysis.Q_Id = 3";
Your result set has columns with the same name. Give them different names:
select t3.Q_Id, a.Right as a_right, t3.right as t3_right
from tabl3 t3 inner join
Analysis a
on a.Q_Id = t3.Q_Id
where a.Q_Id = 3;
When you look for the names in your code, look for a_right and t3_right.
Note that you don't need to return Q_Id twice. The ON clause guarantees that the values are the same.
I want to display all data from the function below, however whenever the function is called, MySQL only returns one row if there are duplicates. What is the problem with my SQL Query?
$sql_response1 = "SELECT *
FROM ".TABLE_PREFIX."tests_answers A, ".TABLE_PREFIX."tests_results R, ".TABLE_PREFIX."members M
WHERE A.question_id=".$q_id." AND R.result_id=A.result_id AND R.final_score<>'' AND R.test_id=".$_GET['tid']." AND M.member_id =A.member_id AND M.member_id IN ($in_text)
GROUP BY A.answer ORDER BY A.answer";
$result_responsed = mysql_query($sql_response1, $db);
while ($row_answer = mysql_fetch_array($result_responsed)) {
$x++;
$response_member = $row_answer['first_name'];
$response_answer = $row_answer['answer'];
$response_department = $row_answer['department_name'];
$x--;
$objPHPExcel->getActiveSheet()->setCellValue(A.$x, $response_answer.' '.'('.$response_member.' - '.$response_department.')');
$x++;
}
Let's say that the column result_id has two instances of the value foo, only one row will be returned. How do I show ALL results without MySQL truncating duplicate rows?
Your problem is that GROUP BY groups together - basically, collapses - all rows with the specified value in common. In this case, that means it will show only one row with a given value for A.answer.
You're using the GROUP BY query which will return only one result and it will remove duplicates. If you're trying to sort the results use ORDER BY without using GROUP BY as well.
$sql_response1 = "SELECT *
FROM ".TABLE_PREFIX."tests_answers A, ".TABLE_PREFIX."tests_results R, ".TABLE_PREFIX."members M
WHERE A.question_id=".$q_id." AND R.result_id=A.result_id AND R.final_score<>'' AND R.test_id=".$_GET['tid']." AND M.member_id =A.member_id AND M.member_id IN ($in_text)
ORDER BY A.answer";
For more background information on the GROUP BY function check out the informative page on W3Schools
I have two tables 'accounts_transactions' and 'accounts_bills_transactions'.
I have to left join these two using active record of codeigniter.But the names of key columns used to join are different.So I am not getting the key column from the left table in the output .What query should I write to get the key column from the left table included in the result.
My code is
$this->db->select('*');
$this->db->from('accounts_transactions');
$this->db->join('accounts_bills_transactions', 'accounts_transactions.id = accounts_bills_transactions.transaction_id','left');
$query = $this->db->get();
So, as you see the key columns used to join here are , id from left table and transaction_id from second table.The problem is that I am not getting the id from left table in the result.But I am getting all other columns.I assume the problem is because of difference in column names used to join.ie both the column names are not named 'id' .So how can I get the id from left table included in the result.
You could alias them:
$this->db->select('accounts_transatctions.*, account_transactions.id AS a_id,
accounts_bills_transactions.*,
account_bills_transactions.id AS ab_id');
$this->db->from('accounts_transactions');
$this->db->join('accounts_bills_transactions', 'accounts_transactions.id = accounts_transactions.transaction_id','left');
$query = $this->db->get();
The two IDs will now be available as a_id and ab_id (or whatever alias you choose)
Note: I'm not sure if you can alias in AR without avoiding escaping (haven't been using CI for a while). Should you get any error for that reason, just pass false as second parameter of $this->db->select():
$this->db->select('...', false);
you can try this if you confuse of using $this->where or $this->join
$query = $this->db->query("select ......");
return $query;
You problem is so simple. You can use this query
$query = $this->db
->select('at.*')
->select('abt.id as abt_id');
->from('accounts_transactions at');
->join('accounts_bills_transactions abt', 'at.id = abt.transaction_id','left');
->get()
->result();
When same column are used in join it selects only one. You need to give alise to the other column in second table. The best practice is to use a structure like this
accounts_transatctions
--------------------------
accounts_transatctions_id
other_columns
accounts_bills_transactions
---------------------------
accounts_bills_transactions_id
accounts_transatctions_id
other_columns
I have one user table with column name id,father_email,mother_email,email_notification and many more, now i want to update email_notification column with
father_email,mother_email I did it with multiple queries but i want to know that how to do it with single query only so it saves the execution time.
my code are as follows:
<?php
$qry="SELECT id,CONCAT(father_email,',',mother_email) as notify FROM user";
$query=mysql_query($qry);
while($row=mysql_fetch_assoc($query))
{
$qry2="UPDATE user SET email_notification='".$row['notify']."' WHERE id=".$row['id']."";
mysql_query($qry2);
}
?>
its working fine but i want to know how to do it with single query
This will update all email_notification columns for all users:
UPDATE user
SET email_notification = CONCAT_WS(',', father_email, mother_email)
(i think it's better to use CONCAT_WS that will skip null values in father_email or in mother_email)
This will replace all of them
UPDATE user AS u
INNER JOIN user AS ul ON ul.id = u.id
SET u.email_notification = CONCAT(ul.father_email,',',ul.mother_email)
UPDATE user
SET email_notification = CONCAT_WS(',', father_email, mother_email)
I'm trying to use the following query syntax from a php file:
$sql = "UPDATE properties SET properties.ht_hs = 3.5 WHERE properties.oil_data_id = acea.oil_data_id AND (acea.ACEA_A3 = 1 OR acea.ACEA_B3 = 1 OR acea.ACEA_B4 = 1) AND properties.ht_hs < 3.5";
$result = mysql_query($sql) or die(mysql_error());
However, It's not doing what I want. I have two tables in my database, and I need to change the value of some of the records for one column within one of the tables (the ht_hs column/field within the properties table). However,the criteria for when to change that field is dependent upon both tables.
Each motor oil in the database has an id, which is listed in the "oil_data_id" column of each table.
I'm trying to find oils that meet the ACEA A3 or B3 or B4 spec (ie, they have a "1" in that column of the acea table) which also have a value of less than 3.5 in the ht_hs column of the properties table.
If they do, I want to update the value to 3.5.
How can I restructure my query so that it works?
I think you're looking for something like this:
UPDATE properties
SET properties.ht_hs = 3.5
WHERE properties.oil_data_id in
(select acea.oil_data_id
from acea
where (acea.ACEA_A3 = 1 OR acea.ACEA_B3 = 1 OR acea.ACEA_B4 = 1))
AND properties.ht_hs < 3.5;
You would need to include table acea in the JOIN like :-
UPDATE properties, acea
SET ...;
See the documentation
UPDATE items,month SET items.price=month.price
WHERE items.id=month.id;