Add number to each row in id column? - mysql

I need to add '000' to each row in the id colum of a Wordpress database. Those are the querys that I tried:
UPDATE wp_posts SET ID = 000 + ID
UPDATE wp_posts SET ID = '000' + ID
There is no execution error but this just doesn't modified the column.
How can I do it?

From the comments I can see you want to merge 2 tables, but the ID is the problem for you. Try using union like this
SELECT * FROM table1
UNION ALL
SELECT * FROM table2
Union all should connect the tables even with duplicate values

You can try with using below query but please take a backup for safety before trying this query.
UPDATE wp_posts SET ID = CONCAT('000', ID);
Hope, This will work for you.

Related

MYSQL Update Table set column equal to select column from same table

I have a new column in my database and I need fill it up with the value of the same column from one specific row. I want to create a feature "copy to all"
For example add the same price to all the products taken from the first row:
ID NAME PRICE
1 PROD1 5
2 PROD2 0
3 PROD3 0
4 PROD4 0
I am trying to select the PRICE of the first row (ID 1) and copy it to all the other rows.
I have tried:
UPDATE PRODUCTS SET PRICE = (select PRICE from PRODUCTS where ID = 1);
I want to end up with this
ID NAME PRICE
1 PROD1 5
2 PROD2 5
3 PROD3 5
4 PROD4 5
But I get this error:
Table 'PRODUCTS' is specified twice,
both as a target for 'UPDATE' and as a separate source for data
I tried specifying each table separately
UPDATE PRODUCTS as a SET a.PRICE = (select b.PRICE from PRODUCTS as b where b.ID = 1);
But I get the same error.
Table 'a' is specified twice,
both as a target for 'UPDATE' and as a separate source for data
Maybe I have to create a temporary table and copy from it?
Any hints on how to accomplish this?
Thanks.
You can do it by nesting the select query:
UPDATE PRODUCTS
SET PRICE = (
select PRICE from (select PRICE from PRODUCTS where ID = 1) t
);
See the demo.
Another way to do it, with a self CROSS JOIN:
UPDATE PRODUCTS p CROSS JOIN (
select PRICE from PRODUCTS where ID = 1
) t
SET p.PRICE = t.PRICE;
See the demo.
This wouldn't work logically, as SQL would try to fetch the data it is updating.
Try running your nested statement select PRICE from PRODUCTS where ID = 1 seperately, saving the response and then running your main statement: "UPDATE PRODUCTS SET PRICE = " + newPrice
If this is a SQL script, i suggest you to break it into 2 queries using a variable:
select #var := PRICE from PRODUCTS where ID = 1;
UPDATE PRODUCTS SET PRICE = #var;
Variables are much more easier than temporary table in my opinion.
I've not fully tested, but the syntax should be that
After looking at many answers on other posts and websites, (none of them had the precise answer), I found the solution with this query, and yes we need temp tables:
CREATE TEMPORARY TABLE tmptable SELECT ID, PRICE FROM PRODUCTS WHERE ID = 1;
UPDATE PRODUCTS SET `PRICE` = (select tmptable.`PRICE` from tmptable where tmptable.ID = 1);
BUT! #forpas solution's is really good and works without creating a temp table.
enjoy.
NEW question: is the temp table removed automatically? Leave me a comment.
Cheers

Having multiple statements on where clause with same column name

I have a sample SQL statement that says:
SELECT * from users WHERE id = 2 OR id = 5 OR id = 7
What I would like is to avoid repeating id each time in the where clause. Is there a shortcut for this in MySQL that will allow me to mention the id only once?
Yes, the IN clause
SELECT * from users WHERE id IN(2,5,7);
if these Ids you are using in the comparison come from another table you can even do
SELECT * FROM users WHERE id in (SELECT other_id FROM other_table WHERE somecondition)
e4c5 gave you the answer you needed, but here is something else you can do with IN:
select * from users where 'steve' IN (users.fname, users.lname)

Update Table with DISTINCT SELECTed values from same TABLE

I have this table
What I want to do is that Select the attr_id WHERE a DISTINCT name_en-GB appears and then SET that selected attr_id for that name_en-GB
I can do this by writing individual queries but I want to know is there any way I can do this in one query?
I have tried this
UPDATE sc_product_phrase
SET attr_id = (
SELECT DISTINCT
sc_product_phrase.caption,
sc_product_phrase.design_phrase_id
FROM
`sc_product_phrase` as x
GROUP BY
sc_product_phrase.caption
)
but this show error
[Err] 1093 - You can't specify target table 'sc_product_phrase' for
update in FROM clause
EDITED
I want my TABLE to look like the following http://sqlfiddle.com/#!2/d65eb/1
NOTE:- I dont want to use that query in that fiddle
Your SQL Fiddle makes the question much clearer. You want the minimum attribute id on all rows with the same value in the last column. You can do this with an update/join like this:
UPDATE table1 JOIN
(SELECT `name_en-GB`, min(Attr_Id) as minai
from table1
GROUP BY `name_en-GB`
) tt
on table1.`name_en-GB` = tt.`name_en-GB`
SET attr_id = tt.minai
WHERE table1.`name_en-GB` IN ('Bride Name', 'Child Grade') AND
table1.attr_id <> tt.minai;
I'm not sure if you need the in part. You can update all of them by removing that clause.

Mysql error 1093 - Can't specify target table for update in FROM clause - update column in same table

I know there are a lot of topics on this but I can't figure out how should I rewrite my query to make it work :(
Here my query. It's just should take currency rate from other table and calculate cost
update site_s_client_base_price
SET calculated_price_in_base_currency =
SELECT (site_s_currencies.rate * site_s_client_base_price.supplier_price) from
site_s_currencies, site_s_client_base_price
WHERE site_s_currencies.currency_id=site_s_client_base_price.currency_id
Please, help me with this
You can't seletc and update in the same table because it's locked, use the example above or use
Update TABLE1 t1 set FIELD1= ( select field1 from TABLE1 t2 where .....)
In your case you can fix this by fixing the subquery. You don't need to mention the outer table in the inner from clause. You want a correlated subquery:
update site_s_client_base_price bp
SET calculated_price_in_base_currency =
(SELECT c.rate * bp.supplier_price
FROM site_s_currencies c
WHERE c.currency_id = bp.currency_id
);

Getting error Operand should countain 1 Column(s);

Im trying to get the id ,feename from 1 table id is not in studentfeeTable where invoiceID is 5 but i Got this error. I dont know how to deal with it. please help
select id, Fee_Head_Name from admission_fees_structure Where ID NOT IN (Select * from
student_fee_detail where invoiceID=5) ;
You have used "ID NOT IN (Select * from student_fee_detail where invoiceID=5)".
You should use "ID NOT IN (Select ID from student_fee_detail where invoiceID=5)".
You should compare one column with other. not entire row.
Its better to use exist in this scenario.
select id, Fee_Head_Name
FROM admission_fees_structure outer
Where
exists
(Select 1 from Student_fee_detail inner
where inner.invoiceID=5 and inner.ID = outer.ID) ;
In your Sub query you must select Id not the '*' because you are comparing it with the ID. You cannot compare a single column with the entire row. how would MySQl know that which value in the entire row is to compare with the ID.
it must be like this
Select Id from
student_fee_detail where invoiceID=5