Replace two rows in Mysql - php

How I can replace everything from the base_amount and amount with the content of the base_real_amount in phpmyadmin.
Thanks

I'm not sure how to do this in phpmyadmin so I apolgoize if this doesn't answer your question. To do this with just SQL:
UPDATE `table_name` SET base_amount=base_real_amount, amount=base_real_amount;

Those aren't rows - they are columns. You could use a simple update statement to assign the value from one to the other:
UPDATE mytable
SET base_real_amount = base_amount

Related

How to use two insert queries in same function in php

I have two insert queries in one table where two columns I am inserting from other table and one is I am inserting by own but in not working. Please help me.
INSERT INTO `g8_list_exec`(`g8_task_id`,`g8_tmpl_id`,`g8_chk_id`)
SELECT `g8_rec_id`,`g8_tmpl_uid` FROM `g8_list_add_task`
WHERE g8_tmpl_uid = 'oe_507','ik_862'
AND
INSERT INTO `g8_list_exec` (`g8_chk_id`) VALUES ('ik_862')
Try this
INSERT INTO g8_list_exec (g8_task_id,g8_tmpl_id,g8_chk_id)
SELECT g8_rec_id,g8_tmpl_uid,'ik_862'
FROM g8_list_add_task WHERE g8_tmpl_uid = 'oe_507';
use below query it will work for you
INSERT INTO g8_list_exec(g8_task_id,g8_tmpl_id,g8_chk_id) SELECT g8_rec_id,g8_tmpl_uid,'ik_862' FROM g8_list_add_task WHERE g8_tmpl_uid = e_507','ik_862'
value which you want to insert manually place in select query only after all columns.

update with max value of the column is not working in mysql

I have tried to set the max value for the particular column but that is not working for me. I do not know where i'm going wrong.
UPDATE `upload_video`
SET order_id ='select max(order_id)+1
FROM upload_video'
WHERE `video_id` = 22
This is my query i run the select max(order_id)+1 from upload_video query separately which is giving the result. But if i use this query in update query, the query is executing without error. But the order_id is not updating properly. please help me
Your query is almost correct in standard SQL, you only need to use brackets () instead of apostrophe ':
SET order_id = (SELECT MAX(...) ...)
but MySQL doesn't allow you to update a table while selecting from the same table, a workaround is to use a subquery that calculates the value that you need, and to join your subquery with the table you need to update:
UPDATE
upload_video JOIN (SELECT COALESCE(MAX(order_id),0)+1 max_id
FROM upload_video) s
SET
upload_video.order_id=s.max_id
WHERE
video_id=22
Please see fiddle here.
You have a typo in the statement, you used UPADTE instead of UPDATE.
One problem is, don't quote the subquery. You have used single quotes, which means the expression select max(order_id)+1... was interpreted as a text literal (a varchar). But you clearly don't want that (I guess order_id is a number). What you want instead is to evaluate the subquery. However, if you try:
UPDATE `upload_video`
SET order_id =(select max(order_id)+1
FROM upload_video)
WHERE `video_id` = 22
then MySQL doesn't allow it (I didn't know about that). Other databases such as PostgreSQL allow it. So you might need two statements:
select #id = coalesce(max(order_id), 0) + 1 FROM upload_video;
UPDATE `upload_video` SET order_id = #id WHERE `video_id` = 22;
Please note this works in MySQL but not in other databases.
Try this:
UPDATE `upload_video`
SET order_id =(select COALESCE(max(U2.order_id),0)+1
FROM upload_video U2)
WHERE `video_id` = 22
Peraphs this query goes in error because MySql doesn't want to use the same table in UPDATE and in subquery.
If your case please write two queries.
The first get the maximum value, the second does update

Finding the sum of integers from multiple mysql rows in same column

Basically, I have a table for users and a column that stores an integer. I want to take all rows from that table and find the sum of them. The only way I can think to do this would be to loop through all the rows and add them together one by one. However, that seems quite inefficient and I'm sure there's a better way.
Anyone know of one?
SELECT sum(COLUMN_NAME) as total FROM TABLE_NAME
SELECT SUM(column) FROM Users;
Should work. Hope it helps!
Also, refer to http://www.w3schools.com/sql/sql_func_sum.asp.
select SUM(col_1) as sum from table_name GROUP BY col_2
SELECT SUM(column_name) as req_value FROM the_req_table;
It will directly give you the sum

What's the easiest way to duplicate a column?

Currently, I'm getting the ID of the column via a select statement and insert all the returned values (except the ID).
Is there an easier way to do it?
I don't know Oracle one bit, but there should be an equivalent to
INSERT INTO TABLENAME select * FROM tablename WHERE id = x

Sql Query not working!

INSERT INTO `test` (`x`, `y`) WHERE `id` = `$id`
VALUES (`$x`, `$y`)
Whats wrong with this query? I run this in a mysql_query() function in a php file.
You can't use a Where clause with an insert. You are either inserting a row or you're not.
If you're trying to update information from the database, use UPDATE instead of INSERT INTO in the query you're running.
You can't use a where clause on an insert. I think you might be wanting an update statement:
update test set
x = $x,
y = $y
where id = $id
When you're inserting a new value in the database, you usually don't have an ID value until after the insert (assuming you're using auto-generated IDs).
You need to remove the "WHERE id=$id" - it has no meaning in an INSERT statement.
So, either
UPDATE test SET x='x', y='$y' WHERE id ='$id';
OR
INSERT INTO test ('x', 'y') VALUES ('$x', '$y');
as stated in other posts - you cannot do an INSERT with a WHERE.
Also note that you must use single quotes (') rather than backticks (`) for values. Backticks are used when referencing column names / field names.
This:
`field` = '$value'
rather than this:
`field` = `$value`
unless you really do want to reference another field. This is sometimes what you want when copying values or matching JOINs and such. But because you have a variable there, I'm assuming you want to use single quotes rather than backticks.

Categories