I have a column containing 123 unique Raw Material Names and another column called OutgoingRawMaterial, the idea being that we can look at the table and see that 270 units of Ingredient A left the company etc.
I wanted to create an update query which would update the related OutgoingRawMaterial with the figure from the query 'qrySumManufacturingRawMaterials' which contains the figures I want. The Update Query works fine for individual records, as below:
Field: OutgoingRawMaterial
Table: tblRawMaterialsManufactured
Update to: [tblSumManufacturingRawMaterials Query].[Expr1]
Criteria: [RawMaterial] Like "Raw Material 1*"
The problem is that I want to do the same for all 123 records and don't know how to do this, short of creating 123 queries and running them all from a macro or VBA. I was also going to create a single query with all 123 Raw Materials, using a different 'LIKE' to isolate them, but get the "Duplicate Output Destination 'tblRawMaterialsManufactured.OutgoingRawMaterial'" I can confirm that I only have one column named 'OutgoingRawMaterial'.
UPDATE:
The answer below is how you would normally solve this. The way you're trying to do it by having 2 columns with a bunch of records and a 3rd column with the sum is actually considered a bad design because your third column is not related to the first 2 columns. You'll have tell me more to understand how that third column will work - does it contain 123 sum (aggregate) values while the first 2 columns contains say hundreds of individual values?
The easiest approach would be to delete everything and recreate the table. For example, let's say that 'qrySumManufacturingRawMaterials' outputs something like this:
Raw1 | 1
Raw2 | 2
...
And OutgoingRawMaterial has the same format then you can just do a INSERT INTO SELECT:
DELETE FROM OutgoingRawMaterial;
INSERT INTO OutgoingRawMaterial SELECT 'qrySumManufacturingRawMaterials'
There are many variations on this - for example if the query outputs different column names you can change the query to INSERT INTO OutgoingRawMaterial (Col1, Col2, ...) so if you need more help please update the question.
Related
Struggling with a large dataset in my mariaDB database. I have two tables, where table A contains 57 million rows and table B contains around 500. Table B is a subset of ids related to a column in table A. I want to delete all rows from A which do not have a corresponding ID in table B.
Example table A:
classification_id
Name
20
Mercedes
30
Kawasaki
80
Leitz
70
HP
Example table B:
classification_id
Type
20
car
30
bike
40
bus
50
boat
So in this example the last two rows from table A would be deleted (or a mirror table would be made containing only the first two rows, thats also fine).
I tried to do the second one using an inner join but this query took a few minutes before giving an out of memory exception.
Any suggestions on how to tackle this?
try this:
delete from "table A" where classification_id not in (select classification_id from "table B");
Since you say that the filter table contains a relatively small number of rows, your best bet would be creating a separate table that contains the same columns as the original table A and the rows that match your criteria, then replace the original table and drop it. Also, with this number of IDs you probably want to use WHERE IN () instead of joins - as long as the field you're using there is indexed, it will usually be way faster. Bringing it all together:
CREATE TABLE new_A AS
SELECT A.* FROM A
WHERE classification_id IN (SELECT classification_id FROM B);
RENAME TABLE A TO old_A, new_A to A;
DROP TABLE old_A;
Things to be aware of:
Backup your data! And test the queries thoroughly before running that DROP TABLE. You don't want to lose 57M rows of data because of a random answer at StackOverflow.
If A has any indexes or foreign keys, these won't be copied over - so you'll have to recreate them all manually. I'd recommend running SHOW CREATE TABLE A first and making note on its structure. Alternatively, you may consider creating the table new_A explicitly using the output of SHOW CREATE TABLE A as a template and then performing INSERT INTO new_A SELECT ... instead of CREATE TABLE new_A AS SELECT ... with the same query after this.
I have created a new column in the "destination" table with the same name, datatype and other values as appear in the "source" column in a different table. I have tried many suggested solutions as found on stackoverflow. This one appeared to work (found on Quora) but when I went to the destination table the previously empty column remains empty with nothing but NULL values noted. This is the Quora suggestion:
you can fill data in column from another existing one by using INSERT INTO statement and SELECT statement together like that
INSERT INTO `table1`(column_name)
SELECT column_name FROM `table2`
here you filled a single column in table 1 with data located in a single column in table 2
so if you want to fill the whole table 1 (all columns) with data located in table 2 you can make table 1 like a copy of table 2 by using the same code but without column name
INSERT INTO `table1`
SELECT * FROM `table2`
but note to do this (copy table content to another one) ensure that both of tables have the same column count and data types.
I'm not sure what is meant by column count (do the two table have to have the same number of columns?)
When I run it I get error # 1138.
Any help greatly appreciated. -JG
I have 2 existing tables in a MySql DB. The tables have identical structures. I want to copy data from table to another.
insert into `Table1`
select * from Table2
where department = "engineering"
the above code seemed to work and it copied the data correctly except for 1 column. The "department" column did not copy over so it was blank. All the other fields seemed to copy over correctly for all of the records.
What can be causing this? As I mentioned both tables have identical structures, same number of columns and everything...
Any ideas?
Note:I just realized that there are actually 2 columns that are not copying over. The "department" and "Category" fields come over blank. So basically when I am inserting the data from table 2 into table 1, 12 out of 14 columns are successfully copied over but then there are 2 columns that remain blank.
Below is the DESCRIBE of Table1 and Table2
The only difference I can see when I do a Describe on both tables is that the 2 fields in question have a data type of enum (.....) but they have differences in between the parenthesis. Could this be causing the issue and if so is there a simple way around it? I'm thinking I might have to do an update query after I do the initial insert that will bring in the "department" and "category" fields from table 2 into table 1 by joining in the ID field.
From the docs:
If you insert an invalid value into an ENUM (that is, a string not
present in the list of permitted values), the empty string is
inserted instead as a special error value.
Read about ENUM.
Been searching on Google for a while now without finding the answer to my problem. I have like 10 tables where 5 of them contains 150 rows. I want to add 15 rows to these 5 tables, is there any simple solution for this? I know it's easy to add the rows manually but I want to know anyway. What I'm looking for is something like this:
INSERT INTO all_tables VALUES (col1, col2, col3) WHERE row_number() = '150'
Is it possible? Thanks in advance!
You can only target updates to one table at a time, which must always be specified by name. Also, you cannot specify a WHERE clause on an INSERT. Your best bet is probably to write one INSERT and copy and paste for the rest.
You could:
Loop through a list of the relevant table names.
Run a dynamic query like select count(*) into #c1 from SpecifiedTable against the relevant table, returning the count into a declared variable.
If the returned value is 150, run another dynamic query to insert the relevant values into the specified table.
You can find out more about dynamic queries and returning values from them in MySQL here. If this is a once-off, you will probably find it easier to do it manually.
I have more than 3 million rows in my table. When the user try to insert or update this table I have to check the following conditions sequentially.(Business Need)
Does any of the row has same address?
Does any of the row has same postcode?
Does any of the row has same DOB?
Obviously the newly inserted or updated row will match lot of the records from this table.
But the business need is, the matching process should end when the first match (row) found and that row has to returned.
I can easily achieve this using simple "SELECT" query . But it's taking very long time to find the match.
Please suggest some efficient way to do this.
If you're just looking for a way to return after the first match, use LIMIT 1.
You may want to maintain a table of either birth dates or postcodes and have each row link to a user, so that you can easily filter customers down to a smaller set. It would allow you to perform a much faster search on the database.
Example:
dob | userID
1/1/1980 | 235
1/1/1980 | 482
1/1/1980 | 123
2/1/1980 | 521
In that scenario, you only have to read 3 rows from the large users table if your target date is 1/1/1980. It's via a primary key index, too, so it'll be really fast.