mysql update query syntax - php

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;

Related

How to add numbers from mysql column to column and save database?

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

MySql Duplicate entries with auto updating context sensitive relations in PHP

Is there any possibility to duplicate specific entries by auto updating context sensitive relations?
Given a table 'table1' like:
My goal is to duplicate all entries with categoryId 42 while updating parentId if neccessary:
id is an auto incremented column and parentId is used to identify relations between the entries.
Currently I'm inserting them one by one, selecting the old data and managing the logic for the parentId in PHP.
//Thats simplified what I do ($conn is a Zend_Db_Adapter_Abstract)
$rows = $conn->fetchAll("SELECT * FROM table1 WHERE categoryId = 42 ORDER BY parentId ASC");
$newIds = [];
foreach($rows as $row){
if(array_key_exists($row['parentId'],$newIds))
$row['parentId'] = $newIds[$row['parentId']];
else
$row['parentId'] = null;
$conn->query("INSERT INTO table1 (parentId,info,categoryId) VALUES (?,?,?)",[$row['parentId'],$row['info'],$row['categoryId']]);
$newId = $conn->lastInsertId('table1');
$newIds[$row['id']] = $newId;
}
I'm stuck with this because I need the lastInsertedId of the new element to set the new parentId for the next one.
But I'm experiencing this to be pretty slow (in relation to one single query which contains the logic).
Is there any possibility to give a query some kind of incremental element sensitive logic? Or have you any suggestions on how to fasten this up?
Any help appreciated! :)
You are not showing any code. I guess you use mysqli_insert_id() to get the last inserted ID. Maybe you can do something with MAX(ID)+1.
Something like:
INSERT INTO table1
SELECT MAX(ID)+1,
info,
categoryId
FROM
table1
WHERE .............. -- the conditions to retreive the parent

in PHP pdo how can i use an incremental identifier

I have the following challenge:
a "Tasks" table:
tasksId int
listId int
taskOrder float
in case i want to move all the tasks from list 2 to list 3 i would do something like:
// pseodo code //
#lastTaskOrder = last task order in list 3
loop - {
UPDATE tasks SET taskOrder = #lastTaskOrder + 1, listId = 3 WHERE listId = 2;
#lastTaskOrder++
}
thus the taskOrder stays unique.
in case i want to move all the tasks from list 2 to the beginning of list 3 i would do something like:
// pseodo code //
#firstTaskOrder = first task order in list 3
#delta = #firstTaskOrder / #numberOfTasksToMove
UPDATE tasks SET taskOrder = #firstTaskOrder + #delta, listId = 3 WHERE listId = 2;
#firstTaskOrder = #firstTaskOrder + #delta
is it possible with mySQL + PDO + PHP?
Short answer: yes.
Longer answer involves some code. To update your list_ids and increment them based on the highest current value in the old list, I had to use a subquery with a window function:
UPDATE tasks
SET list_id = :toList,
task_order =
(SELECT MAX(task_order) from tasks where list_id = :toList)
+ t2.task_sort_order
FROM ( SELECT task_id,
row_number() OVER (PARTITION BY list_id order by task_order)
AS task_sort_order
FROM tasks ) t2
WHERE tasks.task_id = t2.task_id AND tasks.list_id = :fromList
Edit This is heavily edited from the first version. I've thrown away all the PHP in favor of just showing the SQL. I changed the column names because my version of Postgres was complaining about the camel-case names.
this proved to be easier than i thought it would be.
It took me 2 hours but i got it figured out:
SET #start=100;
SET #delta=1.5;
UPDATE tasks SET taskOrder = #start:= (#start+#delta), listId = 3
WHERE listId=2
ORDER BY taskOrder
I PDOed this query with the correct values

Mysql Query In codeigniter

This is my mysql query
$acountry = 1;
$this->db->where_in('varcountry', $acountry);
$val = $this->db->get('tblagencies')->result();
In database table the varcountry filed is stored like this 1,2,3,4 its type is varchar.Each row in table have multiple countries that is the reason to use varchar datatype.
Here i want to select table rows which have $acountry value in the filed varcountry.
How can i do that?The above code is it correct?
You have choosen a wrong data type for storing a comma separated value 1,2,3,4 into varchar,
you should chose a data-type of set, or normalize into a separate table, like :-
create table country (id, name ...);
create table agencies_country ( agency_id, country_id);
insert into agencies_country (agency_id, country_id)
values (x,1), (x,2), (x,3), (x,4);
// meaning 1,2,3,4 = 4 rows
// grabbing result using inner join
Using set is easier, but common practice is to normalize the data (which require some understanding).
I don't like the active record in codeigniter,
is easy to use (not doubt with this),
but it dis-allowed lost of flexibility
Personally I like the construct my own query,
provided you have the understanding of the table schema (which you have to anyway)
use this query..
$search_field = array('varcountry'=>$acountry)
$result = $this->db->get_where('tblagencies' , $search_field );
but in codeignator you can use your own queries like
$sql = "select * from tblagencies where varcountry like '%acountry%'";
$result = $this->db->query($sql);

find value of row out of 3 columns in php and mysql

How can i find 3 in row id#1 with 4 columns in php
id, Content_type1, Content_type2, Content_type3
1 6 3 9
You should restate your question with some context included. Not sure if you're looking for values in that column, or only if the value is 3?
$sql = "SELECT Content_type2 FROM [yourTableHere] WHERE Content_type2 = 3
mysql_query($sql);
That query will get that exact value. If you want to get that value for all records, remove the WHERE clause.
I'll have to say:
$sql = 'SELECT Content_type2 FROM some_table';
$rs = mysql_query($sql);
$row = mysql_fetch_assoc($rs);
echo $row['Content_type2'];
This will fetch the value from Content_type2 assuming there's exactly one row in that table. If there's more than one row or if there's some other logic you want to apply in order to determine what value to use, please clarify.
If you're looking to just return a row that has a value your looking for, then you can use something like the following for your SQL satement:
SELECT * FROM [TABLE] WHERE Content_type1 = [SEARCH] OR Content_type2 = [SEARCH] OR Content_type3 = [SEARCH]
Where [TABLE] is your table name and [SEARCH] is the value you're looking for.
If you want to know which specific column had the value, you'll have to sift through the returned rows to figure that out.

Categories