How to update on union - php

I have a query that does a union on 2 tables. I want to update a column of the result.
something like this:
select * from(
select a.*,'10' as srv from px_conversions_srv10 a
union all
select b.*,'12' as srv from px_conversions_srv12 b
) as ff where ff.adv_transaction_id in(1333764016);
update ff SET ff.`status`=8;
Thanks

Just run two updates:
update px_conversions_srv10
set status = 8
where adv_transaction_id in (1333764016);
update px_conversions_srv12
set status = 8
where adv_transaction_id in (1333764016);
You can run these inside a single transaction if you want them to take effect at exactly the same time.
Note: having multiple tables with the same columns is usually a sign of a poor database design. There are reasons why this might be useful (say, the tables have different replication requirements or different security requirements). But, in general, a single table is a better idea.

Since it is coming from two different tables you need to find out from which table the result comes. You can do this by adding a column to the query and later decide from the column value which table to update. You do this already with the srv column!
The update statement must be on the original table, since the union is only produced by the query. It is not a physical table in the database.
By extension of this logic, to answer the question in the title, you CANNOT execute an UPDATE on the result set of a SELECT query.

Maybe create a view table and then update it:
CREATE VIEW ff AS
select * from(
select a.*,'10' as srv from px_conversions_srv10 a
union all
select b.*,'12' as srv from px_conversions_srv12 b
) as ff where ff.adv_transaction_id in(1333764016);
update ff SET ff.`status`=8;

Related

SQL select all files where a value in table A is the same as in table B (same database)

I'm building a sales system for a company, but I'm stuck with the following issue.
Every day I load .XML productfeed into a database called items. The rows in the productfeed are never in the same order, so sometimes the row with Referentie = 380083 is at the very top, and the other day that very same row is at the very bottum.
I also have to get all the instock value, but when I run the following query
SELECT `instock` FROM SomeTable WHERE `id` > 0
I get all values, but not in the same order as in the other table.
So I have to get the instock value of all rows where referentie in table A is the same as it is in table B.
I already have this query:
select * from `16-11-23 wed 09:37` where `referentie` LIKE '4210310AS'
and this query does the right job, but I have like 500 rows in the table.
So I need to find a way to automate the: LIKE '4210310AS' bit, so it selects all 500 values in one go.
Can somebody tell me how that can be done?
I'm not even sure I understand your problem...
Don't take this personally, but you seem to be concerned/confused by the ordering of the data in the tables which suggests to me your understanding of relational databases and SQL is lacking. I suggest you brush up on the basics.
Can't you just use the following query?
SELECT a.referentie
, b.instock
FROM tableA a
, tableB b
WHERE b.referentie = a.referentie

Comparing huge data from 2 tables

I'm using PHP and under localhost using Wamp. I have 2 tables from my database which has a huge data almost 13,000 plus each table. I want to check if NameFromA from TableA exist in NameFromB from TableB. I have this code working when I try to use small amount of data around 100 data.
SELECT * FROM TableA WHERE EXISTS (SELECT * FROM TableB WHERE NameFromA = NameFromB)
My problem is when I try running it and comparing the 13,000 plus data nothing happens. It has no output.
Create index on column NameFromA in TableA and NameFromB in TableB.
Try below query:
SELECT a.*
FROM TableA AS a, TableB AS b
WHERE a.NameFromA = b.NameFromB
Try this:
SELECT
TableA.*
FROM
TableA
INNER JOIN TableB ON (TableA.NameFromA = TableB.NameFromB)
If it is still slow, may be you have problem in the DB or the timeout time is too short.
You can also try to run this in MySql management too to see how long that query will take.
One more thing. The data that you retreive must be send to web server. If your table contains a lot of data for each row and there are a lot of rows this also will take time.
EDIT:
Ok #mar. You have to make a little troubleshooting to find where is the problem.
First: if your PC is powerfull enought. Because for normal PC and Server 13k records are nothing.
Second: are you sure that there are at least one name from table A which is presented in table B?
Third: try to run query in external SQL tool - not in php. If it retunrs correct set quickly, then the problem is in php.

Shift all data to one table to another table

I have two tables A and B both table are same, if I will insert today some values on table A and I want to all inserted table A data automatic insert on table B after 2 days. for example today is 15 June 2013 and all table A data shifted automatic on 17 June 2013.
Please give me any script.
Thanks in advance.
You'll need to add a timestamp field to your rows in Table A, then you can do this:
insert into b (select * from A where datediff(curdate(), postdate)>=2)
where postdate is your timestamp.
To automate this you can use the mySQL event scheduler (see CREATE EVENT for the syntax) if it's enabled on your server, or use a cron job as suggested elsewhere
insert into tableb select * from tablea;
Full documentation here
Maybe you're looking for something like:
CREATE TABLE tableB LIKE tableA;
As already said the SQL code for this is "insert into b (select * from A)"
to call this automatically you can write a small PHP-Cronjob, add this to the crontab of your server and let it execute every x min/hour/days (what you need)

How do I find which of two tables has the row with a given value in a known column

If I have two tables in a MySQL database that both have a column called order_number, given an order_number value but not knowing which table it comes from how would I go about setting up a query that would return the name of the table it was found in?
I am particularly interested in the name of the table so I can set up subsequent updates to that table.
Also, I am using PHP for the handling of the query.
select "tableA" as tableName,order_number from tableA where order_number=5
UNION
select "tableB" as tableName,order_number from tableB where order_number=5;

See which table a result is in

I took over managing an internal website for the company that I'm working for and I need to get data out of a mysql database. The problem that I'm encountering is that the data is in 6 different tables, all with the same fields but the rows are all unique (the row starts in one table and then gets completely moved to a different table after it is processed by an employee).
Is there an easy way to query against all 6 at once? It would also be useful to be able to retrieve the title of the table it came from.
I'm using PHP to run the query and display it. Would it be better to create another table that defines where all the rows are, have a unique id and then another field for which table it's in?
To complete this query, use union all:
select
'Table1' as TableName,
*
from
Table1
union all
select
'Table2',
*
from
Table2
union all
select
'Table3',
*
from
Table3
...and so on
For better database design, you would want to either have one table with all the rows in it, and a designated Status table where you can link a StatusID column to that says what status that given row is in. A table for each stage in a process is a poor design and will only lead to massive headaches down the road.
If you can't reorganize the tables so that you have just one with all rows and a marker for where in the process they are, I would go for a UNION-approach. Ie:
SELECT 'Data from Table 1', t1.field1, ...
FROM Table1 t1
UNION
SELECT 'Data from Table 2', t2.field1, ...
FROM Table2 t2
UNION
(Table3, 4, 5 and 6 in the same manner)
....
That way you can see where the data is originating from and you get all 6 at once. Just remember that you have to have the exact same field list in all parts of the UNION.
You could create a code generator that generates SQL statements to query the 6 tables. The generator would create a UNION of 6 selects and add a "table" column to each select with a constant value equal to the name of the table queried. That would make writing the statements easy, though I wouldn't say that writing the generator would be.

Categories